-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32295][SQL] Add not null and size > 0 filters before inner explode/inline to benefit from predicate pushdown #29092
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
8cfa6af
ffe43d7
3fc4b12
c3f9063
9797856
6cc7b15
a25bd6f
3bbff2c
d4089ca
e3a0205
b7a7c49
857c007
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 |
|---|---|---|
|
|
@@ -79,7 +79,6 @@ abstract class Optimizer(catalogManager: CatalogManager) | |
| PushLeftSemiLeftAntiThroughJoin, | ||
| LimitPushDown, | ||
| ColumnPruning, | ||
| InferFiltersFromConstraints, | ||
| // Operator combine | ||
| CollapseRepartition, | ||
| CollapseProject, | ||
|
|
@@ -117,14 +116,13 @@ abstract class Optimizer(catalogManager: CatalogManager) | |
| extendedOperatorOptimizationRules | ||
|
|
||
| val operatorOptimizationBatch: Seq[Batch] = { | ||
| val rulesWithoutInferFiltersFromConstraints = | ||
| operatorOptimizationRuleSet.filterNot(_ == InferFiltersFromConstraints) | ||
| Batch("Operator Optimization before Inferring Filters", fixedPoint, | ||
| rulesWithoutInferFiltersFromConstraints: _*) :: | ||
| operatorOptimizationRuleSet: _*) :: | ||
| Batch("Infer Filters", Once, | ||
| InferFiltersFromGenerate, | ||
| InferFiltersFromConstraints) :: | ||
| Batch("Operator Optimization after Inferring Filters", fixedPoint, | ||
| rulesWithoutInferFiltersFromConstraints: _*) :: | ||
| operatorOptimizationRuleSet: _*) :: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| // Set strategy to Once to avoid pushing filter every time because we do not change the | ||
| // join condition. | ||
| Batch("Push extra predicate through join", fixedPoint, | ||
|
|
@@ -868,6 +866,41 @@ object TransposeWindow extends Rule[LogicalPlan] { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Infers filters from [[Generate]], such that rows that would have been removed | ||
| * by this [[Generate]] can be removed earlier - before joins and in data sources. | ||
| */ | ||
| object InferFiltersFromGenerate extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| // This rule does not infer filters from foldable expressions to avoid constant filters | ||
| // like 'size([1, 2, 3]) > 0'. These do not show up in child's constraints and | ||
| // then the idempotence will break. | ||
| case generate @ Generate(e, _, _, _, _, _) | ||
| if !e.deterministic || e.children.forall(_.foldable) => generate | ||
|
|
||
| case generate @ Generate(g, _, false, _, _, _) if canInferFilters(g) => | ||
| // Exclude child's constraints to guarantee idempotency | ||
| val inferredFilters = ExpressionSet( | ||
| Seq( | ||
| GreaterThan(Size(g.children.head), Literal(0)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be useful to filter rows before join, but can we pushdown
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so... |
||
| IsNotNull(g.children.head) | ||
| ) | ||
| ) -- generate.child.constraints | ||
|
|
||
| if (inferredFilters.nonEmpty) { | ||
| generate.copy(child = Filter(inferredFilters.reduce(And), generate.child)) | ||
| } else { | ||
| generate | ||
| } | ||
| } | ||
|
|
||
| private def canInferFilters(g: Generator): Boolean = g match { | ||
| case _: ExplodeBase => true | ||
| case _: Inline => true | ||
| case _ => false | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate a list of additional filters from an operator's existing constraint but remove those | ||
| * that are either already part of the operator's condition or are part of the operator's child | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.catalyst.optimizer | ||
|
|
||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.types.{IntegerType, StructField, StructType} | ||
|
|
||
| class InferFiltersFromGenerateSuite extends PlanTest { | ||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = Batch("Infer Filters", Once, InferFiltersFromGenerate) :: Nil | ||
| } | ||
|
|
||
| val testRelation = LocalRelation('a.array(StructType(Seq( | ||
| StructField("x", IntegerType), | ||
| StructField("y", IntegerType) | ||
| )))) | ||
|
|
||
| Seq(Explode(_), PosExplode(_), Inline(_)).foreach { f => | ||
| val generator = f('a) | ||
| test("Infer filters from " + generator) { | ||
| val originalQuery = testRelation.generate(generator).analyze | ||
| val correctAnswer = testRelation | ||
| .where(IsNotNull('a) && Size('a) > 0) | ||
tanelk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .generate(generator) | ||
| .analyze | ||
| val optimized = Optimize.execute(originalQuery) | ||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("Don't infer duplicate filters from " + generator) { | ||
| val originalQuery = testRelation | ||
| .where(IsNotNull('a) && Size('a) > 0) | ||
| .generate(generator) | ||
| .analyze | ||
| val optimized = Optimize.execute(originalQuery) | ||
| comparePlans(optimized, originalQuery) | ||
| } | ||
|
|
||
| test("Don't infer filters from outer " + generator) { | ||
| val originalQuery = testRelation.generate(generator, outer = true).analyze | ||
| val optimized = Optimize.execute(originalQuery) | ||
| comparePlans(optimized, originalQuery) | ||
| } | ||
|
|
||
| val foldableExplode = f(CreateArray(Seq( | ||
| CreateStruct(Seq(Literal(0), Literal(1))), | ||
| CreateStruct(Seq(Literal(2), Literal(3))) | ||
| ))) | ||
| test("Don't infer filters from " + foldableExplode) { | ||
| val originalQuery = testRelation.generate(foldableExplode).analyze | ||
| val optimized = Optimize.execute(originalQuery) | ||
| comparePlans(optimized, originalQuery) | ||
| } | ||
| } | ||
| } | ||
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 did you put this rule only in this batch? https://github.com/apache/spark/pull/29092/files#diff-a636a87d8843eeccca90140be91d4fafR82
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'm sorry, I don't get the question.
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.
Ah, ok. Looks okay as it is. Since
operatorOptimizationRuleSethasInferFiltersFromConstraints, I thought we'd better to putInferFiltersFromConstraints/InferFiltersFromGeneratethere together.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.
Actually the
InferFiltersFromConstraintsis allways filtered out from theoperatorOptimizationRuleSet:spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
Lines 120 to 123 in 5ce321d
I think it is very misleading , perhaps we could remove it from there and avoid further confusion?
It was asked in the original PR, put looks like it was forgotten:
https://github.com/apache/spark/pull/19149/files#r157777935
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, but I also don't know why. cc: @cloud-fan
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 don't quite remember, maybe we can remove it.
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 went ahead and removed it from the
operatorOptimizationRuleSet. All the tests are still passing as was expected.