-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-24478][SQL] Move projection and filter push down to physical conversion #21503
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 |
---|---|---|
|
@@ -17,15 +17,56 @@ | |
|
||
package org.apache.spark.sql.execution.datasources.v2 | ||
|
||
import org.apache.spark.sql.Strategy | ||
import org.apache.spark.sql.{execution, Strategy} | ||
import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference, AttributeSet} | ||
import org.apache.spark.sql.catalyst.planning.PhysicalOperation | ||
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
import org.apache.spark.sql.execution.SparkPlan | ||
import org.apache.spark.sql.execution.streaming.continuous.{WriteToContinuousDataSource, WriteToContinuousDataSourceExec} | ||
|
||
object DataSourceV2Strategy extends Strategy { | ||
override def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match { | ||
case r: DataSourceV2Relation => | ||
DataSourceV2ScanExec(r.output, r.source, r.options, r.pushedFilters, r.reader) :: Nil | ||
case PhysicalOperation(project, filters, relation: DataSourceV2Relation) => | ||
val projectSet = AttributeSet(project.flatMap(_.references)) | ||
val filterSet = AttributeSet(filters.flatMap(_.references)) | ||
|
||
val projection = if (filterSet.subsetOf(projectSet) && | ||
AttributeSet(relation.output) == projectSet) { | ||
// When the required projection contains all of the filter columns and column pruning alone | ||
// can produce the required projection, push the required projection. | ||
// A final projection may still be needed if the data source produces a different column | ||
// order or if it cannot prune all of the nested columns. | ||
relation.output | ||
} else { | ||
// When there are filter columns not already in the required projection or when the required | ||
// projection is more complicated than column pruning, base column pruning on the set of | ||
// all columns needed by both. | ||
(projectSet ++ filterSet).toSeq | ||
} | ||
|
||
val reader = relation.newReader | ||
|
||
|
||
val output = DataSourceV2Relation.pushRequiredColumns(relation, reader, | ||
projection.asInstanceOf[Seq[AttributeReference]].toStructType) | ||
|
||
val (postScanFilters, pushedFilters) = DataSourceV2Relation.pushFilters(reader, filters) | ||
|
||
logInfo(s"Post-Scan Filters: ${postScanFilters.mkString(",")}") | ||
logInfo(s"Pushed Filters: ${pushedFilters.mkString(", ")}") | ||
|
||
val scan = DataSourceV2ScanExec( | ||
output, relation.source, relation.options, pushedFilters, reader) | ||
|
||
val filter = postScanFilters.reduceLeftOption(And) | ||
val withFilter = filter.map(execution.FilterExec(_, scan)).getOrElse(scan) | ||
|
||
val withProjection = if (withFilter.output != project) { | ||
execution.ProjectExec(project, withFilter) | ||
} else { | ||
withFilter | ||
} | ||
|
||
withProjection :: Nil | ||
|
||
case r: StreamingDataSourceV2Relation => | ||
DataSourceV2ScanExec(r.output, r.source, r.options, r.pushedFilters, r.reader) :: Nil | ||
|
This file was deleted.
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.
I think we don't need to override
newInstance
now.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.
I thought that initially, but the canonicalization test was failing without this.