Skip to content

Commit

Permalink
[fix](planner) fix conjunct planned on exchange node (apache#18042)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sohardforaname authored and morningman committed Mar 28, 2023
1 parent d918886 commit ad61c84
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr> unassigned = analyzer.getUnassignedConjuncts(this);
for (Expr unassignedConjunct : unassigned) {
addConjunct(unassignedConjunct);
Expand Down
58 changes: 58 additions & 0 deletions regression-test/suites/correctness_p0/test_distinct_agg.groovy
Original file line number Diff line number Diff line change
@@ -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]])
}
}

0 comments on commit ad61c84

Please sign in to comment.