wankunde opened a new pull request, #40157:
URL: https://github.com/apache/spark/pull/40157
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR:
https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g.,
'[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a
faster review.
7. If you want to add a new configuration, please read the guideline first
for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the
guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
Add subexpression elimination support into `FilterExec`. It can be
controlled by `spark.sql.subexpressionElimination.enabled` config.
How to do subexpression elimination:
* Get all the sub expressions that appear at least twice
* Iterator all fiter expressions
* If current filter expression contains one sub expression, code gen for
the sub expression and add it to ctx.subExprEliminationExprs to prevent code
gen it again.
* Code gen for the left part of the filter expression.
For example:
Query
```
SELECT * FROM (
SELECT v, v * v + 1 v1 from values(1) as t2(v)
) t
where v > 0 and v1 > 5 and v1 < 10
Codegen plan
```
*(1) Project [v#1, ((v#1 * v#1) + 1) AS v1#0]
+- *(1) Filter (((v#1 > 0) AND (((v#1 * v#1) + 1) > 5)) AND (((v#1 * v#1) +
1) < 10))
+- *(1) LocalTableScan [v#1]
```
the FilterExec `Filter (((v#1 > 0) AND (((v#1 * v#1) + 1) > 5)) AND (((v#1 *
v#1) + 1) < 10))` codegen before this PR:
```java
/* 062 */ // input[0, int, false]
/* 063 */ int localtablescan_value_0 =
localtablescan_row_0.getInt(0);
/* 064 */
/* 065 */ // (input[0, int, false] > 0)
/* 066 */ boolean filter_value_0 = false;
/* 067 */ filter_value_0 = localtablescan_value_0 > 0;
/* 068 */ if (!filter_value_0) continue;
/* 069 */ // (((input[0, int, false] * input[0, int, false]) + 1) >
5)
/* 070 */ int filter_value_5 = -1;
/* 071 */
/* 072 */ filter_value_5 = localtablescan_value_0 *
localtablescan_value_0;
/* 073 */
/* 074 */ int filter_value_4 = -1;
/* 075 */
/* 076 */ filter_value_4 = filter_value_5 + 1;
/* 077 */
/* 078 */ boolean filter_value_3 = false;
/* 079 */ filter_value_3 = filter_value_4 > 5;
/* 080 */ if (!filter_value_3) continue;
/* 081 */ // (((input[0, int, false] * input[0, int, false]) + 1) <
10)
/* 082 */ int filter_value_12 = -1;
/* 083 */
/* 084 */ filter_value_12 = localtablescan_value_0 *
localtablescan_value_0;
/* 085 */
/* 086 */ int filter_value_11 = -1;
/* 087 */
/* 088 */ filter_value_11 = filter_value_12 + 1;
/* 089 */
/* 090 */ boolean filter_value_10 = false;
/* 091 */ filter_value_10 = filter_value_11 < 10;
/* 092 */ if (!filter_value_10) continue;
```
after this PR:
```java
/* 062 */ // input[0, int, false]
/* 063 */ int localtablescan_value_0 =
localtablescan_row_0.getInt(0);
/* 064 */
/* 065 */ // (input[0, int, false] > 0)
/* 066 */ boolean filter_value_0 = false;
/* 067 */ filter_value_0 = localtablescan_value_0 > 0;
/* 068 */ if (!filter_value_0) continue;
/* 069 */ // ((input[0, int, false] * input[0, int, false]) + 1)
/* 070 */ int filter_value_4 = -1;
/* 071 */
/* 072 */ filter_value_4 = localtablescan_value_0 *
localtablescan_value_0;
/* 073 */
/* 074 */ int filter_value_3 = -1;
/* 075 */
/* 076 */ filter_value_3 = filter_value_4 + 1;
/* 077 */
/* 078 */ // (((input[0, int, false] * input[0, int, false]) + 1) >
5)
/* 079 */ boolean filter_value_8 = false;
/* 080 */ filter_value_8 = filter_value_3 > 5;
/* 081 */ if (!filter_value_8) continue;
/* 082 */ // (((input[0, int, false] * input[0, int, false]) + 1) <
10)
/* 083 */ boolean filter_value_10 = false;
/* 084 */ filter_value_10 = filter_value_3 < 10;
/* 085 */ if (!filter_value_10) continue;
```
### Why are the changes needed?
Support subexpression elimination in FilterExec, improve FilterExec
performance.
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Exists UT.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]