-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-21222] Move elimination of Distinct clause from analyzer to optimizer #18429
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
f60c184
7604811
892f50a
2f89499
19163d4
fd3c849
9fb9779
08f61f4
536aae2
5a3df30
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 |
---|---|---|
|
@@ -40,6 +40,7 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: SQLConf) | |
protected val fixedPoint = FixedPoint(conf.optimizerMaxIterations) | ||
|
||
def batches: Seq[Batch] = { | ||
Batch("Eliminate Distinct", Once, EliminateDistinct) :: | ||
// Technically some of the rules in Finish Analysis are not optimizer rules and belong more | ||
// in the analyzer, because they are needed for correctness (e.g. ComputeCurrentTime). | ||
// However, because we also use the analyzer to canonicalized queries (for view definition), | ||
|
@@ -151,6 +152,20 @@ abstract class Optimizer(sessionCatalog: SessionCatalog, conf: SQLConf) | |
def extendedOperatorOptimizationRules: Seq[Rule[LogicalPlan]] = Nil | ||
} | ||
|
||
/** | ||
* Remove useless DISTINCT for MAX and MIN. | ||
* This rule should be applied before RewriteDistinctAggregates. | ||
*/ | ||
object EliminateDistinct extends Rule[LogicalPlan] { | ||
override def apply(plan: LogicalPlan): LogicalPlan = plan transformExpressions { | ||
case ae: AggregateExpression if ae.isDistinct => | ||
ae.aggregateFunction match { | ||
case _: Max | _: Min => ae.copy(isDistinct = false) | ||
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. actually i made a mistake earlier. you'd need to do a match on other cases and return the ae itself too 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. My fault... |
||
case _ => ae | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* An optimizer used in test code. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Expand, LocalRelation, LogicalPlan} | ||
import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
|
||
class EliminateDistinctSuite extends PlanTest { | ||
|
||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = | ||
Batch("Operator Optimizations", Once, | ||
EliminateDistinct) :: Nil | ||
} | ||
|
||
val testRelation = LocalRelation('a.int) | ||
|
||
test("Eliminate Distinct in Max") { | ||
val query = testRelation | ||
.select(maxDistinct('a).as('result)) | ||
.analyze | ||
val answer = testRelation | ||
.select(max('a).as('result)) | ||
.analyze | ||
assert(query != answer) | ||
comparePlans(Optimize.execute(query), answer) | ||
} | ||
|
||
test("Eliminate Distinct in Min") { | ||
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. actually you can put them in one test:
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. well, I prefer to make it simple |
||
val query = testRelation | ||
.select(minDistinct('a).as('result)) | ||
.analyze | ||
val answer = testRelation | ||
.select(min('a).as('result)) | ||
.analyze | ||
assert(query != answer) | ||
comparePlans(Optimize.execute(query), answer) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
hmm, does it have to be executed before the "Finish Analysis" batch?
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.
We can move this into the next batch, but it has to be before
RewriteDistinctAggregates
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.
RewriteDistinctAggregates is inside the "Finish Analysis" batch, so the new rule has to be placed before it.