-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27226][SQL] Reduce the code duplicate when upgrading built-in Hive #24166
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 all commits
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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.orc | ||
|
|
||
| import org.apache.spark.sql.sources.{And, Filter} | ||
| import org.apache.spark.sql.types.{AtomicType, BinaryType, DataType} | ||
|
|
||
| /** | ||
| * Methods that can be shared when upgrading the built-in Hive. | ||
| */ | ||
| trait OrcFiltersBase { | ||
|
|
||
| private[sql] def buildTree(filters: Seq[Filter]): Option[Filter] = { | ||
| filters match { | ||
| case Seq() => None | ||
| case Seq(filter) => Some(filter) | ||
| case Seq(filter1, filter2) => Some(And(filter1, filter2)) | ||
| case _ => // length > 2 | ||
| val (left, right) = filters.splitAt(filters.length / 2) | ||
| Some(And(buildTree(left).get, buildTree(right).get)) | ||
| } | ||
| } | ||
|
|
||
| // 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. | ||
| protected def quoteAttributeNameIfNeeded(name: String) : String = { | ||
| if (!name.contains("`") && name.contains(".")) { | ||
| s"`$name`" | ||
| } else { | ||
| name | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return true if this is a searchable type in ORC. | ||
| * Both CharType and VarcharType are cleaned at AstBuilder. | ||
| */ | ||
| protected def isSearchableType(dataType: DataType) = dataType match { | ||
| case BinaryType => false | ||
| case _: AtomicType => true | ||
| case _ => false | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * 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.orc | ||
|
|
||
| import java.sql.Date | ||
|
|
||
| import org.apache.orc.storage.common.`type`.HiveDecimal | ||
| import org.apache.orc.storage.ql.exec.vector.VectorizedRowBatch | ||
| import org.apache.orc.storage.ql.io.sarg.{SearchArgument => OrcSearchArgument} | ||
| import org.apache.orc.storage.ql.io.sarg.PredicateLeaf.{Operator => OrcOperator} | ||
| import org.apache.orc.storage.serde2.io.{DateWritable, HiveDecimalWritable} | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.SpecializedGetters | ||
| import org.apache.spark.sql.types.Decimal | ||
|
|
||
| /** | ||
| * Various utilities for ORC used to upgrade the built-in Hive. | ||
| */ | ||
| private[sql] object OrcShimUtils { | ||
|
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. not a big deal but just leave a note for the future, |
||
|
|
||
| class VectorizedRowBatchWrap(val batch: VectorizedRowBatch) {} | ||
srowen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private[sql] type Operator = OrcOperator | ||
| private[sql] type SearchArgument = OrcSearchArgument | ||
|
Member
Author
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. Add these two type aliases to avoid copying |
||
|
|
||
| def getSqlDate(value: Any): Date = value.asInstanceOf[DateWritable].get | ||
|
Member
Author
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. The below functions to avoid copying |
||
|
|
||
| def getDecimal(value: Any): Decimal = { | ||
| val decimal = value.asInstanceOf[HiveDecimalWritable].getHiveDecimal() | ||
| Decimal(decimal.bigDecimalValue, decimal.precision(), decimal.scale()) | ||
| } | ||
|
|
||
| def getDateWritable(reuseObj: Boolean): (SpecializedGetters, Int) => DateWritable = { | ||
| if (reuseObj) { | ||
| val result = new DateWritable() | ||
| (getter, ordinal) => | ||
| result.set(getter.getInt(ordinal)) | ||
| result | ||
| } else { | ||
| (getter: SpecializedGetters, ordinal: Int) => | ||
| new DateWritable(getter.getInt(ordinal)) | ||
| } | ||
| } | ||
|
|
||
| def getHiveDecimalWritable(precision: Int, scale: Int): | ||
| (SpecializedGetters, Int) => HiveDecimalWritable = { | ||
| (getter, ordinal) => | ||
| val d = getter.getDecimal(ordinal, precision, scale) | ||
| new HiveDecimalWritable(HiveDecimal.create(d.toJavaBigDecimal)) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,34 +89,6 @@ class OrcFilterSuite extends OrcTest with SharedSQLContext { | |
| checkFilterPredicate(df, predicate, checkLogicalOperator) | ||
| } | ||
|
|
||
| protected def checkNoFilterPredicate | ||
|
Member
Author
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. Moved to super class |
||
| (predicate: Predicate, noneSupported: Boolean = false) | ||
| (implicit df: DataFrame): Unit = { | ||
| val output = predicate.collect { case a: Attribute => a }.distinct | ||
| val query = df | ||
| .select(output.map(e => Column(e)): _*) | ||
| .where(Column(predicate)) | ||
|
|
||
| query.queryExecution.optimizedPlan match { | ||
| case PhysicalOperation(_, filters, | ||
| DataSourceV2Relation(orcTable: OrcTable, _, options)) => | ||
| assert(filters.nonEmpty, "No filter is analyzed from the given query") | ||
| val scanBuilder = orcTable.newScanBuilder(options) | ||
| scanBuilder.pushFilters(filters.flatMap(DataSourceStrategy.translateFilter).toArray) | ||
| val pushedFilters = scanBuilder.pushedFilters() | ||
| if (noneSupported) { | ||
| assert(pushedFilters.isEmpty, "Unsupported filters should not show in pushed filters") | ||
| } else { | ||
| assert(pushedFilters.nonEmpty, "No filter is pushed down") | ||
| val maybeFilter = OrcFilters.createFilter(query.schema, pushedFilters) | ||
| assert(maybeFilter.isEmpty, s"Couldn't generate filter predicate for $pushedFilters") | ||
| } | ||
|
|
||
| case _ => | ||
| throw new AnalysisException("Can not match OrcTable in the query.") | ||
| } | ||
| } | ||
|
|
||
| test("filter pushdown - integer") { | ||
| withOrcDataFrame((1 to 4).map(i => Tuple1(Option(i)))) { implicit df => | ||
| checkFilterPredicate('_1.isNull, PredicateLeaf.Operator.IS_NULL) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,11 @@ import scala.reflect.runtime.universe.TypeTag | |
| import org.scalatest.BeforeAndAfterAll | ||
|
|
||
| import org.apache.spark.sql._ | ||
| import org.apache.spark.sql.execution.datasources.FileBasedDataSourceTest | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, Predicate} | ||
| import org.apache.spark.sql.catalyst.planning.PhysicalOperation | ||
| import org.apache.spark.sql.execution.datasources.{DataSourceStrategy, FileBasedDataSourceTest} | ||
| import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
| import org.apache.spark.sql.execution.datasources.v2.orc.OrcTable | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.SQLConf.ORC_IMPLEMENTATION | ||
|
|
||
|
|
@@ -104,4 +108,32 @@ abstract class OrcTest extends QueryTest with FileBasedDataSourceTest with Befor | |
| assert(actual < numRows) | ||
| } | ||
| } | ||
|
|
||
| protected def checkNoFilterPredicate | ||
| (predicate: Predicate, noneSupported: Boolean = false) | ||
| (implicit df: DataFrame): Unit = { | ||
| val output = predicate.collect { case a: Attribute => a }.distinct | ||
| val query = df | ||
| .select(output.map(e => Column(e)): _*) | ||
|
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. No big deal, but |
||
| .where(Column(predicate)) | ||
|
|
||
| query.queryExecution.optimizedPlan match { | ||
| case PhysicalOperation(_, filters, | ||
| DataSourceV2Relation(orcTable: OrcTable, _, options)) => | ||
| assert(filters.nonEmpty, "No filter is analyzed from the given query") | ||
| val scanBuilder = orcTable.newScanBuilder(options) | ||
| scanBuilder.pushFilters(filters.flatMap(DataSourceStrategy.translateFilter).toArray) | ||
| val pushedFilters = scanBuilder.pushedFilters() | ||
| if (noneSupported) { | ||
| assert(pushedFilters.isEmpty, "Unsupported filters should not show in pushed filters") | ||
| } else { | ||
| assert(pushedFilters.nonEmpty, "No filter is pushed down") | ||
| val maybeFilter = OrcFilters.createFilter(query.schema, pushedFilters) | ||
| assert(maybeFilter.isEmpty, s"Couldn't generate filter predicate for $pushedFilters") | ||
| } | ||
|
|
||
| case _ => | ||
| throw new AnalysisException("Can not match OrcTable in the query.") | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.