This is an automated email from the ASF dual-hosted git repository. huajianlan 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 a47eff1e46 [enhancement](Nereids) support all join type in Nereids that could do join by HashJoinNode (#11446) a47eff1e46 is described below commit a47eff1e4669e877bff6ffb710b93af9492227d6 Author: morrySnow <101034200+morrys...@users.noreply.github.com> AuthorDate: Wed Aug 3 12:14:17 2022 +0800 [enhancement](Nereids) support all join type in Nereids that could do join by HashJoinNode (#11446) add and test join type: 1. inner join 2. left outer join 3. right outer join 4. left semi join 5. right semi join 6. left anti join 7. right anti join --- .../antlr4/org/apache/doris/nereids/DorisParser.g4 | 6 +- .../doris/nereids/parser/LogicalPlanBuilder.java | 34 ++++++----- .../doris/nereids/parser/NereidsParserTest.java | 64 +++++++++++++++++++++ regression-test/data/nereids_syntax/join.out | 45 +++++++++++++++ regression-test/suites/nereids_syntax/join.groovy | 67 ++++++++++++++++++++++ 5 files changed, 201 insertions(+), 15 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 6fab3a9b0c..6af602e2f4 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 @@ -125,10 +125,12 @@ joinType : INNER? | CROSS | LEFT OUTER? - | LEFT? SEMI | RIGHT OUTER? | FULL OUTER? - | LEFT? ANTI + | LEFT SEMI + | RIGHT SEMI + | LEFT ANTI + | RIGHT ANTI ; joinCriteria 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 d20cb728e7..e4ebeddabb 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 @@ -709,23 +709,31 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> { LogicalPlan last = input; for (JoinRelationContext join : ctx.joinRelation()) { JoinType joinType; - if (join.joinType().LEFT() != null) { - joinType = JoinType.LEFT_OUTER_JOIN; - } else if (join.joinType().RIGHT() != null) { - joinType = JoinType.RIGHT_OUTER_JOIN; + if (join.joinType().CROSS() != null) { + joinType = JoinType.CROSS_JOIN; } else if (join.joinType().FULL() != null) { joinType = JoinType.FULL_OUTER_JOIN; } else if (join.joinType().SEMI() != null) { - joinType = JoinType.LEFT_SEMI_JOIN; + if (join.joinType().LEFT() != null) { + joinType = JoinType.LEFT_SEMI_JOIN; + } else { + joinType = JoinType.RIGHT_SEMI_JOIN; + } } else if (join.joinType().ANTI() != null) { - joinType = JoinType.LEFT_ANTI_JOIN; - } else if (join.joinType().CROSS() != null) { - joinType = JoinType.CROSS_JOIN; + if (join.joinType().LEFT() != null) { + joinType = JoinType.LEFT_ANTI_JOIN; + } else { + joinType = JoinType.RIGHT_ANTI_JOIN; + } + } else if (join.joinType().LEFT() != null) { + joinType = JoinType.LEFT_OUTER_JOIN; + } else if (join.joinType().RIGHT() != null) { + joinType = JoinType.RIGHT_OUTER_JOIN; } else { joinType = JoinType.INNER_JOIN; } - // TODO: natural join, lateral join, using join + // TODO: natural join, lateral join, using join, union join JoinCriteriaContext joinCriteria = join.joinCriteria(); Expression condition; if (joinCriteria == null) { @@ -734,7 +742,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> { condition = getExpression(joinCriteria.booleanExpression()); } - last = new LogicalJoin(joinType, Optional.ofNullable(condition), last, plan(join.relationPrimary())); + last = new LogicalJoin<>(joinType, Optional.ofNullable(condition), last, plan(join.relationPrimary())); } return last; } @@ -747,14 +755,14 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> { return input; } else { List<NamedExpression> projects = getNamedExpressions(selectCtx.namedExpressionSeq()); - return new LogicalProject(projects, input); + return new LogicalProject<>(projects, input); } }); } private LogicalPlan withFilter(LogicalPlan input, Optional<WhereClauseContext> whereCtx) { return input.optionalMap(whereCtx, () -> - new LogicalFilter(getExpression((whereCtx.get().booleanExpression())), input) + new LogicalFilter<>(getExpression((whereCtx.get().booleanExpression())), input) ); } @@ -763,7 +771,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor<Object> { return input.optionalMap(aggCtx, () -> { List<Expression> groupByExpressions = visit(aggCtx.get().groupByItem().expression(), Expression.class); List<NamedExpression> namedExpressions = getNamedExpressions(selectCtx.namedExpressionSeq()); - return new LogicalAggregate(groupByExpressions, namedExpressions, input); + return new LogicalAggregate<>(groupByExpressions, namedExpressions, input); }); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java index ef20960897..e5c1ffba03 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java @@ -21,9 +21,11 @@ import org.apache.doris.analysis.ExplainOptions; import org.apache.doris.analysis.StatementBase; import org.apache.doris.nereids.exceptions.ParseException; import org.apache.doris.nereids.glue.LogicalPlanAdapter; +import org.apache.doris.nereids.trees.plans.JoinType; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand; import org.apache.doris.nereids.trees.plans.commands.ExplainCommand.ExplainLevel; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; import org.apache.doris.nereids.trees.plans.logical.LogicalProject; @@ -128,4 +130,66 @@ public class NereidsParserTest { Assertions.assertTrue(explainOptions.isGraph()); Assertions.assertFalse(explainOptions.isVerbose()); } + + @Test + public void testParseJoin() { + NereidsParser nereidsParser = new NereidsParser(); + LogicalPlan logicalPlan; + LogicalJoin logicalJoin; + + String innerJoin1 = "SELECT t1.a FROM t1 INNER JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(innerJoin1); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.INNER_JOIN, logicalJoin.getJoinType()); + + String innerJoin2 = "SELECT t1.a FROM t1 JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(innerJoin2); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.INNER_JOIN, logicalJoin.getJoinType()); + + String leftJoin1 = "SELECT t1.a FROM t1 LEFT JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(leftJoin1); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.LEFT_OUTER_JOIN, logicalJoin.getJoinType()); + + String leftJoin2 = "SELECT t1.a FROM t1 LEFT OUTER JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(leftJoin2); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.LEFT_OUTER_JOIN, logicalJoin.getJoinType()); + + String rightJoin1 = "SELECT t1.a FROM t1 RIGHT JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(rightJoin1); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.RIGHT_OUTER_JOIN, logicalJoin.getJoinType()); + + String rightJoin2 = "SELECT t1.a FROM t1 RIGHT OUTER JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(rightJoin2); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.RIGHT_OUTER_JOIN, logicalJoin.getJoinType()); + + String leftSemiJoin = "SELECT t1.a FROM t1 LEFT SEMI JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(leftSemiJoin); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.LEFT_SEMI_JOIN, logicalJoin.getJoinType()); + + String rightSemiJoin = "SELECT t2.a FROM t1 RIGHT SEMI JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(rightSemiJoin); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.RIGHT_SEMI_JOIN, logicalJoin.getJoinType()); + + String leftAntiJoin = "SELECT t1.a FROM t1 LEFT ANTI JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(leftAntiJoin); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.LEFT_ANTI_JOIN, logicalJoin.getJoinType()); + + String righAntiJoin = "SELECT t2.a FROM t1 RIGHT ANTI JOIN t2 ON t1.id = t2.id;"; + logicalPlan = nereidsParser.parseSingle(righAntiJoin); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.RIGHT_ANTI_JOIN, logicalJoin.getJoinType()); + + String crossJoin = "SELECT t1.a FROM t1 CROSS JOIN t2;"; + logicalPlan = nereidsParser.parseSingle(crossJoin); + logicalJoin = (LogicalJoin) logicalPlan.child(0); + Assertions.assertEquals(JoinType.CROSS_JOIN, logicalJoin.getJoinType()); + } } diff --git a/regression-test/data/nereids_syntax/join.out b/regression-test/data/nereids_syntax/join.out new file mode 100644 index 0000000000..2a1cc0480a --- /dev/null +++ b/regression-test/data/nereids_syntax/join.out @@ -0,0 +1,45 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !inner_join_1 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !inner_join_2 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !left_outer_join_1 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !left_outer_join_2 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !right_outer_join_1 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !right_outer_join_2 -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK 15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL 9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB 29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 + +-- !left_semi_join -- +1309892 1 1303 1432 15 19920517 3-MEDIUM 0 24 2959704 5119906 7 2752524 73992 0 19920619 TRUCK +1309892 2 1303 1165 9 19920517 3-MEDIUM 0 21 2404899 5119906 8 2212507 68711 7 19920616 RAIL +1310179 6 1312 1455 29 19921110 3-MEDIUM 0 15 1705830 20506457 10 1535247 68233 8 19930114 FOB + +-- !right_semi_join -- +15 Supplier#000000015 DF35PepL5saAK INDIA 0 INDIA ASIA 18-687-542-7601 +29 Supplier#000000029 VVSymB3fbwaN ARGENTINA4 ARGENTINA AMERICA 11-773-203-7342 +9 Supplier#000000009 ,gJ6K2MKveYxQT IRAN 6 IRAN MIDDLE EAST 20-338-906-3675 + +-- !left_anti_join -- + +-- !right_anti_join -- + diff --git a/regression-test/suites/nereids_syntax/join.groovy b/regression-test/suites/nereids_syntax/join.groovy new file mode 100644 index 0000000000..0f598e78b1 --- /dev/null +++ b/regression-test/suites/nereids_syntax/join.groovy @@ -0,0 +1,67 @@ +// 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("join") { + sql """ + SET enable_vectorized_engine=true + """ + + sql """ + SET enable_nereids_planner=true + """ + + order_qt_inner_join_1 """ + SELECT * FROM lineorder JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_inner_join_2 """ + SELECT * FROM lineorder INNER JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_left_outer_join_1 """ + SELECT * FROM lineorder LEFT JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_left_outer_join_2 """ + SELECT * FROM lineorder LEFT OUTER JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_right_outer_join_1 """ + SELECT * FROM lineorder RIGHT JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_right_outer_join_2 """ + SELECT * FROM lineorder RIGHT OUTER JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_left_semi_join """ + SELECT * FROM lineorder LEFT SEMI JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_right_semi_join """ + SELECT * FROM lineorder RIGHT SEMI JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_left_anti_join """ + SELECT * FROM lineorder LEFT ANTI JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ + + order_qt_right_anti_join """ + SELECT * FROM lineorder RIGHT ANTI JOIN supplier ON lineorder.lo_suppkey = supplier.s_suppkey + """ +} + --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org