From e9167c3f56e04f4283b9c345aa27330b5879c01e Mon Sep 17 00:00:00 2001 From: starocean999 <12095047@qq.com> Date: Fri, 20 Sep 2024 15:32:13 +0800 Subject: [PATCH] [enhancement](nereids)show user friendly error msg when window function contains order by expression --- .../nereids/parser/LogicalPlanBuilder.java | 10 ++++++++- .../ExtractAndNormalizeWindowExpression.java | 6 +++++ .../nereids_syntax_p0/window_function.groovy | 22 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 7d9f36750d5184..07d6f77f6d0f8c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -1225,7 +1225,15 @@ public Expression visitFunctionCall(DorisParser.FunctionCallContext ctx) { List 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 unboundStars = ExpressionUtils.collectAll(params, UnboundStar.class::isInstance); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java index 383ad266511f32..fe69536840096b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ExtractAndNormalizeWindowExpression.java @@ -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; @@ -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()) diff --git a/regression-test/suites/nereids_syntax_p0/window_function.groovy b/regression-test/suites/nereids_syntax_p0/window_function.groovy index 004f47d6f1ce2c..e4c4b2a2aeb891 100644 --- a/regression-test/suites/nereids_syntax_p0/window_function.groovy +++ b/regression-test/suites/nereids_syntax_p0/window_function.groovy @@ -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" + } }