From 265cded7dac07dbd82375617a1e1f89f963610e9 Mon Sep 17 00:00:00 2001 From: LiBinfeng <46676950+LiBinfeng-01@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:15:51 +0800 Subject: [PATCH] [Fix](Planner) fix window function in aggregation (#22603) Problem: When window function in aggregation function, executor would report an error like: Required field 'node_type' was not present! Example: SELECT SUM(MAX(c1) OVER (PARTITION BY c2, c3)) FROM test_window_in_agg; Reason: When analyze aggregate, analytic expr (window function carrior when analyze) transfered to slot and loss message. So when serialize to thrift package, TExpr can not determine node_type of analytic expr. Solved: We do not support aggregate(window function) yet. So we report an error when analyze. --- .../org/apache/doris/analysis/SelectStmt.java | 10 ++++++++ .../window_functions/test_window_fn.groovy | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index 80ac94b688..b69466ab51 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -1343,6 +1343,16 @@ public class SelectStmt extends QueryStmt { } } + // can't contain analytic exprs + ArrayList aggExprsForChecking = Lists.newArrayList(); + TreeNode.collect(resultExprs, Expr.isAggregatePredicate(), aggExprsForChecking); + ArrayList analyticExprs = Lists.newArrayList(); + TreeNode.collect(aggExprsForChecking, AnalyticExpr.class, analyticExprs); + if (!analyticExprs.isEmpty()) { + throw new AnalysisException( + "AGGREGATE clause must not contain analytic expressions"); + } + // Collect the aggregate expressions from the SELECT, HAVING and ORDER BY clauses // of this statement. ArrayList aggExprs = Lists.newArrayList(); diff --git a/regression-test/suites/query_p0/sql_functions/window_functions/test_window_fn.groovy b/regression-test/suites/query_p0/sql_functions/window_functions/test_window_fn.groovy index 1cbab91a44..22a9e798f0 100644 --- a/regression-test/suites/query_p0/sql_functions/window_functions/test_window_fn.groovy +++ b/regression-test/suites/query_p0/sql_functions/window_functions/test_window_fn.groovy @@ -365,6 +365,29 @@ suite("test_window_fn") { """ sql "DROP TABLE IF EXISTS example_window_tb;" + + sql """ + CREATE TABLE IF NOT EXISTS test_window_in_agg + ( + `c1` int , + `c2` int , + `c3` int + ) + ENGINE=OLAP + DUPLICATE KEY(`c1`) + COMMENT "" + DISTRIBUTED BY HASH(`c1`) BUCKETS 1 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + test { + sql """SELECT SUM(MAX(c1) OVER (PARTITION BY c2, c3)) FROM test_window_in_agg;""" + exception "errCode = 2, detailMessage = AGGREGATE clause must not contain analytic expressions" + } + sql "DROP TABLE IF EXISTS test_window_in_agg;" }