-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-27033][SQL]Add Optimize rule RewriteArithmeticFiltersOnIntegralColumn #23942
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 6 commits
45b6f58
aaf2b8a
82ff2a1
597d6d7
03c522e
0f61953
5d0a5e8
3927dec
2c02777
43892ac
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 |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * 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.{Filter, LogicalPlan} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.types.{DataType, IntegerType, LongType} | ||
|
|
||
| /** | ||
| * Rewrite arithmetic filters on int or long column to its equivalent form, | ||
| * leaving attribute alone in one side, so that we can push it down to | ||
| * parquet or other file format. | ||
| * For example, this rule can optimize | ||
| * {{{ | ||
| * SELECT * FROM table WHERE i + 3 = 5 | ||
| * }}} | ||
| * to | ||
| * {{{ | ||
| * SELECT * FROM table WHERE i = 5 - 3 | ||
| * }}} | ||
| * when i is Int or Long, and then other rules will further optimize it to | ||
| * {{{ | ||
| * SELECT * FROM table WHERE i = 2 | ||
| * }}} | ||
| * The arithmetic operation supports `Add` and `Subtract`. The comparision supports | ||
| * '=', '>=', '<=', '>', '<', '!='. It only supports type of `INT` and `LONG`, | ||
| * it doesn't support `FLOAT` or `DOUBLE` for precision issues. | ||
| */ | ||
|
||
| object RewriteArithmeticFiltersOnIntOrLongColumn extends Rule[LogicalPlan] with PredicateHelper { | ||
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| case f: Filter => | ||
| f transformExpressionsUp { | ||
| case e @ BinaryComparison(left: BinaryArithmetic, right: Literal) | ||
|
||
| if isDataTypeSafe(left.dataType) => | ||
| transformLeft(e, left, right) | ||
| case e @ BinaryComparison(left: Literal, right: BinaryArithmetic) | ||
| if isDataTypeSafe(right.dataType) => | ||
| transformRight(e, left, right) | ||
| } | ||
| } | ||
|
|
||
| private def transformLeft( | ||
| bc: BinaryComparison, | ||
| left: BinaryArithmetic, | ||
| right: Literal): Expression = { | ||
| left match { | ||
| case Add(ar: AttributeReference, lit: Literal) if isOptSafe(Subtract(right, lit)) => | ||
| bc.makeCopy(Array(ar, Subtract(right, lit))) | ||
| case Add(lit: Literal, ar: AttributeReference) if isOptSafe(Subtract(right, lit)) => | ||
| bc.makeCopy(Array(ar, Subtract(right, lit))) | ||
| case Subtract(ar: AttributeReference, lit: Literal) if isOptSafe(Add(right, lit)) => | ||
| bc.makeCopy(Array(ar, Add(right, lit))) | ||
| case Subtract(lit: Literal, ar: AttributeReference) if isOptSafe(Subtract(lit, right)) => | ||
| bc.makeCopy(Array(Subtract(lit, right), ar)) | ||
| case _ => bc | ||
| } | ||
| } | ||
|
|
||
| private def transformRight( | ||
| bc: BinaryComparison, | ||
| left: Literal, | ||
| right: BinaryArithmetic): Expression = { | ||
| right match { | ||
| case Add(ar: AttributeReference, lit: Literal) if isOptSafe(Subtract(left, lit)) => | ||
| bc.makeCopy(Array(Subtract(left, lit), ar)) | ||
| case Add(lit: Literal, ar: AttributeReference) if isOptSafe(Subtract(left, lit)) => | ||
| bc.makeCopy(Array(Subtract(left, lit), ar)) | ||
| case Subtract(ar: AttributeReference, lit: Literal) if isOptSafe(Add(left, lit)) => | ||
| bc.makeCopy(Array(Add(left, lit), ar)) | ||
| case Subtract(lit: Literal, ar: AttributeReference) if isOptSafe(Subtract(lit, left)) => | ||
| bc.makeCopy(Array(ar, Subtract(lit, left))) | ||
| case _ => bc | ||
| } | ||
| } | ||
|
|
||
| private def isDataTypeSafe(dataType: DataType): Boolean = dataType match { | ||
|
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. why only integer and longs are accepted?
Contributor
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.
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. How about the other integral types, e.g., short?
Contributor
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. yes, integral types(byte, short, int, long) are all ok. I'll add support for byte and short type as well.
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 think the precision issue would be there anyway, when executed at runtime, am I wrong?
Contributor
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.
I have a simple test on a table with a Double type column with
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. Here is an example: Although
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. thanks for the explanation |
||
| case IntegerType | LongType => true | ||
| case _ => false | ||
| } | ||
|
|
||
| private def isOptSafe(e: BinaryArithmetic): Boolean = { | ||
| val leftVal = e.left.eval(EmptyRow) | ||
| val rightVal = e.right.eval(EmptyRow) | ||
|
|
||
| e match { | ||
| case Add(_: Literal, _: Literal) => | ||
| e.dataType match { | ||
| case IntegerType => | ||
| isAddSafe(leftVal, rightVal, Int.MinValue, Int.MaxValue) | ||
| case LongType => | ||
| isAddSafe(leftVal, rightVal, Long.MinValue, Long.MaxValue) | ||
| case _ => false | ||
| } | ||
|
|
||
| case Subtract(_: Literal, _: Literal) => | ||
| e.dataType match { | ||
| case IntegerType => | ||
| isSubtractSafe(leftVal, rightVal, Int.MinValue, Int.MaxValue) | ||
| case LongType => | ||
| isSubtractSafe(leftVal, rightVal, Long.MinValue, Long.MaxValue) | ||
| case _ => false | ||
| } | ||
|
|
||
| case _ => false | ||
| } | ||
| } | ||
|
|
||
| private def isAddSafe[T](left: Any, right: Any, minValue: T, maxValue: T)( | ||
| implicit num: Numeric[T]): Boolean = { | ||
| import num._ | ||
| val leftVal = left.asInstanceOf[T] | ||
| val rightVal = right.asInstanceOf[T] | ||
| if (rightVal > zero) { | ||
| leftVal <= maxValue - rightVal | ||
| } else { | ||
| leftVal >= minValue - rightVal | ||
| } | ||
| } | ||
|
|
||
| private def isSubtractSafe[T](left: Any, right: Any, minValue: T, maxValue: T)( | ||
| implicit num: Numeric[T]): Boolean = { | ||
| import num._ | ||
| val leftVal = left.asInstanceOf[T] | ||
| val rightVal = right.asInstanceOf[T] | ||
| if (rightVal > zero) { | ||
| leftVal >= minValue + rightVal | ||
| } else { | ||
| leftVal <= maxValue + rightVal | ||
| } | ||
| } | ||
| } | ||

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.
nit: how about 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.
It's more clearly. I've updated it.