You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/01/09 07:31:36 UTC

[doris] branch master updated: [feature](nereids) let set operation syntax campatible with lagecy planner (#15664)

This is an automated email from the ASF dual-hosted git repository.

morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 5ceb5441f4 [feature](nereids) let set operation syntax campatible with lagecy planner (#15664)
5ceb5441f4 is described below

commit 5ceb5441f4903f4f9f31f17c563563210449f21c
Author: AKIRA <33...@users.noreply.github.com>
AuthorDate: Mon Jan 9 15:31:29 2023 +0800

    [feature](nereids) let set operation syntax campatible with lagecy planner (#15664)
    
    Though this syntax doesn't get suppoted in many other systems since the order by clause here almost redandunt and useless but we have to keep consistent with the legacy doris syntax
    
    Here is a example:
    SELECT * FROM (SELECT k1, k3 FROM tbl1 ORDER BY k3 UNION ALL SELECT k1, k5 FROM tbl2) t;
---
 .../antlr4/org/apache/doris/nereids/DorisParser.g4 |  13 ++-
 .../doris/nereids/parser/LogicalPlanBuilder.java   |   8 +-
 .../rules/rewrite/logical/LimitPushDownTest.java   |  16 ++--
 .../data/nereids_syntax_p0/set_operation.out       | 104 +++++++++++++++++++++
 .../suites/nereids_syntax_p0/set_operation.groovy  |  25 ++++-
 5 files changed, 149 insertions(+), 17 deletions(-)

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 6494bceaa2..b865734754 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
@@ -21,6 +21,10 @@ parser grammar DorisParser;
 
 options { tokenVocab = DorisLexer; }
 
+@members {
+    public boolean doris_legacy_SQL_syntax = true;
+}
+
 multiStatements
     : (statement SEMICOLON*)+ EOF
     ;
@@ -53,7 +57,8 @@ planType
 
 //  -----------------Query-----------------
 query
-    : cte? queryTerm queryOrganization
+    : {!doris_legacy_SQL_syntax}? cte? queryTerm queryOrganization
+    | {doris_legacy_SQL_syntax}? queryTerm
     ;
 
 queryTerm
@@ -74,11 +79,13 @@ queryPrimary
     ;
 
 querySpecification
-    : selectClause
+    : {doris_legacy_SQL_syntax}? cte?
+      selectClause
       fromClause?
       whereClause?
       aggClause?
-      havingClause?                                                         #regularQuerySpecification
+      havingClause?
+      {doris_legacy_SQL_syntax}? queryOrganization                                               #regularQuerySpecification
     ;
 
 cte
diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 1e29b80bd2..4b01af8cec 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -384,7 +384,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
 
     @Override
     public LogicalPlan visitSubquery(SubqueryContext ctx) {
-        return ParserUtils.withOrigin(ctx, () -> visitQuery(ctx.query()));
+        return ParserUtils.withOrigin(ctx, () -> plan(ctx.query()));
     }
 
     @Override
@@ -408,7 +408,8 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
                         Optional.ofNullable(ctx.havingClause())
                 );
             }
-
+            selectPlan = withCte(selectPlan, ctx.cte());
+            selectPlan = withQueryOrganization(selectPlan, ctx.queryOrganization());
             return withSelectHint(selectPlan, selectCtx.selectHint());
         });
     }
@@ -1152,6 +1153,9 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> {
     }
 
     private LogicalPlan withQueryOrganization(LogicalPlan inputPlan, QueryOrganizationContext ctx) {
+        if (ctx == null) {
+            return inputPlan;
+        }
         Optional<SortClauseContext> sortClauseContext = Optional.ofNullable(ctx.sortClause());
         Optional<LimitClauseContext> limitClauseContext = Optional.ofNullable(ctx.limitClause());
         LogicalPlan sort = withSort(inputPlan, sortClauseContext);
diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/LimitPushDownTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/LimitPushDownTest.java
index f79ed455ab..ef251c911f 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/LimitPushDownTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/logical/LimitPushDownTest.java
@@ -220,17 +220,13 @@ class LimitPushDownTest extends TestWithFeService implements PatternMatchSupport
                         + "union all select k1 from t3 "
                         + "limit 10")
                 .rewrite()
