Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

cloud-fan
Copy link
Contributor

@cloud-fan cloud-fan commented Oct 4, 2017

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.

@cloud-fan
Copy link
Contributor Author

cc @rdblue @gatorsmile

@SparkQA
Copy link

SparkQA commented Oct 4, 2017

Test build #82444 has finished for PR 19424 at commit 75457f6.

  • This patch fails due to an unknown error code, -9.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Oct 4, 2017

Test build #82447 has finished for PR 19424 at commit 75457f6.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

* 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.
Copy link
Member

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(
Copy link
Member

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 = {
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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?

@SparkQA
Copy link

SparkQA commented Oct 5, 2017

Test build #82461 has finished for PR 19424 at commit a9f2c57.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Oct 5, 2017

Test build #82464 has finished for PR 19424 at commit df1d1c5.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan cloud-fan force-pushed the follow branch 2 times, most recently from e880328 to 24f1a75 Compare October 6, 2017 14:57
@SparkQA
Copy link

SparkQA commented Oct 6, 2017

Test build #82513 has finished for PR 19424 at commit e880328.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
  • trait DataSourceReaderHolder

@SparkQA
Copy link

SparkQA commented Oct 6, 2017

Test build #82514 has finished for PR 19424 at commit 24f1a75.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
  • trait DataSourceReaderHolder

@cloud-fan
Copy link
Contributor Author

retest this please

@SparkQA
Copy link

SparkQA commented Oct 9, 2017

Test build #82547 has finished for PR 19424 at commit 24f1a75.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
  • trait DataSourceReaderHolder

Copy link
Contributor

@rdblue rdblue left a 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)
Copy link
Contributor

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)
Copy link
Contributor

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.
Copy link
Contributor

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))
Copy link
Contributor

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?

@rdblue
Copy link
Contributor

rdblue commented Oct 10, 2017

What are the guarantees made by the previous batches in the optimizer? The work done by FilterAndProject seems redundant to me because the optimizer should already push filters below projection. Is that not guaranteed by the time this runs?

@SparkQA
Copy link

SparkQA commented Oct 10, 2017

Test build #82586 has finished for PR 19424 at commit e8e8fee.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds the following public classes (experimental):
  • trait DataSourceReaderHolder

@cloud-fan
Copy link
Contributor Author

@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 ColumnPruning rule tries to generate a new Project node under every node that has more input columns than it references, which means it will also generate a Project under Filter, and this conflicts with PushDownPredicate rule. Currently the conflict is resolved by a hacky way so there are cases Project under Filter.

import org.apache.spark.sql.sources.v2.reader._

/**
* A base class for data source reader holder and defines equals/hashCode methods.
Copy link
Member

@gatorsmile gatorsmile Oct 11, 2017

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
Copy link
Member

@gatorsmile gatorsmile Oct 11, 2017

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

@gatorsmile
Copy link
Member

I think the current PR is good enough to merge. Later, we still continue improving the Data source API v2.

LGTM pending Jenkins.

@SparkQA
Copy link

SparkQA commented Oct 12, 2017

Test build #82662 has finished for PR 19424 at commit 98dbd70.

  • This patch fails to build.
  • This patch merges cleanly.
  • This patch adds no public classes.

}

lazy val output: Seq[Attribute] = reader.readSchema().map(_.name).map { name =>
fullOutput.find(_.name == name).get
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Member

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?

Copy link
Contributor Author

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.

@viirya
Copy link
Member

viirya commented Oct 12, 2017

late LGTM with minor comments.

@SparkQA
Copy link

SparkQA commented Oct 12, 2017

Test build #82665 has finished for PR 19424 at commit 200cd20.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@cloud-fan
Copy link
Contributor Author

thanks for review, merging to master!

@asfgit asfgit closed this in 73d80ec Oct 12, 2017
jzhuge pushed a commit to jzhuge/spark that referenced this pull request Aug 20, 2018
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.
@rafaelkyrdan
Copy link

rafaelkyrdan commented Oct 9, 2019

Could you confirm that limit now optimized and pushed to PushedFilters? In the physical plan I see that it is not. I'm using spark 2.4.4

@cloud-fan
Copy link
Contributor Author

@rafaelkyrdan limit pushdown has not been supported yet.

@rafaelkyrdan
Copy link

@cloud-fan
I'm wondering whether it is not a priority or it wasn't easy to implement?
If I want to implement it what class/interface should I extend?
Is it possible to do it in a custom JDBC source?
Do you have any suggestions?
The use case looks like this: there are 50M records in the DB I wanna select only 1M why I should read all of them?

@rafaelkyrdan
Copy link

I think I found what I have to implement:
trait TableScan { def buildScan(): RDD[Row] }
am I right?

@cloud-fan
Copy link
Contributor Author

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.

@lokkju
Copy link

lokkju commented Jan 29, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants