From 062e8755848cb342c326a345e32d838f10595a58 Mon Sep 17 00:00:00 2001 From: morrySnow Date: Wed, 20 Dec 2023 16:39:13 +0800 Subject: [PATCH] [fix](Nereids) join order is not right after sql parsing --- .../nereids/parser/LogicalPlanBuilder.java | 3 +- .../plans/DeleteFromUsingCommandTest.java | 25 +- .../trees/plans/UpdateCommandTest.java | 26 +- .../data/nereids_syntax_p0/join_order.out | 464 ++++++++++++++++++ .../nereids_syntax_p0/join_order.groovy | 83 ++++ 5 files changed, 572 insertions(+), 29 deletions(-) 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 c2c67174489479..c546e90c9df2c9 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 @@ -2808,7 +2808,7 @@ private LogicalPlan withRelations(LogicalPlan inputPlan, List r LogicalPlan left = inputPlan; for (RelationContext relation : relations) { // build left deep join tree - LogicalPlan right = visitRelation(relation); + LogicalPlan right = withJoinRelations(visitRelation(relation), relation); left = (left == null) ? right : new LogicalJoin<>( JoinType.CROSS_JOIN, @@ -2818,7 +2818,6 @@ private LogicalPlan withRelations(LogicalPlan inputPlan, List r Optional.empty(), left, right); - left = withJoinRelations(left, relation); // TODO: pivot and lateral view } return left; diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/DeleteFromUsingCommandTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/DeleteFromUsingCommandTest.java index ab6e33e6fc3c45..913d357f7b87e2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/DeleteFromUsingCommandTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/DeleteFromUsingCommandTest.java @@ -73,7 +73,7 @@ protected void runBeforeAll() throws Exception { public void testFromClauseDelete() throws AnalysisException { String sql = "delete from t1 a using src join t2 on src.k1 = t2.k1 where t2.k1 = a.k1"; LogicalPlan parsed = new NereidsParser().parseSingle(sql); - Assertions.assertTrue(parsed instanceof DeleteFromUsingCommand); + Assertions.assertInstanceOf(DeleteFromUsingCommand.class, parsed); DeleteFromUsingCommand command = ((DeleteFromUsingCommand) parsed); LogicalPlan plan = command.completeQueryPlan(connectContext, command.getLogicalQuery()); PlanChecker.from(connectContext, plan) @@ -83,19 +83,22 @@ public void testFromClauseDelete() throws AnalysisException { logicalOlapTableSink( logicalProject( logicalJoin( - logicalJoin( - logicalProject( - logicalFilter( - logicalOlapScan() + logicalProject( + logicalJoin( + logicalProject( + logicalFilter( + logicalOlapScan() + ) + ), + logicalProject( + logicalFilter( + logicalOlapScan() + ) ) - ), - logicalProject( - logicalOlapScan()) + ) ), logicalProject( - logicalFilter( - logicalOlapScan() - ) + logicalOlapScan() ) ) ) diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/UpdateCommandTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/UpdateCommandTest.java index 731375c953a13f..c93dd0c52b75ea 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/UpdateCommandTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/UpdateCommandTest.java @@ -98,24 +98,18 @@ public void testFromClauseUpdate() { LogicalPlan plan = command.completeQueryPlan(connectContext, command.getLogicalQuery()); PlanChecker.from(connectContext, plan) .analyze(plan) - .rewrite() .matches( - logicalOlapTableSink( - logicalProject( + logicalFilter( + logicalJoin( + logicalSubQueryAlias( + logicalFilter( + logicalOlapScan() + ) + ), logicalJoin( - logicalJoin( - logicalProject( - logicalFilter( - logicalOlapScan() - ) - ), - logicalProject( - logicalOlapScan()) - ), - logicalProject( - logicalFilter( - logicalOlapScan() - ) + logicalOlapScan(), + logicalFilter( + logicalOlapScan() ) ) ) diff --git a/regression-test/data/nereids_syntax_p0/join_order.out b/regression-test/data/nereids_syntax_p0/join_order.out index 5176628bce0774..f81c024ccc7ae4 100644 --- a/regression-test/data/nereids_syntax_p0/join_order.out +++ b/regression-test/data/nereids_syntax_p0/join_order.out @@ -8,3 +8,467 @@ -- !select1 -- 1 +-- !test_order_with_both_comma_and_join -- +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +0 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +1 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +10 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +11 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +12 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +13 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +14 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +15 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +16 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +17 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +18 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +19 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +2 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +20 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +4 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +6 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +7 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +8 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 + diff --git a/regression-test/suites/nereids_syntax_p0/join_order.groovy b/regression-test/suites/nereids_syntax_p0/join_order.groovy index c05bb8d8d2b13e..64056e0202bc84 100644 --- a/regression-test/suites/nereids_syntax_p0/join_order.groovy +++ b/regression-test/suites/nereids_syntax_p0/join_order.groovy @@ -184,4 +184,87 @@ suite("join_order") { sql """drop table if exists test_table_t1;""" sql """drop table if exists test_table_t2;""" sql """drop table if exists test_table_t3;""" + + sql """ + drop table if exists table_3_undef_undef; + """ + + sql """ + drop table if exists table_21_undef_undef; + """ + + sql """ + drop table if exists table_22_undef_undef; + """ + + sql """ + create table table_3_undef_undef ( + `pk` int, + `col_int_undef_signed` int , + `col_varchar_10__undef_signed` varchar(10) , + `col_varchar_1024__undef_signed` varchar(1024) + )engine=olap + distributed by hash(pk) buckets 10 + properties( + 'replication_num' = '1' + ); + """ + + sql """ + insert into table_3_undef_undef values (0,1,"right","me"),(1,8,'q',"have"),(2,7,'o','e'); + """ + + sql """ + create table table_21_undef_undef ( + `pk` int, + `col_int_undef_signed` int , + `col_varchar_10__undef_signed` varchar(10) , + `col_varchar_1024__undef_signed` varchar(1024) + )engine=olap + distributed by hash(pk) buckets 10 + properties( + 'replication_num' = '1' + ); + """ + + sql """ + insert into table_21_undef_undef values (0,7,'y','b'),(1,null,'j','w'),(2,4,"this","she"),(3,null,'w','r'),(4,1,'i','j'),(5,null,'j','h'),(6,null,'k','h'),(7,null,'o',"when"),(8,null,"out",'n'),(9,8,"out",'h'),(10,null,'c','j'),(11,null,'y','z'),(12,null,'m',"so"),(13,null,"so",'m'),(14,2,"not","and"),(15,0,"about","really"),(16,null,'p',"that's"),(17,4,'z','y'),(18,6,'t','f'),(19,7,'k','w'),(20,9,'a',"for"); + """ + + sql """ + create table table_22_undef_undef ( + `pk` int, + `col_int_undef_signed` int , + `col_varchar_10__undef_signed` varchar(10) , + `col_varchar_1024__undef_signed` varchar(1024) + ) + engine=olap + distributed by hash(pk) buckets 10 + properties( + 'replication_num' = '1' + ); + """ + + sql """ + insert into table_22_undef_undef values (0,null,"can","why"),(1,null,"had","yeah"),(2,null,"ok",'y'),(3,null,"this",'w'),(4,null,'f',"not"),(5,7,'v',"really"),(6,5,"for",'y'),(7,null,'o',"of"),(8,0,'x','q'),(9,null,"about",'h'),(10,8,"you","this"),(11,null,'i','y'),(12,null,"this","who"),(13,4,"see",'h'),(14,8,"because","him"),(15,1,"good",'r'),(16,6,"know","know"),(17,3,"what",'e'),(18,null,'h',"then"),(19,null,'l','z'),(20,4,'l',"i"),(21,null,'f','q'); + """ + + order_qt_test_order_with_both_comma_and_join """ + SELECT t1.`pk` + FROM table_21_undef_undef AS t1, + table_3_undef_undef AS alias1 + FULL OUTER JOIN table_22_undef_undef AS alias2 ON alias1.`pk` = alias2.`pk` + """ + + sql """ + drop table if exists table_3_undef_undef; + """ + + sql """ + drop table if exists table_21_undef_undef; + """ + + sql """ + drop table if exists table_22_undef_undef; + """ }