From 5d4a2d93a675c18968f42eeacd0bb8cb9978eb5f Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Mon, 26 Feb 2024 14:32:46 +0800 Subject: [PATCH] [feature](nereids) support join with joinRelation (#30909) --- .../antlr4/org/apache/doris/nereids/DorisParser.g4 | 9 +++++++-- .../doris/nereids/parser/LogicalPlanBuilder.java | 14 ++++++++++++-- .../data/nereids_p0/subquery/test_subquery.out | 3 +++ .../nereids_p0/subquery/test_subquery.groovy | 6 ++++++ 4 files changed, 28 insertions(+), 4 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 73933fd86b..cb34af20e6 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 @@ -71,7 +71,7 @@ statementBase whereClause #update | explain? cte? DELETE FROM tableName=multipartIdentifier partitionSpec? tableAlias - (USING relation (COMMA relation)*)? + (USING relations)? whereClause? #delete | LOAD LABEL lableName=identifier LEFT_PAREN dataDescs+=dataDesc (COMMA dataDescs+=dataDesc)* RIGHT_PAREN @@ -333,7 +333,11 @@ whereClause ; fromClause - : FROM relation (COMMA relation)* + : FROM relations + ; + +relations + : relation (COMMA relation)* ; relation @@ -451,6 +455,7 @@ relationPrimary | tvfName=identifier LEFT_PAREN (properties=propertyItemList)? RIGHT_PAREN tableAlias #tableValuedFunction + | LEFT_PAREN relations RIGHT_PAREN #relationList ; materializedViewName 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 4fd6242782..9fd280c7f6 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 @@ -805,7 +805,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { StatementScopeIdGenerator.newRelationId(), visitMultipartIdentifier(ctx.tableName))); query = withTableAlias(query, ctx.tableAlias()); if (ctx.fromClause() != null) { - query = withRelations(query, ctx.fromClause().relation()); + query = withRelations(query, ctx.fromClause().relations().relation()); } query = withFilter(query, Optional.of(ctx.whereClause())); String tableAlias = null; @@ -836,7 +836,7 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { return new DeleteFromCommand(tableName, tableAlias, partitionSpec.first, partitionSpec.second, query); } else { // convert to insert into select - query = withRelations(query, ctx.relation()); + query = withRelations(query, ctx.relations().relation()); query = withFilter(query, Optional.ofNullable(ctx.whereClause())); Optional cte = Optional.empty(); if (ctx.cte() != null) { @@ -2248,9 +2248,19 @@ public class LogicalPlanBuilder extends DorisParserBaseVisitor { @Override public LogicalPlan visitFromClause(FromClauseContext ctx) { + return ParserUtils.withOrigin(ctx, () -> visitRelations(ctx.relations())); + } + + @Override + public LogicalPlan visitRelations(DorisParser.RelationsContext ctx) { return ParserUtils.withOrigin(ctx, () -> withRelations(null, ctx.relation())); } + @Override + public LogicalPlan visitRelationList(DorisParser.RelationListContext ctx) { + return ParserUtils.withOrigin(ctx, () -> withRelations(null, ctx.relations().relation())); + } + /* ******************************************************************************************** * Table Identifier parsing * ******************************************************************************************** */ diff --git a/regression-test/data/nereids_p0/subquery/test_subquery.out b/regression-test/data/nereids_p0/subquery/test_subquery.out index 004aa0bcad..a8d6064680 100644 --- a/regression-test/data/nereids_p0/subquery/test_subquery.out +++ b/regression-test/data/nereids_p0/subquery/test_subquery.out @@ -30,3 +30,6 @@ true 15 1992 3021 11011920 0.000 true 9999-12-12 2015-04-02T00:00 3.141592653 2 1 9 2 \N +-- !select_sub2 -- +9 + diff --git a/regression-test/suites/nereids_p0/subquery/test_subquery.groovy b/regression-test/suites/nereids_p0/subquery/test_subquery.groovy index 36a863a211..aa94e91e8d 100644 --- a/regression-test/suites/nereids_p0/subquery/test_subquery.groovy +++ b/regression-test/suites/nereids_p0/subquery/test_subquery.groovy @@ -244,6 +244,12 @@ suite("test_subquery") { sql """insert into table_9_undef_undef values (0,3,"his",'g'),(1,8,'p','n'),(2,null,"get","got"),(3,3,'r','r'),(4,null,"or","get"),(5,0,'j',"yeah"),(6,null,'w','x'),(7,8,'q',"for"),(8,3,'p',"that");""" qt_select_sub"""SELECT DISTINCT alias1.`pk` AS field1, alias2.`col_int_undef_signed` AS field2 FROM table_23_undef_undef AS alias1, table_20_undef_undef AS alias2 WHERE ( EXISTS ( SELECT DISTINCT SQ1_alias1.`col_varchar_10__undef_signed` AS SQ1_field1 FROM table_9_undef_undef AS SQ1_alias1 WHERE SQ1_alias1.`col_varchar_10__undef_signed` = alias1.`col_varchar_10__undef_signed` ) ) OR alias1.`col_varchar_1024__undef_signed` = "TmxRwcNZHC" AND ( alias1.`col_varchar_10__undef_signed` <> "rnZeukOcuM" AND alias2.`col_varchar_10__undef_signed` != "dbPAEpzstk" ) ORDER BY alias1.`pk`, field1, field2 LIMIT 2 OFFSET 7; """ + qt_select_sub2 """select count(*) from table_9_undef_undef SQ1_alias1 + LEFT JOIN ( table_20_undef_undef AS SQ1_alias2 RIGHT OUTER + JOIN table_23_undef_undef AS SQ1_alias3 + ON SQ1_alias3.`pk` = SQ1_alias2.`pk` ) + ON SQ1_alias1.`pk` = SQ1_alias2.`pk`;""" + sql """drop table if exists table_23_undef_undef""" sql """drop table if exists table_20_undef_undef""" sql """drop table if exists table_9_undef_undef"""