This is an automated email from the ASF dual-hosted git repository.
kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new 836a974cfc4 [feature](planner) Support `select constant from dual`
syntax sugar (#34200) (#34267)
836a974cfc4 is described below
commit 836a974cfc4addb9bef68a58857eccf7bb320b7f
Author: zy-kkk <[email protected]>
AuthorDate: Sat May 11 08:01:19 2024 +0800
[feature](planner) Support `select constant from dual` syntax sugar
(#34200) (#34267)
---
.../antlr4/org/apache/doris/nereids/DorisLexer.g4 | 4 +
.../antlr4/org/apache/doris/nereids/DorisParser.g4 | 1 +
fe/fe-core/src/main/cup/sql_parser.cup | 3 +
fe/fe-core/src/main/jflex/sql_scanner.flex | 1 +
regression-test/data/query_p0/dual/dual.out | 73 +++++++++++++++++
regression-test/suites/query_p0/dual/dual.groovy | 93 ++++++++++++++++++++++
6 files changed, 175 insertions(+)
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
index 3793001b699..e82538e1e20 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4
@@ -222,6 +222,7 @@ DO: 'DO';
DOUBLE: 'DOUBLE';
DROP: 'DROP';
DROPP: 'DROPP';
+DUAL: 'DUAL';
DUPLICATE: 'DUPLICATE';
DYNAMIC: 'DYNAMIC';
ELSE: 'ELSE';
@@ -656,6 +657,9 @@ BRACKETED_COMMENT
: '/*' {!isHint()}? ( BRACKETED_COMMENT | . )*? ('*/' |
{markUnclosedComment();} EOF) -> channel(HIDDEN)
;
+FROM_DUAL
+ : 'FROM' WS+ 'DUAL' -> channel(HIDDEN);
+
WS
: [ \r\n\t]+ -> channel(HIDDEN)
;
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
index 148a3f84def..c830a2f0d7e 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
@@ -651,6 +651,7 @@ nonReserved
| DISTINCTPC
| DISTINCTPCSA
| DO
+ | DUAL
| DYNAMIC
| ENABLE
| ENCRYPTKEY
diff --git a/fe/fe-core/src/main/cup/sql_parser.cup
b/fe/fe-core/src/main/cup/sql_parser.cup
index 48ddcbb514a..6159221f1e6 100644
--- a/fe/fe-core/src/main/cup/sql_parser.cup
+++ b/fe/fe-core/src/main/cup/sql_parser.cup
@@ -352,6 +352,7 @@ terminal String
KW_DOUBLE,
KW_DROP,
KW_DROPP,
+ KW_DUAL,
KW_DUPLICATE,
KW_DYNAMIC,
KW_ELSE,
@@ -5627,6 +5628,8 @@ type_function_name ::=
from_clause ::=
KW_FROM table_ref_list:l
{: RESULT = new FromClause(l); :}
+ | KW_FROM KW_DUAL
+ {: RESULT = null; :}
;
table_ref_list ::=
diff --git a/fe/fe-core/src/main/jflex/sql_scanner.flex
b/fe/fe-core/src/main/jflex/sql_scanner.flex
index 583fd916d20..2922eac328f 100644
--- a/fe/fe-core/src/main/jflex/sql_scanner.flex
+++ b/fe/fe-core/src/main/jflex/sql_scanner.flex
@@ -202,6 +202,7 @@ import org.apache.doris.qe.SqlModeHelper;
keywordMap.put("double", new Integer(SqlParserSymbols.KW_DOUBLE));
keywordMap.put("drop", new Integer(SqlParserSymbols.KW_DROP));
keywordMap.put("dropp", new Integer(SqlParserSymbols.KW_DROPP));
+ keywordMap.put("dual", new Integer(SqlParserSymbols.KW_DUAL));
keywordMap.put("duplicate", new
Integer(SqlParserSymbols.KW_DUPLICATE));
keywordMap.put("dynamic", new Integer(SqlParserSymbols.KW_DYNAMIC));
keywordMap.put("else", new Integer(SqlParserSymbols.KW_ELSE));
diff --git a/regression-test/data/query_p0/dual/dual.out
b/regression-test/data/query_p0/dual/dual.out
new file mode 100644
index 00000000000..fee517f8f2c
--- /dev/null
+++ b/regression-test/data/query_p0/dual/dual.out
@@ -0,0 +1,73 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+
+-- !sql --
+
+-- !sql --
+2
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+
+-- !sql --
+1
+2
+
+-- !sql --
+1
+
+-- !sql --
+1
+1
+1
+
+-- !sql --
+1
+
diff --git a/regression-test/suites/query_p0/dual/dual.groovy
b/regression-test/suites/query_p0/dual/dual.groovy
new file mode 100644
index 00000000000..ef579fecc0f
--- /dev/null
+++ b/regression-test/suites/query_p0/dual/dual.groovy
@@ -0,0 +1,93 @@
+// 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('dual') {
+
+ qt_sql 'select 1 from dual'
+ qt_sql 'select 1 from dual where 1'
+ qt_sql 'select 1 from dual where 1 = 1'
+ qt_sql 'select 1 from dual where 0'
+ qt_sql 'select 1 from dual where 1 = 0'
+ qt_sql 'select 1+1 from dual'
+
+ // Testing constant expressions in more complex contexts
+ qt_sql 'select * from (select 1 as a from dual) sub'
+ qt_sql 'select 1 from dual group by 1'
+ qt_sql 'select 1 from dual having 1'
+ qt_sql 'select 1 from dual group by 1 having 1'
+ qt_sql 'select 1 from dual order by 1'
+ qt_sql 'select 1 from dual order by 1 desc'
+ qt_sql 'select 1 from dual order by 1 limit 1'
+ qt_sql 'select 1 from dual order by 1 limit 1 offset 1'
+ qt_sql 'select 1 from dual where 1 in (1)'
+ qt_sql 'select 1 from dual where 1 group by 1'
+ qt_sql 'select 1 from dual where 1 having 1'
+ qt_sql 'select 1 from dual where 1 group by 1 having 1'
+ qt_sql 'select 1 from dual where 1 order by 1'
+ qt_sql 'with cte as (select 1 as a from dual) select a from cte'
+ qt_sql 'select a from (select 1 as a from dual union all select 2 as a
from dual) u'
+ qt_sql 'select row_number() over (order by 1) from dual;'
+
+ // Dropping and creating a table named 'dual' to test behavior when dual
is a real table
+ sql 'drop table if exists `dual`'
+ sql '''
+ create table `dual` (
+ k0 int
+ )
+ distributed by hash(k0) buckets 16
+ properties(
+ 'replication_num'='1'
+ )
+ '''
+ sql 'insert into `dual` values (1)'
+ sql 'insert into `dual` values (2)'
+ sql 'insert into `dual` values (3)'
+
+ qt_sql 'select 1 from `dual`'
+ qt_sql 'select 1 from dual'
+
+ // Tests for dropping 'dual' and ensuring correct error handling
+ test {
+ sql 'drop table if exists dual'
+ exception """DUAL is keyword, maybe `DUAL`"""
+ }
+ sql 'drop table if exists `dual`'
+
+ // Test error handling when table does not exist
+ test {
+ sql "select 1 from `dual`"
+ exception "Unknown table 'dual'"
+ }
+
+ // Disable and enable Nereids planner to check behavior differences
+ sql "set enable_nereids_planner = false"
+ test {
+ sql "select 1 from `dual`"
+ exception "Unknown table 'dual'"
+ }
+ sql "set enable_nereids_planner = true"
+
+ // Tests for unknown column errors
+ test {
+ sql "select a from dual"
+ exception "Unknown column 'a' in 'table list'"
+ }
+ test {
+ sql "select 1, a from dual"
+ exception "Unknown column 'a' in 'table list'"
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]