You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2017/10/12 12:34:24 UTC
spark git commit: [SPARK-22197][SQL] push down operators to data
source before planning
Repository: spark
Updated Branches:
refs/heads/master b5c1ef7a8 -> 73d80ec49
[SPARK-22197][SQL] push down operators to data source before planning
## What changes were proposed in this pull request?
As we discussed in https://github.com/apache/spark/pull/19136#discussion_r137023744 , we should push down operators to data source before planning, so that data source can report statistics more accurate.
This PR also includes some cleanup for the read path.
## How was this patch tested?
existing tests.
Author: Wenchen Fan <we...@databricks.com>
Closes #19424 from cloud-fan/follow.
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/73d80ec4
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/73d80ec4
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/73d80ec4
Branch: refs/heads/master
Commit: 73d80ec49713605d6a589e688020f0fc2d6feab2
Parents: b5c1ef7
Author: Wenchen Fan <we...@databricks.com>
Authored: Thu Oct 12 20:34:03 2017 +0800
Committer: Wenchen Fan <we...@databricks.com>
Committed: Thu Oct 12 20:34:03 2017 +0800
----------------------------------------------------------------------
.../spark/sql/sources/v2/ReadSupport.java | 5 +-
.../sql/sources/v2/ReadSupportWithSchema.java | 5 +-
.../spark/sql/sources/v2/reader/DataReader.java | 4 +
.../sources/v2/reader/DataSourceV2Reader.java | 2 +-
.../spark/sql/sources/v2/reader/ReadTask.java | 3 +-
.../reader/SupportsPushDownCatalystFilters.java | 8 ++
.../v2/reader/SupportsPushDownFilters.java | 8 ++
.../org/apache/spark/sql/DataFrameReader.scala | 5 +-
.../spark/sql/execution/SparkOptimizer.scala | 4 +-
.../datasources/v2/DataSourceReaderHolder.scala | 68 +++++++++
.../datasources/v2/DataSourceV2Relation.scala | 8 +-
.../datasources/v2/DataSourceV2ScanExec.scala | 22 ++-
.../datasources/v2/DataSourceV2Strategy.scala | 60 +-------
.../v2/PushDownOperatorsToDataSource.scala | 140 +++++++++++++++++++
.../sources/v2/JavaAdvancedDataSourceV2.java | 5 +
.../sql/sources/v2/DataSourceV2Suite.scala | 2 +
16 files changed, 262 insertions(+), 87 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupport.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupport.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupport.java
index ab5254a..ee489ad 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupport.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupport.java
@@ -30,9 +30,8 @@ public interface ReadSupport {
/**
* Creates a {@link DataSourceV2Reader} to scan the data from this data source.
*
- * @param options the options for this data source reader, which is an immutable case-insensitive
- * string-to-string map.
- * @return a reader that implements the actual read logic.
+ * @param options the options for the returned data source reader, which is an immutable
+ * case-insensitive string-to-string map.
*/
DataSourceV2Reader createReader(DataSourceV2Options options);
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupportWithSchema.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupportWithSchema.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupportWithSchema.java
index c13aeca..74e81a2 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupportWithSchema.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/ReadSupportWithSchema.java
@@ -39,9 +39,8 @@ public interface ReadSupportWithSchema {
* physical schema of the underlying storage of this data source reader, e.g.
* CSV files, JSON files, etc, while this reader may not read data with full
* schema, as column pruning or other optimizations may happen.
- * @param options the options for this data source reader, which is an immutable case-insensitive
- * string-to-string map.
- * @return a reader that implements the actual read logic.
+ * @param options the options for the returned data source reader, which is an immutable
+ * case-insensitive string-to-string map.
*/
DataSourceV2Reader createReader(StructType schema, DataSourceV2Options options);
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataReader.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataReader.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataReader.java
index cfafc1a..95e0915 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataReader.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataReader.java
@@ -24,6 +24,10 @@ import org.apache.spark.annotation.InterfaceStability;
/**
* A data reader returned by {@link ReadTask#createReader()} and is responsible for outputting data
* for a RDD partition.
+ *
+ * Note that, Currently the type `T` can only be {@link org.apache.spark.sql.Row} for normal data
+ * source readers, or {@link org.apache.spark.sql.catalyst.expressions.UnsafeRow} for data source
+ * readers that mix in {@link SupportsScanUnsafeRow}.
*/
@InterfaceStability.Evolving
public interface DataReader<T> extends Closeable {
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataSourceV2Reader.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataSourceV2Reader.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataSourceV2Reader.java
index fb4d5c0..5989a4a 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataSourceV2Reader.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/DataSourceV2Reader.java
@@ -30,7 +30,7 @@ import org.apache.spark.sql.types.StructType;
* {@link org.apache.spark.sql.sources.v2.ReadSupportWithSchema#createReader(
* StructType, org.apache.spark.sql.sources.v2.DataSourceV2Options)}.
* It can mix in various query optimization interfaces to speed up the data scan. The actual scan
- * logic should be delegated to {@link ReadTask}s that are returned by {@link #createReadTasks()}.
+ * logic is delegated to {@link ReadTask}s that are returned by {@link #createReadTasks()}.
*
* There are mainly 3 kinds of query optimizations:
* 1. Operators push-down. E.g., filter push-down, required columns push-down(aka column
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ReadTask.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ReadTask.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ReadTask.java
index 7885bfc..01362df 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ReadTask.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/ReadTask.java
@@ -27,7 +27,8 @@ import org.apache.spark.annotation.InterfaceStability;
* is similar to the relationship between {@link Iterable} and {@link java.util.Iterator}.
*
* Note that, the read task will be serialized and sent to executors, then the data reader will be
- * created on executors and do the actual reading.
+ * created on executors and do the actual reading. So {@link ReadTask} must be serializable and
+ * {@link DataReader} doesn't need to be.
*/
@InterfaceStability.Evolving
public interface ReadTask<T> extends Serializable {
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownCatalystFilters.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownCatalystFilters.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownCatalystFilters.java
index 19d7062..d609177 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownCatalystFilters.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownCatalystFilters.java
@@ -40,4 +40,12 @@ public interface SupportsPushDownCatalystFilters {
* Pushes down filters, and returns unsupported filters.
*/
Expression[] pushCatalystFilters(Expression[] filters);
+
+ /**
+ * Returns the catalyst filters that are pushed in {@link #pushCatalystFilters(Expression[])}.
+ * It's possible that there is no filters in the query and
+ * {@link #pushCatalystFilters(Expression[])} is never called, empty array should be returned for
+ * this case.
+ */
+ Expression[] pushedCatalystFilters();
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownFilters.java
----------------------------------------------------------------------
diff --git a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownFilters.java b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownFilters.java
index d4b509e..d6f297c 100644
--- a/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownFilters.java
+++ b/sql/core/src/main/java/org/apache/spark/sql/sources/v2/reader/SupportsPushDownFilters.java
@@ -18,6 +18,7 @@
package org.apache.spark.sql.sources.v2.reader;
import org.apache.spark.annotation.InterfaceStability;
+import org.apache.spark.sql.catalyst.expressions.Expression;
import org.apache.spark.sql.sources.Filter;
/**
@@ -35,4 +36,11 @@ public interface SupportsPushDownFilters {
* Pushes down filters, and returns unsupported filters.
*/
Filter[] pushFilters(Filter[] filters);
+
+ /**
+ * Returns the filters that are pushed in {@link #pushFilters(Filter[])}.
+ * It's possible that there is no filters in the query and {@link #pushFilters(Filter[])}
+ * is never called, empty array should be returned for this case.
+ */
+ Filter[] pushedFilters();
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala b/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala
index 78b668c..17966ee 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala
@@ -184,7 +184,6 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
val cls = DataSource.lookupDataSource(source)
if (classOf[DataSourceV2].isAssignableFrom(cls)) {
- val dataSource = cls.newInstance()
val options = new DataSourceV2Options(extraOptions.asJava)
val reader = (cls.newInstance(), userSpecifiedSchema) match {
@@ -194,8 +193,8 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
case (ds: ReadSupport, None) =>
ds.createReader(options)
- case (_: ReadSupportWithSchema, None) =>
- throw new AnalysisException(s"A schema needs to be specified when using $dataSource.")
+ case (ds: ReadSupportWithSchema, None) =>
+ throw new AnalysisException(s"A schema needs to be specified when using $ds.")
case (ds: ReadSupport, Some(schema)) =>
val reader = ds.createReader(options)
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala
index 00ff4c8..1c8e405 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkOptimizer.scala
@@ -21,6 +21,7 @@ import org.apache.spark.sql.ExperimentalMethods
import org.apache.spark.sql.catalyst.catalog.SessionCatalog
import org.apache.spark.sql.catalyst.optimizer.Optimizer
import org.apache.spark.sql.execution.datasources.PruneFileSourcePartitions
+import org.apache.spark.sql.execution.datasources.v2.PushDownOperatorsToDataSource
import org.apache.spark.sql.execution.python.ExtractPythonUDFFromAggregate
class SparkOptimizer(
@@ -31,7 +32,8 @@ class SparkOptimizer(
override def batches: Seq[Batch] = (preOptimizationBatches ++ super.batches :+
Batch("Optimize Metadata Only Query", Once, OptimizeMetadataOnlyQuery(catalog)) :+
Batch("Extract Python UDF from Aggregate", Once, ExtractPythonUDFFromAggregate) :+
- Batch("Prune File Source Table Partitions", Once, PruneFileSourcePartitions)) ++
+ Batch("Prune File Source Table Partitions", Once, PruneFileSourcePartitions) :+
+ Batch("Push down operators to data source scan", Once, PushDownOperatorsToDataSource)) ++
postHocOptimizationBatches :+
Batch("User Provided Optimizers", fixedPoint, experimentalMethods.extraOptimizations: _*)
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceReaderHolder.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceReaderHolder.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceReaderHolder.scala
new file mode 100644
index 0000000..6093df2
--- /dev/null
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceReaderHolder.scala
@@ -0,0 +1,68 @@
+/*
+ * 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.v2
+
+import java.util.Objects
+
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference}
+import org.apache.spark.sql.sources.v2.reader._
+
+/**
+ * A base class for data source reader holder with customized equals/hashCode methods.
+ */
+trait DataSourceReaderHolder {
+
+ /**
+ * The full output of the data source reader, without column pruning.
+ */
+ def fullOutput: Seq[AttributeReference]
+
+ /**
+ * The held data source reader.
+ */
+ def reader: DataSourceV2Reader
+
+ /**
+ * The metadata of this data source reader that can be used for equality test.
+ */
+ private def metadata: Seq[Any] = {
+ val filters: Any = reader match {
+ case s: SupportsPushDownCatalystFilters => s.pushedCatalystFilters().toSet
+ case s: SupportsPushDownFilters => s.pushedFilters().toSet
+ case _ => Nil
+ }
+ Seq(fullOutput, reader.getClass, reader.readSchema(), filters)
+ }
+
+ def canEqual(other: Any): Boolean
+
+ override def equals(other: Any): Boolean = other match {
+ case other: DataSourceReaderHolder =>
+ canEqual(other) && metadata.length == other.metadata.length &&
+ metadata.zip(other.metadata).forall { case (l, r) => l == r }
+ case _ => false
+ }
+
+ override def hashCode(): Int = {
+ metadata.map(Objects.hashCode).foldLeft(0)((a, b) => 31 * a + b)
+ }
+
+ lazy val output: Seq[Attribute] = reader.readSchema().map(_.name).map { name =>
+ fullOutput.find(_.name == name).get
+ }
+}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala
index 3c9b598..7eb99a6 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala
@@ -19,11 +19,13 @@ package org.apache.spark.sql.execution.datasources.v2
import org.apache.spark.sql.catalyst.expressions.AttributeReference
import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, Statistics}
-import org.apache.spark.sql.sources.v2.reader.{DataSourceV2Reader, SupportsReportStatistics}
+import org.apache.spark.sql.sources.v2.reader._
case class DataSourceV2Relation(
- output: Seq[AttributeReference],
- reader: DataSourceV2Reader) extends LeafNode {
+ fullOutput: Seq[AttributeReference],
+ reader: DataSourceV2Reader) extends LeafNode with DataSourceReaderHolder {
+
+ override def canEqual(other: Any): Boolean = other.isInstanceOf[DataSourceV2Relation]
override def computeStats(): Statistics = reader match {
case r: SupportsReportStatistics =>
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2ScanExec.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2ScanExec.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2ScanExec.scala
index 7999c0c..addc12a 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2ScanExec.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2ScanExec.scala
@@ -29,20 +29,14 @@ import org.apache.spark.sql.execution.metric.SQLMetrics
import org.apache.spark.sql.sources.v2.reader._
import org.apache.spark.sql.types.StructType
+/**
+ * Physical plan node for scanning data from a data source.
+ */
case class DataSourceV2ScanExec(
- fullOutput: Array[AttributeReference],
- @transient reader: DataSourceV2Reader,
- // TODO: these 3 parameters are only used to determine the equality of the scan node, however,
- // the reader also have this information, and ideally we can just rely on the equality of the
- // reader. The only concern is, the reader implementation is outside of Spark and we have no
- // control.
- readSchema: StructType,
- @transient filters: ExpressionSet,
- hashPartitionKeys: Seq[String]) extends LeafExecNode {
-
- def output: Seq[Attribute] = readSchema.map(_.name).map { name =>
- fullOutput.find(_.name == name).get
- }
+ fullOutput: Seq[AttributeReference],
+ @transient reader: DataSourceV2Reader) extends LeafExecNode with DataSourceReaderHolder {
+
+ override def canEqual(other: Any): Boolean = other.isInstanceOf[DataSourceV2ScanExec]
override def references: AttributeSet = AttributeSet.empty
@@ -74,7 +68,7 @@ class RowToUnsafeRowReadTask(rowReadTask: ReadTask[Row], schema: StructType)
override def preferredLocations: Array[String] = rowReadTask.preferredLocations
override def createReader: DataReader[UnsafeRow] = {
- new RowToUnsafeDataReader(rowReadTask.createReader, RowEncoder.apply(schema))
+ new RowToUnsafeDataReader(rowReadTask.createReader, RowEncoder.apply(schema).resolveAndBind())
}
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala
index b80f695..f2cda00 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala
@@ -29,64 +29,8 @@ import org.apache.spark.sql.sources.v2.reader._
object DataSourceV2Strategy extends Strategy {
// TODO: write path
override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
- case PhysicalOperation(projects, filters, DataSourceV2Relation(output, reader)) =>
- val stayUpFilters: Seq[Expression] = reader match {
- case r: SupportsPushDownCatalystFilters =>
- r.pushCatalystFilters(filters.toArray)
-
- case r: SupportsPushDownFilters =>
- // A map from original Catalyst expressions to corresponding translated data source
- // filters. If a predicate is not in this map, it means it cannot be pushed down.
- val translatedMap: Map[Expression, Filter] = filters.flatMap { p =>
- DataSourceStrategy.translateFilter(p).map(f => p -> f)
- }.toMap
-
- // Catalyst predicate expressions that cannot be converted to data source filters.
- val nonConvertiblePredicates = filters.filterNot(translatedMap.contains)
-
- // Data source filters that cannot be pushed down. An unhandled filter means
- // the data source cannot guarantee the rows returned can pass the filter.
- // As a result we must return it so Spark can plan an extra filter operator.
- val unhandledFilters = r.pushFilters(translatedMap.values.toArray).toSet
- val unhandledPredicates = translatedMap.filter { case (_, f) =>
- unhandledFilters.contains(f)
- }.keys
-
- nonConvertiblePredicates ++ unhandledPredicates
-
- case _ => filters
- }
-
- val attrMap = AttributeMap(output.zip(output))
- val projectSet = AttributeSet(projects.flatMap(_.references))
- val filterSet = AttributeSet(stayUpFilters.flatMap(_.references))
-
- // Match original case of attributes.
- // TODO: nested fields pruning
- val requiredColumns = (projectSet ++ filterSet).toSeq.map(attrMap)
- reader match {
- case r: SupportsPushDownRequiredColumns =>
- r.pruneColumns(requiredColumns.toStructType)
- case _ =>
- }
-
- val scan = DataSourceV2ScanExec(
- output.toArray,
- reader,
- reader.readSchema(),
- ExpressionSet(filters),
- Nil)
-
- val filterCondition = stayUpFilters.reduceLeftOption(And)
- val withFilter = filterCondition.map(FilterExec(_, scan)).getOrElse(scan)
-
- val withProject = if (projects == withFilter.output) {
- withFilter
- } else {
- ProjectExec(projects, withFilter)
- }
-
- withProject :: Nil
+ case DataSourceV2Relation(output, reader) =>
+ DataSourceV2ScanExec(output, reader) :: Nil
case _ => Nil
}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PushDownOperatorsToDataSource.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PushDownOperatorsToDataSource.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PushDownOperatorsToDataSource.scala
new file mode 100644
index 0000000..0c17081
--- /dev/null
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/PushDownOperatorsToDataSource.scala
@@ -0,0 +1,140 @@
+/*
+ * 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.v2
+
+import org.apache.spark.sql.catalyst.expressions.{And, Attribute, AttributeMap, Expression, NamedExpression, PredicateHelper}
+import org.apache.spark.sql.catalyst.optimizer.RemoveRedundantProject
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.DataSourceStrategy
+import org.apache.spark.sql.sources
+import org.apache.spark.sql.sources.v2.reader._
+
+/**
+ * Pushes down various operators to the underlying data source for better performance. Operators are
+ * being pushed down with a specific order. As an example, given a LIMIT has a FILTER child, you
+ * can't push down LIMIT if FILTER is not completely pushed down. When both are pushed down, the
+ * data source should execute FILTER before LIMIT. And required columns are calculated at the end,
+ * because when more operators are pushed down, we may need less columns at Spark side.
+ */
+object PushDownOperatorsToDataSource extends Rule[LogicalPlan] with PredicateHelper {
+ override def apply(plan: LogicalPlan): LogicalPlan = {
+ // Note that, we need to collect the target operator along with PROJECT node, as PROJECT may
+ // appear in many places for column pruning.
+ // TODO: Ideally column pruning should be implemented via a plan property that is propagated
+ // top-down, then we can simplify the logic here and only collect target operators.
+ val filterPushed = plan transformUp {
+ case FilterAndProject(fields, condition, r @ DataSourceV2Relation(_, reader)) =>
+ // Non-deterministic expressions are stateful and we must keep the input sequence unchanged
+ // to avoid changing the result. This means, we can't evaluate the filter conditions that
+ // are after the first non-deterministic condition ahead. Here we only try to push down
+ // deterministic conditions that are before the first non-deterministic condition.
+ val (candidates, containingNonDeterministic) =
+ splitConjunctivePredicates(condition).span(_.deterministic)
+
+ val stayUpFilters: Seq[Expression] = reader match {
+ case r: SupportsPushDownCatalystFilters =>
+ r.pushCatalystFilters(candidates.toArray)
+
+ case r: SupportsPushDownFilters =>
+ // A map from original Catalyst expressions to corresponding translated data source
+ // filters. If a predicate is not in this map, it means it cannot be pushed down.
+ val translatedMap: Map[Expression, sources.Filter] = candidates.flatMap { p =>
+ DataSourceStrategy.translateFilter(p).map(f => p -> f)
+ }.toMap
+
+ // Catalyst predicate expressions that cannot be converted to data source filters.
+ val nonConvertiblePredicates = candidates.filterNot(translatedMap.contains)
+
+ // Data source filters that cannot be pushed down. An unhandled filter means
+ // the data source cannot guarantee the rows returned can pass the filter.
+ // As a result we must return it so Spark can plan an extra filter operator.
+ val unhandledFilters = r.pushFilters(translatedMap.values.toArray).toSet
+ val unhandledPredicates = translatedMap.filter { case (_, f) =>
+ unhandledFilters.contains(f)
+ }.keys
+
+ nonConvertiblePredicates ++ unhandledPredicates
+
+ case _ => candidates
+ }
+
+ val filterCondition = (stayUpFilters ++ containingNonDeterministic).reduceLeftOption(And)
+ val withFilter = filterCondition.map(Filter(_, r)).getOrElse(r)
+ if (withFilter.output == fields) {
+ withFilter
+ } else {
+ Project(fields, withFilter)
+ }
+ }
+
+ // TODO: add more push down rules.
+
+ // TODO: nested fields pruning
+ def pushDownRequiredColumns(plan: LogicalPlan, requiredByParent: Seq[Attribute]): Unit = {
+ plan match {
+ case Project(projectList, child) =>
+ val required = projectList.filter(requiredByParent.contains).flatMap(_.references)
+ pushDownRequiredColumns(child, required)
+
+ case Filter(condition, child) =>
+ val required = requiredByParent ++ condition.references
+ pushDownRequiredColumns(child, required)
+
+ case DataSourceV2Relation(fullOutput, reader) => reader match {
+ case r: SupportsPushDownRequiredColumns =>
+ // Match original case of attributes.
+ val attrMap = AttributeMap(fullOutput.zip(fullOutput))
+ val requiredColumns = requiredByParent.map(attrMap)
+ r.pruneColumns(requiredColumns.toStructType)
+ case _ =>
+ }
+
+ // TODO: there may be more operators can be used to calculate required columns, we can add
+ // more and more in the future.
+ case _ => plan.children.foreach(child => pushDownRequiredColumns(child, child.output))
+ }
+ }
+
+ pushDownRequiredColumns(filterPushed, filterPushed.output)
+ // After column pruning, we may have redundant PROJECT nodes in the query plan, remove them.
+ RemoveRedundantProject(filterPushed)
+ }
+
+ /**
+ * Finds a Filter node(with an optional Project child) above data source relation.
+ */
+ object FilterAndProject {
+ // returns the project list, the filter condition and the data source relation.
+ def unapply(plan: LogicalPlan)
+ : Option[(Seq[NamedExpression], Expression, DataSourceV2Relation)] = plan match {
+
+ case Filter(condition, r: DataSourceV2Relation) => Some((r.output, condition, r))
+
+ case Filter(condition, Project(fields, r: DataSourceV2Relation))
+ if fields.forall(_.deterministic) =>
+ val attributeMap = AttributeMap(fields.map(e => e.toAttribute -> e))
+ val substituted = condition.transform {
+ case a: Attribute => attributeMap.getOrElse(a, a)
+ }
+ Some((fields, substituted, r))
+
+ case _ => None
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/test/java/test/org/apache/spark/sql/sources/v2/JavaAdvancedDataSourceV2.java
----------------------------------------------------------------------
diff --git a/sql/core/src/test/java/test/org/apache/spark/sql/sources/v2/JavaAdvancedDataSourceV2.java b/sql/core/src/test/java/test/org/apache/spark/sql/sources/v2/JavaAdvancedDataSourceV2.java
index 7aacf03..da2c13f 100644
--- a/sql/core/src/test/java/test/org/apache/spark/sql/sources/v2/JavaAdvancedDataSourceV2.java
+++ b/sql/core/src/test/java/test/org/apache/spark/sql/sources/v2/JavaAdvancedDataSourceV2.java
@@ -55,6 +55,11 @@ public class JavaAdvancedDataSourceV2 implements DataSourceV2, ReadSupport {
}
@Override
+ public Filter[] pushedFilters() {
+ return filters;
+ }
+
+ @Override
public List<ReadTask<Row>> createReadTasks() {
List<ReadTask<Row>> res = new ArrayList<>();
http://git-wip-us.apache.org/repos/asf/spark/blob/73d80ec4/sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2Suite.scala
----------------------------------------------------------------------
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2Suite.scala b/sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2Suite.scala
index 9ce93d7..f238e56 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2Suite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2Suite.scala
@@ -129,6 +129,8 @@ class AdvancedDataSourceV2 extends DataSourceV2 with ReadSupport {
Array.empty
}
+ override def pushedFilters(): Array[Filter] = filters
+
override def readSchema(): StructType = {
requiredSchema
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org