GitHub user dongjoon-hyun opened a pull request: https://github.com/apache/spark/pull/12850
[SPARK-15076][SQL] Improve ConstantFolding optimizer by using integral associative property ## What changes were proposed in this pull request? This issue improves `ConstantFolding` optimizer by taking advantage of integral associative property. Currently, `ConstantFolding` works like the following. 1) Can optimize `1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + a` into `45 + a`. 2) Cannot optimize `a + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9`. This issue enables `ConstantFolding` to handle Case 2 for Add/Multiply expression whose data types are `ByteType`, `ShortType`, `IntegerType`, and `LongType`. The followings are the plan comparision between `before` and `after` this issue. **Before** ``` scala> sql("select a+1+2+3+4+5+6+7+8+9 from (select explode(array(1)) a)").explain == Physical Plan == WholeStageCodegen : +- Project [(((((((((a#7 + 1) + 2) + 3) + 4) + 5) + 6) + 7) + 8) + 9) AS (((((((((a + 1) + 2) + 3) + 4) + 5) + 6) + 7) + 8) + 9)#8] : +- INPUT +- Generate explode([1]), false, false, [a#7] +- Scan OneRowRelation[] ``` **After** ``` scala> sql("select a+1+2+3+4+5+6+7+8+9 from (select explode(array(1)) a)").explain == Physical Plan == WholeStageCodegen : +- Project [(a#7 + 45) AS (((((((((a + 1) + 2) + 3) + 4) + 5) + 6) + 7) + 8) + 9)#8] : +- INPUT +- Generate explode([1]), false, false, [a#7] +- Scan OneRowRelation[] ``` ## How was this patch tested? Pass the Jenkins tests including new and updated testcases. You can merge this pull request into a Git repository by running: $ git pull https://github.com/dongjoon-hyun/spark SPARK-15076 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/spark/pull/12850.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #12850 ---- commit 71c3c73ad0f507837539ed58489e63e501bd4c3d Author: Dongjoon Hyun <dongj...@apache.org> Date: 2016-05-02T22:38:31Z [SPARK-15076][SQL] Improve ConstantFolding optimizer by using integral associative property. ---- --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org