Skip to content

Commit

Permalink
[feature](Nereids): eliminate distinct for max/min/any_value (apache#…
Browse files Browse the repository at this point in the history
…23428)

eliminate distinct for max/min/any_value function
```
max(distinct value) = max(value)
```
  • Loading branch information
keanji-x authored Aug 30, 2023
1 parent a136836 commit ade598e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public AnyValue(Expression arg) {
* constructor with 1 argument.
*/
public AnyValue(boolean distinct, Expression arg) {
super("any_value", distinct, arg);
super("any_value", false, arg);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Max(boolean distinct, Expression arg) {
}

private Max(boolean distinct, boolean alwaysNullable, Expression arg) {
super("max", distinct, alwaysNullable, arg);
super("max", false, alwaysNullable, arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Min(boolean distinct, Expression arg) {
}

private Min(boolean distinct, boolean alwaysNullable, Expression arg) {
super("min", distinct, alwaysNullable, arg);
super("min", false, alwaysNullable, arg);
}

@Override
Expand Down
11 changes: 11 additions & 0 deletions regression-test/suites/nereids_p0/aggregate/aggregate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,15 @@ suite("aggregate") {

sql "select k1 as k, k1 from tempbaseall group by k1 having k1 > 0"
sql "select k1 as k, k1 from tempbaseall group by k1 having k > 0"

// remove distinct for max, min, any_value
def plan = sql(
"""explain optimized plan SELECT max(distinct c_bigint),
min(distinct c_bigint),
any_value(distinct c_bigint)
FROM regression_test_nereids_p0_aggregate.${tableName};"""
).toString()
assertTrue(plan.contains("max(c_bigint"))
assertTrue(plan.contains("min(c_bigint"))
assertTrue(plan.contains("any_value(c_bigint"))
}
4 changes: 2 additions & 2 deletions regression-test/suites/nereids_syntax_p0/analyze_agg.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ suite("analyze_agg") {
"""

test {
sql "select count(distinct t2.id), max(distinct t2.c) from t2"
exception "max(DISTINCT c#2) can't support multi distinct."
sql "select count(distinct t2.b), variance(distinct t2.c) from t2"
exception "variance(DISTINCT c#2) can't support multi distinct."
}
}

0 comments on commit ade598e

Please sign in to comment.