You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/11/04 14:57:43 UTC

[GitHub] [spark] cloud-fan opened a new pull request, #38511: [WIP][SPARK-41017][SQL] Do not push Filter through reference-only Project

cloud-fan opened a new pull request, #38511:
URL: https://github.com/apache/spark/pull/38511

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] sigmod commented on pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
sigmod commented on PR #38511:
URL: https://github.com/apache/spark/pull/38511#issuecomment-1311080859

   cc @rkkorlapati-db


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023927228


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -85,15 +72,25 @@ object PhysicalOperation extends AliasHelper with PredicateHelper {
         // projects. We need to meet the following conditions to do so:
         //   1) no Project collected so far or the collected Projects are all deterministic
         //   2) the collected filters and this filter are all deterministic, or this is the
-        //      first collected filter.
+        //      first collected filter. This condition can be relaxed if `canKeepMultipleFilters` is
+        //      true.
         //   3) this filter does not repeat any expensive expressions from the collected
         //      projects.
-        val canIncludeThisFilter = fields.forall(_.forall(_.deterministic)) && {
-          filters.isEmpty || (filters.forall(_.deterministic) && condition.deterministic)
-        } && canCollapseExpressions(Seq(condition), aliases, alwaysInline)
-        if (canIncludeThisFilter) {
-          val replaced = replaceAlias(condition, aliases)
-          (fields, filters ++ splitConjunctivePredicates(replaced), other, aliases)
+        val canPushFilterThroughProject = fields.forall(_.forall(_.deterministic)) &&
+          canCollapseExpressions(Seq(condition), aliases, alwaysInline)
+        if (canPushFilterThroughProject) {
+          val canIncludeThisFilter = filters.isEmpty || {
+            filters.length == 1 && filters.head.forall(_.deterministic) && condition.deterministic
+          }

Review Comment:
   This is the core change of this PR. `PhysicalOperation` returns a single filter condition, which means it combines filters, and we have to make sure all the filters are deterministic. `ScanOperation` returns multiple filter conditions and does not have this restriction.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #38511:
URL: https://github.com/apache/spark/pull/38511#issuecomment-1317241669

   pushed a refactor to make the code easier to understand, please take another look, thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] viirya commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023726173


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/SchemaPruning.scala:
##########
@@ -138,18 +132,18 @@ object SchemaPruning extends Rule[LogicalPlan] {
   private def buildNewProjection(
       projects: Seq[NamedExpression],
       normalizedProjects: Seq[NamedExpression],
-      filters: Seq[Expression],
+      filters: Seq[Seq[Expression]],
       leafNode: LeafNode,
       projectionOverSchema: ProjectionOverSchema): Project = {
     // Construct a new target for our projection by rewriting and
     // including the original filters where available
     val projectionChild =
       if (filters.nonEmpty) {
-        val projectedFilters = filters.map(_.transformDown {
+        val projectedFilters = filters.map(_.map(_.transformDown {
           case projectionOverSchema(expr) => expr
-        })
-        val newFilterCondition = projectedFilters.reduce(And)
-        Filter(newFilterCondition, leafNode)
+        }))
+        val newFilterConditions = projectedFilters.map(_.reduce(And))
+        newFilterConditions.foldRight[LogicalPlan](leafNode)((cond, plan) => Filter(cond, plan))

Review Comment:
   Hmm, is that same as before?
   
   This constructs a new `Filter` with projected predicates (reduced by `And`). But this change reduces all projected predicates from all adjoining `Filter`s which can be non-deterministic?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023204106


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -85,15 +72,25 @@ object PhysicalOperation extends AliasHelper with PredicateHelper {
         // projects. We need to meet the following conditions to do so:
         //   1) no Project collected so far or the collected Projects are all deterministic
         //   2) the collected filters and this filter are all deterministic, or this is the
-        //      first collected filter.
+        //      first collected filter. This condition can be relaxed if `canKeepMultipleFilters` is

Review Comment:
   TBH, the comment here is hard to understand..



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #38511:
URL: https://github.com/apache/spark/pull/38511#issuecomment-1310574974

   also cc @wangyum @ulysses-you 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023935711


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala:
##########
@@ -146,8 +146,12 @@ object FileSourceStrategy extends Strategy with PredicateHelper with Logging {
   }
 
   def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
-    case PhysicalOperation(projects, filters,
+    case ScanOperation(projects, allFilters,
       l @ LogicalRelation(fsRelation: HadoopFsRelation, _, table, _)) =>
+      // We can only push down the bottom-most filter to the relation, as `ScanOperation` decided to
+      // not merge these filters and we need to keep their evaluation order.
+      val filters = allFilters.lastOption.getOrElse(Nil)

Review Comment:
   This is the only place that does column pruning and filter pushdown together. Making `ScanOperation` return both `allFilters` and `pushdownFilters` will make the code more verbose in other places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023241331


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -29,26 +29,13 @@ import org.apache.spark.sql.errors.QueryCompilationErrors
 import org.apache.spark.sql.execution.datasources.v2.{DataSourceV2Relation, DataSourceV2ScanRelation}
 import org.apache.spark.sql.internal.SQLConf
 
-/**
- * A pattern that matches any number of project or filter operations even if they are
- * non-deterministic, as long as they satisfy the requirement of CollapseProject and CombineFilters.
- * All filter operators are collected and their conditions are broken up and returned
- * together with the top project operator. [[Alias Aliases]] are in-lined/substituted if
- * necessary.
- */
-object PhysicalOperation extends AliasHelper with PredicateHelper {
+trait OperationHelper extends AliasHelper with PredicateHelper {
   import org.apache.spark.sql.catalyst.optimizer.CollapseProject.canCollapseExpressions
 
-  type ReturnType =
-    (Seq[NamedExpression], Seq[Expression], LogicalPlan)
   type IntermediateType =
-    (Option[Seq[NamedExpression]], Seq[Expression], LogicalPlan, AttributeMap[Alias])
+    (Option[Seq[NamedExpression]], Seq[Seq[Expression]], LogicalPlan, AttributeMap[Alias])
 
-  def unapply(plan: LogicalPlan): Option[ReturnType] = {
-    val alwaysInline = SQLConf.get.getConf(SQLConf.COLLAPSE_PROJECT_ALWAYS_INLINE)
-    val (fields, filters, child, _) = collectProjectsAndFilters(plan, alwaysInline)
-    Some((fields.getOrElse(child.output), filters, child))
-  }
+  protected def canKeepMultipleFilters: Boolean

Review Comment:
   Nit: add a simple comment



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] wangyum closed pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
wangyum closed pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters
URL: https://github.com/apache/spark/pull/38511


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #38511:
URL: https://github.com/apache/spark/pull/38511#issuecomment-1308318576

   cc @viirya @sigmod @hvanhovell 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] wangyum commented on pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
wangyum commented on PR #38511:
URL: https://github.com/apache/spark/pull/38511#issuecomment-1318162616

   Merged to master.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023241088


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala:
##########
@@ -146,8 +146,12 @@ object FileSourceStrategy extends Strategy with PredicateHelper with Logging {
   }
 
   def apply(plan: LogicalPlan): Seq[SparkPlan] = plan match {
-    case PhysicalOperation(projects, filters,
+    case ScanOperation(projects, allFilters,
       l @ LogicalRelation(fsRelation: HadoopFsRelation, _, table, _)) =>
+      // We can only push down the bottom-most filter to the relation, as `ScanOperation` decided to
+      // not merge these filters and we need to keep their evaluation order.
+      val filters = allFilters.lastOption.getOrElse(Nil)

Review Comment:
   So for filter pushdown, we will use the last filter. For schema pruning, we will use all the filters. 
   I wonder if we should return both `allFilters` and `pushdownFilters` to make the syntax clear.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] viirya commented on a diff in pull request #38511: [SPARK-41017][SQL] Support column pruning with multiple nondeterministic Filters

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #38511:
URL: https://github.com/apache/spark/pull/38511#discussion_r1023711232


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/planning/patterns.scala:
##########
@@ -85,15 +72,25 @@ object PhysicalOperation extends AliasHelper with PredicateHelper {
         // projects. We need to meet the following conditions to do so:
         //   1) no Project collected so far or the collected Projects are all deterministic
         //   2) the collected filters and this filter are all deterministic, or this is the
-        //      first collected filter.
+        //      first collected filter. This condition can be relaxed if `canKeepMultipleFilters` is
+        //      true.
         //   3) this filter does not repeat any expensive expressions from the collected
         //      projects.
-        val canIncludeThisFilter = fields.forall(_.forall(_.deterministic)) && {
-          filters.isEmpty || (filters.forall(_.deterministic) && condition.deterministic)
-        } && canCollapseExpressions(Seq(condition), aliases, alwaysInline)
-        if (canIncludeThisFilter) {
-          val replaced = replaceAlias(condition, aliases)
-          (fields, filters ++ splitConjunctivePredicates(replaced), other, aliases)
+        val canPushFilterThroughProject = fields.forall(_.forall(_.deterministic)) &&
+          canCollapseExpressions(Seq(condition), aliases, alwaysInline)
+        if (canPushFilterThroughProject) {
+          val canIncludeThisFilter = filters.isEmpty || {
+            filters.length == 1 && filters.head.forall(_.deterministic) && condition.deterministic
+          }

Review Comment:
   Previously, this is `filters.forall(_.deterministic)`, why it is relaxed here too? I think it is not under `canKeepMultipleFilters` condition below.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org