This is an automated email from the ASF dual-hosted git repository.
starocean999 pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new 97c2e7ffcfa [enhancement](nereids)show user friendly error msg when
window functon contains order by expression (#41054)
97c2e7ffcfa is described below
commit 97c2e7ffcfa7a006ef5102f91d477ea0c1e81f60
Author: starocean999 <[email protected]>
AuthorDate: Mon Sep 23 09:11:56 2024 +0800
[enhancement](nereids)show user friendly error msg when window functon
contains order by expression (#41054)
pick from master https://github.com/apache/doris/pull/40937
## Proposed changes
Issue Number: close #xxx
<!--Describe your changes.-->
---
.../doris/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 7d9f36750d5..07d6f77f6d0 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 class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
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);
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 383ad266511..fe695368400 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 class ExtractAndNormalizeWindowExpression extends
OneRewriteRuleFactory i
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 004f47d6f1c..e4c4b2a2aeb 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"
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]