From ad61c84a606d2de6e048e0a6ecf035ab9394e803 Mon Sep 17 00:00:00 2001 From: mch_ucchi <41606806+sohardforaname@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:50:52 +0800 Subject: [PATCH] [fix](planner) fix conjunct planned on exchange node (#18042) sql like: select k5, k6, SUM(k3) AS k3 from ( select k5, date_format(k6, '%Y-%m-%d') as k6, count(distinct k3) as k3 from t group by k5, k6 ) AS temp where 1=1 group by k5, k6; will throw exception since conjuncts planned on exchange node, because exchange node cannot handle conjuncts, now we skip exchange node when planning conjuncts, which fixes the bug. notice: the bug occurs iff the conjunct is always true like 1=1 above. --- .../org/apache/doris/planner/PlanNode.java | 4 ++ .../correctness_p0/test_distinct_agg.groovy | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 regression-test/suites/correctness_p0/test_distinct_agg.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java index 4543576a0db0ce..80646867190e6b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/PlanNode.java @@ -714,6 +714,10 @@ public void init(Analyzer analyzer) throws UserException { * Assign remaining unassigned conjuncts. */ protected void assignConjuncts(Analyzer analyzer) { + // we cannot plan conjuncts on exchange node, so we just skip the node. + if (this instanceof ExchangeNode) { + return; + } List unassigned = analyzer.getUnassignedConjuncts(this); for (Expr unassignedConjunct : unassigned) { addConjunct(unassignedConjunct); diff --git a/regression-test/suites/correctness_p0/test_distinct_agg.groovy b/regression-test/suites/correctness_p0/test_distinct_agg.groovy new file mode 100644 index 00000000000000..8c80c9f1847a38 --- /dev/null +++ b/regression-test/suites/correctness_p0/test_distinct_agg.groovy @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_distinct_agg") { + sql 'drop table if exists t' + + sql ''' + CREATE TABLE `t` ( + `k1` bigint(20) NULL, + `k2` varchar(20) NULL, + `k3` varchar(20) NULL, + `k4` varchar(20) NULL, + `k5` varchar(20) NULL, + `k6` datetime NULL + ) ENGINE=OLAP + UNIQUE KEY(`k1`, `k2`) + DISTRIBUTED BY HASH(`k1`, `k2`) BUCKETS 3 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + ); + ''' + + sql ''' + INSERT INTO `t` (`k1`, `k2`, `k3`, `k4`, `k5`, `k6`) VALUES + (1, '1234', 'A0', 'C0', '1', '2023-01-10 23:00:00'); + ''' + + test { + sql ''' + select k5, k6, SUM(k3) AS k3 + from ( + select + k5, + date_format(k6, '%Y-%m-%d') as k6, + count(distinct k3) as k3 + from t + where 1=1 + group by k5, k6 + ) AS temp where 1=1 + group by k5, k6; + ''' + result([['1', '2023-01-10', 1L]]) + } +} \ No newline at end of file