Skip to content

Commit

Permalink
[fix](parser) Syntax error in column with agg_state (#46829)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

the problem only exists in the old optimizer

```
mysql> CREATE TABLE t (
    ->   `k1` int NULL,
    ->   `k2` agg_state<sum(int null)> GENERIC NOT NULL,
    ->   `k3` agg_state<group_concat(text null)> GENERIC NOT NULL
    -> ) ENGINE=OLAP
    -> AGGREGATE KEY(`k1`)
    -> DISTRIBUTED BY HASH(`k1`) BUCKETS 3
    -> PROPERTIES (
    -> "replication_allocation" = "tag.location.default: 1"
    -> );
ERROR 1105 (HY000): errCode = 2, detailMessage = Syntax error in line 3:
  `k2` agg_state<sum(int null)> GENERIC NOT NULL,
                 ^
Encountered: SUM
Expected: SUM is keyword, maybe `SUM`
```
  • Loading branch information
wyxxxcat authored Jan 13, 2025
1 parent 2f32ed2 commit 7f9ea1a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -6676,7 +6676,7 @@ type ::=
{: ScalarType type = ScalarType.createHllType();
RESULT = type;
:}
| KW_AGG_STATE LESSTHAN IDENT:fnName LPAREN type_def_nullable_list:list RPAREN GREATERTHAN
| KW_AGG_STATE LESSTHAN ident:fnName LPAREN type_def_nullable_list:list RPAREN GREATERTHAN
{:
RESULT = Expr.createAggStateType(fnName,
list.stream().map(TypeDef::getType).collect(Collectors.toList()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sum --
6

-- !avg --
2.0

-- !max_by --
2 1

-- !sum_const --
3

-- !sum_null --
\N

-- !desc --
k1 int Yes true \N
k2 agg_state<max_by(int, int null)> No false \N GENERIC

-- !length1 --
1 11

-- !group1 --
1 1

-- !merge1 --
1

-- !length2 --
1 11
2 11
3 11
4 11

-- !group2 --
1 1
2 1
3 2
4 3

-- !merge2 --
3

-- !union --
3

-- !max_by_null --
\N \N

-- !ndv --
1

-- !approx_count_distinct --
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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_dis_nereids_agg_state") {
sql "set global enable_agg_state=true"
sql "set enable_nereids_planner=false"
sql """ DROP TABLE IF EXISTS dis_nereids_d_table; """
sql """
create table dis_nereids_d_table(
k1 int null,
k2 int not null,
k3 bigint null,
k4 varchar(100) null
)
duplicate key (k1,k2,k3)
distributed BY hash(k1) buckets 3
properties("replication_num" = "1");
"""

sql "insert into dis_nereids_d_table select 1,1,1,'a';"
sql "insert into dis_nereids_d_table select 2,2,2,'b';"
sql "insert into dis_nereids_d_table select 3,3,null,'c';"

qt_sum """ select sum_merge(sum_state(k1)) from dis_nereids_d_table; """
qt_avg """ select avg_merge(avg_state(k1)) from dis_nereids_d_table; """
qt_max_by """ select max_by_merge(max_by_state(k1,k3)),min_by_merge(min_by_state(k1,k3)) from dis_nereids_d_table; """

qt_sum_const """ select sum_merge(sum_state(1)) from dis_nereids_d_table; """
qt_sum_null """ select sum_merge(sum_state(null)) from dis_nereids_d_table; """

sql """ DROP TABLE IF EXISTS dis_nereids_a_table; """
sql """
create table dis_nereids_a_table(
k1 int null,
k2 agg_state<max_by(int not null,int)> generic
)
aggregate key (k1)
distributed BY hash(k1) buckets 3
properties("replication_num" = "1");
"""

qt_desc "desc dis_nereids_a_table;"

sql "insert into dis_nereids_a_table select 1,max_by_state(1,3);"
sql "insert into dis_nereids_a_table select 1,max_by_state(2,2);"
sql "insert into dis_nereids_a_table values(1,max_by_state(3,1));"

qt_length1 """select k1,length(k2) from dis_nereids_a_table order by k1;"""
qt_group1 """select k1,max_by_merge(k2) from dis_nereids_a_table group by k1 order by k1;"""
qt_merge1 """select max_by_merge(k2) from dis_nereids_a_table;"""

sql "insert into dis_nereids_a_table select k1+1, max_by_state(k2,k1+10) from dis_nereids_d_table;"

qt_length2 """select k1,length(k2) from dis_nereids_a_table order by k1;"""
qt_group2 """select k1,max_by_merge(k2) from dis_nereids_a_table group by k1 order by k1;"""
qt_merge2 """select max_by_merge(k2) from dis_nereids_a_table;"""

qt_union """ select max_by_merge(kstate) from (select k1,max_by_union(k2) kstate from dis_nereids_a_table group by k1 order by k1) t; """
qt_max_by_null """ select max_by_merge(max_by_state(k1,null)),min_by_merge(min_by_state(null,k3)) from dis_nereids_d_table; """

test {
sql "select avg_state(1) from dis_nereids_d_table;"
}

qt_ndv """select ndv_merge(t) from (select ndv_union(ndv_state(1)) as t from dis_nereids_d_table group by k1)p;"""
qt_approx_count_distinct """select approx_count_distinct_merge(t) from (select approx_count_distinct_union(approx_count_distinct_state(1)) as t from dis_nereids_d_table group by k1)p;"""
}

0 comments on commit 7f9ea1a

Please sign in to comment.