-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-25557][SQL] Nested column predicate pushdown for ORC #28761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9903e05
1486382
e12939e
bd691ed
e76b5f4
7175e7c
0747fcd
558db46
dc77290
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,11 @@ | |
|
|
||
| package org.apache.spark.sql.execution.datasources.orc | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap | ||
| import org.apache.spark.sql.sources.{And, Filter} | ||
| import org.apache.spark.sql.types.{AtomicType, BinaryType, DataType} | ||
| import org.apache.spark.sql.types.{AtomicType, BinaryType, DataType, StructField, StructType} | ||
|
|
||
| /** | ||
| * Methods that can be shared when upgrading the built-in Hive. | ||
|
|
@@ -37,12 +40,44 @@ trait OrcFiltersBase { | |
| } | ||
|
|
||
| /** | ||
| * Return true if this is a searchable type in ORC. | ||
| * Both CharType and VarcharType are cleaned at AstBuilder. | ||
| * This method returns a map which contains ORC field name and data type. Each key | ||
| * represents a column; `dots` are used as separators for nested columns. If any part | ||
| * of the names contains `dots`, it is quoted to avoid confusion. See | ||
| * `org.apache.spark.sql.connector.catalog.quote` for implementation details. | ||
| */ | ||
| protected[sql] def isSearchableType(dataType: DataType) = dataType match { | ||
| case BinaryType => false | ||
| case _: AtomicType => true | ||
| case _ => false | ||
| protected[sql] def getNameToOrcFieldMap( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| schema: StructType, | ||
| caseSensitive: Boolean): Map[String, DataType] = { | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper | ||
|
viirya marked this conversation as resolved.
|
||
|
|
||
| def getPrimitiveFields( | ||
| fields: Seq[StructField], | ||
| parentFieldNames: Seq[String] = Seq.empty): Seq[(String, DataType)] = { | ||
| fields.flatMap { f => | ||
| f.dataType match { | ||
| case st: StructType => | ||
| getPrimitiveFields(st.fields, parentFieldNames :+ f.name) | ||
| case BinaryType => None | ||
| case _: AtomicType => | ||
| Some(((parentFieldNames :+ f.name).quoted, f.dataType)) | ||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val primitiveFields = getPrimitiveFields(schema.fields) | ||
| if (caseSensitive) { | ||
| primitiveFields.toMap | ||
|
dongjoon-hyun marked this conversation as resolved.
|
||
| } else { | ||
| // Don't consider ambiguity here, i.e. more than one field is matched in case insensitive | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // mode, just skip pushdown for these fields, they will trigger Exception when reading, | ||
| // See: SPARK-25175. | ||
| val dedupPrimitiveFields = | ||
| primitiveFields | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation? - val dedupPrimitiveFields =
- primitiveFields
+ val dedupPrimitiveFields = primitiveFields |
||
| .groupBy(_._1.toLowerCase(Locale.ROOT)) | ||
| .filter(_._2.size == 1) | ||
| .mapValues(_.head._2) | ||
| CaseInsensitiveMap(dedupPrimitiveFields) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.datasources | ||
|
|
||
| import org.apache.spark.sql.{DataFrame, Row} | ||
| import org.apache.spark.sql.functions.struct | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * Defines common stuff for nested column predicate pushdown test, e.g. `ParquetFilterSuite`. | ||
| */ | ||
| trait NestedColumnPredicateTest { | ||
| /** | ||
| * Takes single level `inputDF` dataframe to generate multi-level nested | ||
| * dataframes as new test data. | ||
| * | ||
| * This method accepts a function to run test. The given function takes three | ||
| * parameters: a DataFrame which ranges from zero-nested to multi-level nested, | ||
| * a string of the primitive field name, and a function that produces expected | ||
| * result of collected column. | ||
| */ | ||
| protected def withNestedDataFrame(inputDF: DataFrame) | ||
| (runTest: (DataFrame, String, Any => Any) => Unit): Unit = { | ||
| assert(inputDF.schema.fields.length == 1) | ||
| assert(!inputDF.schema.fields.head.dataType.isInstanceOf[StructType]) | ||
| val df = inputDF.toDF("temp") | ||
| Seq( | ||
| ( | ||
| df.withColumnRenamed("temp", "a"), | ||
| "a", // zero nesting | ||
| (x: Any) => x), | ||
| ( | ||
| df.withColumn("a", struct(df("temp") as "b")).drop("temp"), | ||
| "a.b", // one level nesting | ||
| (x: Any) => Row(x)), | ||
| ( | ||
| df.withColumn("a", struct(struct(df("temp") as "c") as "b")).drop("temp"), | ||
| "a.b.c", // two level nesting | ||
| (x: Any) => Row(Row(x)) | ||
| ), | ||
| ( | ||
| df.withColumnRenamed("temp", "a.b"), | ||
| "`a.b`", // zero nesting with column name containing `dots` | ||
| (x: Any) => x | ||
| ), | ||
| ( | ||
| df.withColumn("a.b", struct(df("temp") as "c.d") ).drop("temp"), | ||
| "`a.b`.`c.d`", // one level nesting with column names containing `dots` | ||
| (x: Any) => Row(x) | ||
| ) | ||
| ).foreach { case (df, colName, resultFun) => | ||
| runTest(df, colName, resultFun) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ import org.apache.orc.storage.serde2.io.HiveDecimalWritable | |
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils.{instantToMicros, localDateToDays, toJavaDate, toJavaTimestamp} | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.quoteIfNeeded | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.sources.Filter | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
|
|
@@ -68,11 +68,9 @@ private[sql] object OrcFilters extends OrcFiltersBase { | |
| * Create ORC filter as a SearchArgument instance. | ||
| */ | ||
| def createFilter(schema: StructType, filters: Seq[Filter]): Option[SearchArgument] = { | ||
| val dataTypeMap = schema.map(f => quoteIfNeeded(f.name) -> f.dataType).toMap | ||
| val dataTypeMap = OrcFilters.getNameToOrcFieldMap(schema, SQLConf.get.caseSensitiveAnalysis) | ||
| // Combines all convertible filters using `And` to produce a single conjunction | ||
| // TODO (SPARK-25557): ORC doesn't support nested predicate pushdown, so they are removed. | ||
| val newFilters = filters.filter(!_.containsNestedColumn) | ||
| val conjunctionOptional = buildTree(convertibleFilters(schema, dataTypeMap, newFilters)) | ||
| val conjunctionOptional = buildTree(convertibleFilters(schema, dataTypeMap, filters)) | ||
| conjunctionOptional.map { conjunction => | ||
| // Then tries to build a single ORC `SearchArgument` for the conjunction predicate. | ||
| // The input predicate is fully convertible. There should not be any empty result in the | ||
|
|
@@ -231,37 +229,37 @@ private[sql] object OrcFilters extends OrcFiltersBase { | |
| // Since ORC 1.5.0 (ORC-323), we need to quote for column names with `.` characters | ||
| // in order to distinguish predicate pushdown for nested columns. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we removed |
||
| expression match { | ||
| case EqualTo(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case EqualTo(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startAnd().equals(name, getType(name), castedValue).end()) | ||
|
|
||
| case EqualNullSafe(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case EqualNullSafe(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startAnd().nullSafeEquals(name, getType(name), castedValue).end()) | ||
|
|
||
| case LessThan(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case LessThan(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startAnd().lessThan(name, getType(name), castedValue).end()) | ||
|
|
||
| case LessThanOrEqual(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case LessThanOrEqual(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startAnd().lessThanEquals(name, getType(name), castedValue).end()) | ||
|
|
||
| case GreaterThan(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case GreaterThan(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startNot().lessThanEquals(name, getType(name), castedValue).end()) | ||
|
|
||
| case GreaterThanOrEqual(name, value) if isSearchableType(dataTypeMap(name)) => | ||
| case GreaterThanOrEqual(name, value) if dataTypeMap.contains(name) => | ||
| val castedValue = castLiteralValue(value, dataTypeMap(name)) | ||
| Some(builder.startNot().lessThan(name, getType(name), castedValue).end()) | ||
|
|
||
| case IsNull(name) if isSearchableType(dataTypeMap(name)) => | ||
| case IsNull(name) if dataTypeMap.contains(name) => | ||
| Some(builder.startAnd().isNull(name, getType(name)).end()) | ||
|
|
||
| case IsNotNull(name) if isSearchableType(dataTypeMap(name)) => | ||
| case IsNotNull(name) if dataTypeMap.contains(name) => | ||
| Some(builder.startNot().isNull(name, getType(name)).end()) | ||
|
|
||
| case In(name, values) if isSearchableType(dataTypeMap(name)) => | ||
| case In(name, values) if dataTypeMap.contains(name) => | ||
| val castedValues = values.map(v => castLiteralValue(v, dataTypeMap(name))) | ||
| Some(builder.startAnd().in(name, getType(name), | ||
| castedValues.map(_.asInstanceOf[AnyRef]): _*).end()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quote->quoted.