Skip to content

Commit

Permalink
[enhancement](nereids)show user friendly error msg when window functi…
Browse files Browse the repository at this point in the history
…on contains order by expression
  • Loading branch information
starocean999 committed Sep 20, 2024
1 parent 70e8224 commit e9167c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,15 @@ public Expression visitFunctionCall(DorisParser.FunctionCallContext ctx) {

List<OrderKey> orderKeys = visit(ctx.sortItem(), OrderKey.class);
if (!orderKeys.isEmpty()) {
return parseFunctionWithOrderKeys(functionName, isDistinct, params, orderKeys, ctx);
Expression expression = parseFunctionWithOrderKeys(functionName, isDistinct, params, orderKeys, ctx);
if (ctx.windowSpec() != null) {
if (isDistinct) {
throw new ParseException("DISTINCT not allowed in analytic function: " + functionName, ctx);
}
return withWindowSpec(ctx.windowSpec(), expression);
} else {
return expression;
}
}

List<UnboundStar> unboundStars = ExpressionUtils.collectAll(params, UnboundStar.class::isInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.rewrite.NormalizeToSlot.NormalizeToSlotContext;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.NamedExpression;
import org.apache.doris.nereids.trees.expressions.OrderExpression;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.WindowExpression;
import org.apache.doris.nereids.trees.plans.Plan;
Expand Down Expand Up @@ -52,6 +54,10 @@ public Rule build() {
if (output instanceof WindowExpression) {
// remove literal partition by and order by keys
WindowExpression windowExpression = (WindowExpression) output;
Expression function = windowExpression.getFunction();
if (function.containsType(OrderExpression.class)) {
throw new AnalysisException("order by is not supported in " + function);
}
return windowExpression.withPartitionKeysOrderKeys(
windowExpression.getPartitionKeys().stream()
.filter(expression -> !expression.isConstant())
Expand Down
22 changes: 22 additions & 0 deletions regression-test/suites/nereids_syntax_p0/window_function.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,26 @@ suite("window_function") {

qt_select_lead "SELECT c3,c2,c1,lead(c1, 0, 111) over(partition by c3 order by c2,c1) FROM window_test order by c3;"
qt_select_lag "SELECT c3,c2,c1,lag(c1, 0, 222) over(partition by c3 order by c2,c1) FROM window_test order by c3;"

sql "drop table if exists test_normalize_window"
sql """CREATE TABLE test_normalize_window (
`xwho` varchar(50) NULL COMMENT 'xwho',
`xwhen` datetime COMMENT 'xwhen',
`xwhat` int NULL COMMENT 'xwhat'
)
DUPLICATE KEY(xwho)
DISTRIBUTED BY HASH(xwho) BUCKETS 3
PROPERTIES (
"replication_num" = "1"
);"""

sql """INSERT into test_normalize_window (xwho, xwhen, xwhat) values ('1', '2022-03-12 10:41:00', 1),
('1', '2022-03-12 13:28:02', 2),
('1', '2022-03-12 16:15:01', 3),
('1', '2022-03-12 19:05:04', 4);"""

test {
sql "select group_concat(xwho order by xwhat) over(partition by xwhen) from test_normalize_window;"
exception "order by is not supported"
}
}

0 comments on commit e9167c3

Please sign in to comment.