Skip to content

Commit 836a974

Browse files
authored
[feature](planner) Support select constant from dual syntax sugar (#34200) (#34267)
1 parent 2fb8d68 commit 836a974

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ DO: 'DO';
222222
DOUBLE: 'DOUBLE';
223223
DROP: 'DROP';
224224
DROPP: 'DROPP';
225+
DUAL: 'DUAL';
225226
DUPLICATE: 'DUPLICATE';
226227
DYNAMIC: 'DYNAMIC';
227228
ELSE: 'ELSE';
@@ -656,6 +657,9 @@ BRACKETED_COMMENT
656657
: '/*' {!isHint()}? ( BRACKETED_COMMENT | . )*? ('*/' | {markUnclosedComment();} EOF) -> channel(HIDDEN)
657658
;
658659

660+
FROM_DUAL
661+
: 'FROM' WS+ 'DUAL' -> channel(HIDDEN);
662+
659663
WS
660664
: [ \r\n\t]+ -> channel(HIDDEN)
661665
;

fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ nonReserved
651651
| DISTINCTPC
652652
| DISTINCTPCSA
653653
| DO
654+
| DUAL
654655
| DYNAMIC
655656
| ENABLE
656657
| ENCRYPTKEY

fe/fe-core/src/main/cup/sql_parser.cup

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ terminal String
352352
KW_DOUBLE,
353353
KW_DROP,
354354
KW_DROPP,
355+
KW_DUAL,
355356
KW_DUPLICATE,
356357
KW_DYNAMIC,
357358
KW_ELSE,
@@ -5627,6 +5628,8 @@ type_function_name ::=
56275628
from_clause ::=
56285629
KW_FROM table_ref_list:l
56295630
{: RESULT = new FromClause(l); :}
5631+
| KW_FROM KW_DUAL
5632+
{: RESULT = null; :}
56305633
;
56315634

56325635
table_ref_list ::=

fe/fe-core/src/main/jflex/sql_scanner.flex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ import org.apache.doris.qe.SqlModeHelper;
202202
keywordMap.put("double", new Integer(SqlParserSymbols.KW_DOUBLE));
203203
keywordMap.put("drop", new Integer(SqlParserSymbols.KW_DROP));
204204
keywordMap.put("dropp", new Integer(SqlParserSymbols.KW_DROPP));
205+
keywordMap.put("dual", new Integer(SqlParserSymbols.KW_DUAL));
205206
keywordMap.put("duplicate", new Integer(SqlParserSymbols.KW_DUPLICATE));
206207
keywordMap.put("dynamic", new Integer(SqlParserSymbols.KW_DYNAMIC));
207208
keywordMap.put("else", new Integer(SqlParserSymbols.KW_ELSE));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !sql --
3+
1
4+
5+
-- !sql --
6+
1
7+
8+
-- !sql --
9+
1
10+
11+
-- !sql --
12+
13+
-- !sql --
14+
15+
-- !sql --
16+
2
17+
18+
-- !sql --
19+
1
20+
21+
-- !sql --
22+
1
23+
24+
-- !sql --
25+
1
26+
27+
-- !sql --
28+
1
29+
30+
-- !sql --
31+
1
32+
33+
-- !sql --
34+
1
35+
36+
-- !sql --
37+
1
38+
39+
-- !sql --
40+
41+
-- !sql --
42+
1
43+
44+
-- !sql --
45+
1
46+
47+
-- !sql --
48+
1
49+
50+
-- !sql --
51+
1
52+
53+
-- !sql --
54+
1
55+
56+
-- !sql --
57+
1
58+
59+
-- !sql --
60+
1
61+
2
62+
63+
-- !sql --
64+
1
65+
66+
-- !sql --
67+
1
68+
1
69+
1
70+
71+
-- !sql --
72+
1
73+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite('dual') {
19+
20+
qt_sql 'select 1 from dual'
21+
qt_sql 'select 1 from dual where 1'
22+
qt_sql 'select 1 from dual where 1 = 1'
23+
qt_sql 'select 1 from dual where 0'
24+
qt_sql 'select 1 from dual where 1 = 0'
25+
qt_sql 'select 1+1 from dual'
26+
27+
// Testing constant expressions in more complex contexts
28+
qt_sql 'select * from (select 1 as a from dual) sub'
29+
qt_sql 'select 1 from dual group by 1'
30+
qt_sql 'select 1 from dual having 1'
31+
qt_sql 'select 1 from dual group by 1 having 1'
32+
qt_sql 'select 1 from dual order by 1'
33+
qt_sql 'select 1 from dual order by 1 desc'
34+
qt_sql 'select 1 from dual order by 1 limit 1'
35+
qt_sql 'select 1 from dual order by 1 limit 1 offset 1'
36+
qt_sql 'select 1 from dual where 1 in (1)'
37+
qt_sql 'select 1 from dual where 1 group by 1'
38+
qt_sql 'select 1 from dual where 1 having 1'
39+
qt_sql 'select 1 from dual where 1 group by 1 having 1'
40+
qt_sql 'select 1 from dual where 1 order by 1'
41+
qt_sql 'with cte as (select 1 as a from dual) select a from cte'
42+
qt_sql 'select a from (select 1 as a from dual union all select 2 as a from dual) u'
43+
qt_sql 'select row_number() over (order by 1) from dual;'
44+
45+
// Dropping and creating a table named 'dual' to test behavior when dual is a real table
46+
sql 'drop table if exists `dual`'
47+
sql '''
48+
create table `dual` (
49+
k0 int
50+
)
51+
distributed by hash(k0) buckets 16
52+
properties(
53+
'replication_num'='1'
54+
)
55+
'''
56+
sql 'insert into `dual` values (1)'
57+
sql 'insert into `dual` values (2)'
58+
sql 'insert into `dual` values (3)'
59+
60+
qt_sql 'select 1 from `dual`'
61+
qt_sql 'select 1 from dual'
62+
63+
// Tests for dropping 'dual' and ensuring correct error handling
64+
test {
65+
sql 'drop table if exists dual'
66+
exception """DUAL is keyword, maybe `DUAL`"""
67+
}
68+
sql 'drop table if exists `dual`'
69+
70+
// Test error handling when table does not exist
71+
test {
72+
sql "select 1 from `dual`"
73+
exception "Unknown table 'dual'"
74+
}
75+
76+
// Disable and enable Nereids planner to check behavior differences
77+
sql "set enable_nereids_planner = false"
78+
test {
79+
sql "select 1 from `dual`"
80+
exception "Unknown table 'dual'"
81+
}
82+
sql "set enable_nereids_planner = true"
83+
84+
// Tests for unknown column errors
85+
test {
86+
sql "select a from dual"
87+
exception "Unknown column 'a' in 'table list'"
88+
}
89+
test {
90+
sql "select 1, a from dual"
91+
exception "Unknown column 'a' in 'table list'"
92+
}
93+
}

0 commit comments

Comments
 (0)