-                .matches(logicalLimit(
+                .matches(
                         logicalUnion(
-                                logicalLimit(
-                                        logicalProject(
-                                                logicalOlapScan().when(scan -> "t1".equals(scan.getTable().getName()))
-                                        )
+                                logicalProject(
+                                        logicalOlapScan().when(scan -> "t1".equals(scan.getTable().getName()))
                                 ),
-                                logicalLimit(
-                                        logicalProject(
-                                                logicalOlapScan().when(scan -> "t2".equals(scan.getTable().getName()))
-                                        )
+                                logicalProject(
+                                        logicalOlapScan().when(scan -> "t2".equals(scan.getTable().getName()))
                                 ),
                                 logicalLimit(
                                         logicalProject(
@@ -238,7 +234,7 @@ class LimitPushDownTest extends TestWithFeService implements PatternMatchSupport
                                         )
                                 )
                         )
-                ));
+                );
     }
 
     private void test(JoinType joinType, boolean hasProject, PatternDescriptor<? extends Plan> pattern) {
diff --git a/regression-test/data/nereids_syntax_p0/set_operation.out b/regression-test/data/nereids_syntax_p0/set_operation.out
index 2e62101146..39a0740e6c 100644
--- a/regression-test/data/nereids_syntax_p0/set_operation.out
+++ b/regression-test/data/nereids_syntax_p0/set_operation.out
@@ -477,3 +477,107 @@ hell0
 1
 2
 
+-- !select43 --
+1	0
+1	1
+1	2
+1	3
+1	a
+1	a
+1	a
+1	b
+2	0
+2	2
+2	4
+2	6
+2	b
+2	b
+2	c
+2	c
+3	0
+3	3
+3	6
+3	9
+3	c
+3	d
+3	d
+3	d
+
+-- !select44 --
+1	0
+1	1
+1	2
+1	3
+1	a
+1	a
+1	a
+1	b
+2	0
+2	2
+2	4
+2	6
+2	b
+2	b
+2	c
+2	c
+3	0
+3	3
+3	6
+3	9
+3	c
+3	d
+3	d
+3	d
+
+-- !select45 --
+1	0
+1	1
+1	2
+1	3
+1	a
+1	a
+1	a
+1	b
+2	0
+2	2
+2	4
+2	6
+2	b
+2	b
+2	c
+2	c
+3	0
+3	3
+3	6
+3	9
+3	c
+3	d
+3	d
+3	d
+
+-- !select46 --
+1	0
+1	1
+1	2
+1	3
+1	a
+1	a
+1	a
+1	b
+2	0
+2	2
+2	4
+2	6
+2	b
+2	b
+2	c
+2	c
+3	0
+3	3
+3	6
+3	9
+3	c
+3	d
+3	d
+3	d
+
diff --git a/regression-test/suites/nereids_syntax_p0/set_operation.groovy b/regression-test/suites/nereids_syntax_p0/set_operation.groovy
index 757430c46e..3f3db650fb 100644
--- a/regression-test/suites/nereids_syntax_p0/set_operation.groovy
+++ b/regression-test/suites/nereids_syntax_p0/set_operation.groovy
@@ -139,8 +139,7 @@ suite("test_nereids_set_operation") {
             select * from (select k1, k2 from setOperationTableNotNullable union all select k1, k5 from setOperationTable) t;
     """
 
-    order_qt_select21 """
-            select * from (select k1, k2 from setOperationTableNotNullable union select k1, k5 from setOperationTable) t;
+    order_qt_select21 """            select * from (select k1, k2 from setOperationTableNotNullable union select k1, k5 from setOperationTable) t;
     """
 
     order_qt_select24 """select * from (select 1 a, 2 b 
@@ -242,4 +241,26 @@ suite("test_nereids_set_operation") {
     INTERSECT
     SELECT k1 FROM setOperationTable WHERE k2 > 0)
     """
+
+    order_qt_select43 """
+        SELECT * FROM (select k1, k3 from setOperationTableNotNullable order by k3 union all 
+            select k1, k5 from setOperationTable) t;
+    """
+
+    order_qt_select44 """
+    select k1, k3 from setOperationTableNotNullable order by k3 union all 
+            select k1, k5 from setOperationTable
+    """
+
+    order_qt_select45 """
+    (select k1, k3 from setOperationTableNotNullable order by k3) union all 
+            (select k1, k5 from setOperationTable)
+    """
+
+    order_qt_select46 """
+    (with cte AS (select k1, k3 from setOperationTableNotNullable) select * from cte order by k3) union all 
+            (select k1, k5 from setOperationTable)
+    """
+
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org