-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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
[SPARK-22197][SQL] push down operators to data source before planning #19424
Conversation
Test build #82444 has finished for PR 19424 at commit
|
Test build #82447 has finished for PR 19424 at commit
|
* won't change if we switch the operators within a layer(e.g. we can switch the order of predicates | ||
* and required columns). The operators in layer N can only be pushed down if operators in layer N-1 | ||
* that above the data source relation are all pushed down. As an example, you can't push down limit | ||
* if a filter below limit is not pushed down. |
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.
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.
@@ -32,13 +32,12 @@ import org.apache.spark.sql.types.StructType | |||
case class DataSourceV2ScanExec( |
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.
/**
* Physical plan node for scanning data from a data source.
*/
* layer 1: predicates, required columns. | ||
*/ | ||
object PushDownOperatorsToDataSource extends Rule[LogicalPlan] { | ||
override def apply(plan: LogicalPlan): LogicalPlan = { |
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.
This is an optimizer rule? The input is a LogicalPlan
and the output is still a LogicalPlan
?
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.
yea it's an optimizer rule run before planner
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.
Can we add a test suite for the unit test cases of this rule?
Test build #82461 has finished for PR 19424 at commit
|
Test build #82464 has finished for PR 19424 at commit
|
e880328
to
24f1a75
Compare
Test build #82513 has finished for PR 19424 at commit
|
Test build #82514 has finished for PR 19424 at commit
|
retest this please |
Test build #82547 has finished for PR 19424 at commit
|
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.
A few comments, but mostly it looks about ready to me.
object PushDownOperatorsToDataSource extends Rule[LogicalPlan] with PredicateHelper { | ||
override def apply(plan: LogicalPlan): LogicalPlan = { | ||
// make sure filters are at very bottom. | ||
val prepared = PushDownPredicate(plan) |
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.
Why apply this rule one more time? Is there reason to suspect that predicates won't already be pushed and that one more run would be worth it?
val afterPushDown = prepared transformUp { | ||
case Filter(condition, r @ DataSourceV2Relation(_, reader)) => | ||
val (candidates, containingNonDeterministic) = | ||
splitConjunctivePredicates(condition).span(_.deterministic) |
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.
It isn't immediately clear why you would use span
here instead of partition
. I think it is because span
will produce all deterministic predicates that would be run before the first non-deterministic predicate in an in-order traversal of teh condition, right? If so, then a comment would be really useful to make this clear. I'd also like to see a comment about why deterministic predicates "after" the first non-deterministic predicate shouldn't be pushed down. An example would really help, too.
case r: SupportsPushDownRequiredColumns => | ||
val attrMap = AttributeMap(fullOutput.zip(fullOutput)) | ||
val requiredColumns = requiredByParent.map(attrMap) | ||
// Match original case of attributes. |
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.
Shouldn't this comment be on line 90? That's the purpose of looking up the required attributes in the set of attrs that was produced from the data source's schema right? That lookup happens by exprId, which was generated by the DataSourceV2Relation
so we know we have a copy of the original attribute and case.
case _ => | ||
} | ||
|
||
case _ => plan.children.foreach(child => pushDownRequiredColumns(child, child.output)) |
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.
How do we know that there aren't more cases that need to be supported?
What are the guarantees made by the previous batches in the optimizer? The work done by |
Test build #82586 has finished for PR 19424 at commit
|
@rdblue we assume the previous batches should push down operators as close to data source relations as possible. One special case is column pruning. The |
import org.apache.spark.sql.sources.v2.reader._ | ||
|
||
/** | ||
* A base class for data source reader holder and defines equals/hashCode methods. |
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.
defines
-> with customized
case (l: SupportsPushDownCatalystFilters, r: SupportsPushDownCatalystFilters) => | ||
l.pushedCatalystFilters().toSeq == r.pushedCatalystFilters().toSeq | ||
case (l: SupportsPushDownFilters, r: SupportsPushDownFilters) => | ||
l.pushedFilters().toSeq == r.pushedFilters().toSeq |
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.
The evaluation order of these filters must be the same? If the orders are different, they are still the same, right?
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.
good catch!
I think the current PR is good enough to merge. Later, we still continue improving the Data source API v2. LGTM pending Jenkins. |
Test build #82662 has finished for PR 19424 at commit
|
} | ||
|
||
lazy val output: Seq[Attribute] = reader.readSchema().map(_.name).map { name => | ||
fullOutput.find(_.name == name).get |
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.
Shall we use resolver instead of string comparison?
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.
These names should already be normalized before reaching here.
@@ -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; |
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.
Seems we don't use Expression
here?
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.
good catch. I'll remove it in my following PRs.
late LGTM with minor comments. |
Test build #82665 has finished for PR 19424 at commit
|
thanks for review, merging to master! |
As we discussed in apache#19136 (comment) , 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. existing tests. Author: Wenchen Fan <[email protected]> Closes apache#19424 from cloud-fan/follow.
Could you confirm that |
@rafaelkyrdan limit pushdown has not been supported yet. |
@cloud-fan |
I think I found what I have to implement: |
Usually a data source scans its data incrementally. So when the query has a limit, Spark stops consuming the iterator from data source, and data source won't scan all the data. But limit pushdown does have use cases. For now the only way is to write a catalyst rule, to catch the limit + scan query plan, and convert it to a custom query plan with limit pushed down. |
It seems the (very abandoned) issue for adding LIMIT is https://issues.apache.org/jira/browse/SPARK-22388 This was a nasty surprise when running a df.take(10) on a large postgres dataset, considering limit type pushdowns are implemented in most other connectors. |
What changes were proposed in this pull request?
As we discussed in #19136 (comment) , 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.