-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-2052] [SQL] Add optimization for CaseConversionExpression's. #990
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
3977d80
0ff7568
23e2363
c4eea67
dde7ede
2568666
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 |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ import org.apache.spark.sql.catalyst.analysis.EliminateAnalysisOperators | |
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
| import org.apache.spark.sql.catalyst.types.{DoubleType, IntegerType} | ||
| import org.apache.spark.sql.catalyst.types._ | ||
|
|
||
| // For implicit conversions | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
|
|
@@ -173,4 +173,63 @@ class ConstantFoldingSuite extends OptimizerTest { | |
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("Constant folding test: expressions have null literals") { | ||
| val originalQuery = | ||
| testRelation | ||
| .select( | ||
| IsNull(Literal(null)) as 'c1, | ||
| IsNotNull(Literal(null)) as 'c2, | ||
|
|
||
| GetItem(Literal(null, ArrayType(IntegerType)), 1) as 'c3, | ||
|
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. This is only foldable because both values are literals. if you make one of them an attribute (e.g.,
Member
Author
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. Ah, I see, that's right! |
||
| GetItem(Literal(Seq(1), ArrayType(IntegerType)), Literal(null, IntegerType)) as 'c4, | ||
| GetField( | ||
| Literal(null, StructType(Seq(StructField("a", IntegerType, true)))), | ||
| "a") as 'c5, | ||
|
|
||
| UnaryMinus(Literal(null, IntegerType)) as 'c6, | ||
| Cast(Literal(null), IntegerType) as 'c7, | ||
| Not(Literal(null, BooleanType)) as 'c8, | ||
|
|
||
| Add(Literal(null, IntegerType), 1) as 'c9, | ||
|
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. Same here and all the expression with more than one child. |
||
| Add(1, Literal(null, IntegerType)) as 'c10, | ||
|
|
||
| Equals(Literal(null, IntegerType), 1) as 'c11, | ||
| Equals(1, Literal(null, IntegerType)) as 'c12, | ||
|
|
||
| Like(Literal(null, StringType), "abc") as 'c13, | ||
| Like("abc", Literal(null, StringType)) as 'c14, | ||
|
|
||
| Upper(Literal(null, StringType)) as 'c15) | ||
|
|
||
| val optimized = Optimize(originalQuery.analyze) | ||
|
|
||
| val correctAnswer = | ||
| testRelation | ||
| .select( | ||
| Literal(true) as 'c1, | ||
| Literal(false) as 'c2, | ||
|
|
||
| Literal(null, IntegerType) as 'c3, | ||
| Literal(null, IntegerType) as 'c4, | ||
| Literal(null, IntegerType) as 'c5, | ||
|
|
||
| Literal(null, IntegerType) as 'c6, | ||
| Literal(null, IntegerType) as 'c7, | ||
| Literal(null, BooleanType) as 'c8, | ||
|
|
||
| Literal(null, IntegerType) as 'c9, | ||
| Literal(null, IntegerType) as 'c10, | ||
|
|
||
| Literal(null, BooleanType) as 'c11, | ||
| Literal(null, BooleanType) as 'c12, | ||
|
|
||
| Literal(null, BooleanType) as 'c13, | ||
| Literal(null, BooleanType) as 'c14, | ||
|
|
||
| Literal(null, StringType) as 'c15) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.expressions._ | ||
| import org.apache.spark.sql.catalyst.plans.logical._ | ||
| import org.apache.spark.sql.catalyst.rules._ | ||
|
|
||
| /* Implicit conversions */ | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
|
|
||
| class SimplifyCaseConversionExpressionsSuite extends OptimizerTest { | ||
|
|
||
| object Optimize extends RuleExecutor[LogicalPlan] { | ||
| val batches = | ||
| Batch("Simplify CaseConversionExpressions", Once, | ||
| SimplifyCaseConversionExpressions) :: Nil | ||
| } | ||
|
|
||
| val testRelation = LocalRelation('a.string) | ||
|
|
||
| test("simplify UPPER(UPPER(str))") { | ||
| val originalQuery = | ||
| testRelation | ||
| .select(Upper(Upper('a)) as 'u) | ||
|
|
||
| val optimized = Optimize(originalQuery.analyze) | ||
| val correctAnswer = | ||
| testRelation | ||
| .select(Upper('a) as 'u) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("simplify UPPER(LOWER(str))") { | ||
| val originalQuery = | ||
| testRelation | ||
| .select(Upper(Lower('a)) as 'u) | ||
|
|
||
| val optimized = Optimize(originalQuery.analyze) | ||
| val correctAnswer = | ||
| testRelation | ||
| .select(Upper('a) as 'u) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("simplify LOWER(UPPER(str))") { | ||
| val originalQuery = | ||
| testRelation | ||
| .select(Lower(Upper('a)) as 'l) | ||
|
|
||
| val optimized = Optimize(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Lower('a) as 'l) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
|
|
||
| test("simplify LOWER(LOWER(str))") { | ||
| val originalQuery = | ||
| testRelation | ||
| .select(Lower(Lower('a)) as 'l) | ||
|
|
||
| val optimized = Optimize(originalQuery.analyze) | ||
| val correctAnswer = testRelation | ||
| .select(Lower('a) as 'l) | ||
| .analyze | ||
|
|
||
| comparePlans(optimized, correctAnswer) | ||
| } | ||
| } |
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.
Just curious - do you have a use case for this rule? I wonder how often it happens in practice.
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.
Hi,
I just cared that the conversions were used redundantly like
UPPER(LOWER(str)), which would be a mistake in most case, though.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.
Do you actually have queries that trigger this?
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.
Just use
UPPER(LOWER(str))orUPPER(UPPER(str)), or something like those.