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 3ea98b65df [Fix](Nereids) fix nereids failed to parse set operation
with query in parenthesis (#18062)
3ea98b65df is described below
commit 3ea98b65df504564a0dda879f9eb7928c8f5f634
Author: mch_ucchi <[email protected]>
AuthorDate: Fri Mar 31 15:55:52 2023 +0800
[Fix](Nereids) fix nereids failed to parse set operation with query in
parenthesis (#18062)
sql like the format (q1, q2, q3 is a query):
``` sql
(q1)
UNION ALL (q2)
UNION ALL (q3)
ORDER BY keys
```
cannot be parsed by nereids, because order will be recognized as an alias
of query, we add queryOrganization to avoid it.
---
.../main/antlr4/org/apache/doris/nereids/DorisParser.g4 | 6 ++++--
.../apache/doris/nereids/parser/LogicalPlanBuilder.java | 11 +++++++----
.../trees/plans/logical/LogicalSetOperation.java | 9 +++++----
.../apache/doris/nereids/parser/NereidsParserTest.java | 17 +++++++++++++++--
regression-test/suites/nereids_p0/join/test_join.groovy | 8 ++++----
.../suites/nereids_p0/union/test_union.groovy | 1 -
6 files changed, 35 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 73dc5079bd..2749ac8d15 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
@@ -68,9 +68,11 @@ planType
;
// -----------------Query-----------------
+// add queryOrganization for parse (q1) union (q2) union (q3) order by keys,
otherwise 'order' will be recognized to be
+// identifier.
query
: {!doris_legacy_SQL_syntax}? cte? queryTerm queryOrganization
- | {doris_legacy_SQL_syntax}? queryTerm
+ | {doris_legacy_SQL_syntax}? queryTerm queryOrganization
;
queryTerm
@@ -186,7 +188,7 @@ queryOrganization
;
sortClause
- : (ORDER BY sortItem (COMMA sortItem)*)
+ : ORDER BY sortItem (COMMA sortItem)*
;
sortItem
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 22dcd3e171..65cdf62ad5 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
@@ -422,14 +422,17 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
.add(rightQuery)
.build();
+ LogicalPlan plan;
if (ctx.UNION() != null) {
- return new LogicalUnion(qualifier, newChildren);
+ plan = new LogicalUnion(qualifier, newChildren);
} else if (ctx.EXCEPT() != null) {
- return new LogicalExcept(qualifier, newChildren);
+ plan = new LogicalExcept(qualifier, newChildren);
} else if (ctx.INTERSECT() != null) {
- return new LogicalIntersect(qualifier, newChildren);
+ plan = new LogicalIntersect(qualifier, newChildren);
+ } else {
+ throw new ParseException("not support", ctx);
}
- throw new ParseException("not support", ctx);
+ return plan;
});
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java
index 2faeb82f8f..e1a74c0790 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSetOperation.java
@@ -114,12 +114,13 @@ public abstract class LogicalSetOperation extends
AbstractLogicalPlan implements
ImmutableList.Builder<NamedExpression> newOutputs = new Builder<>();
for (Expression expression : leftCastExpressions) {
if (expression instanceof Cast) {
+ Cast cast = ((Cast) expression);
newOutputs.add(new SlotReference(
- ((Cast) expression).child().toSql(),
expression.getDataType(),
- ((Cast) expression).child().nullable()));
+ cast.child().toSql(), expression.getDataType(),
+ cast.child().nullable()));
} else if (expression instanceof Slot) {
- newOutputs.add(new SlotReference(
- expression.toSql(), expression.getDataType(),
expression.nullable()));
+ Slot slot = ((Slot) expression);
+ newOutputs.add(new SlotReference(slot.toSql(),
slot.getDataType(), slot.nullable()));
}
}
return newOutputs.build();
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 07e6b7d448..bade627b9b 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
@@ -267,9 +267,22 @@ public class NereidsParserTest extends ParserTestBase {
System.out.println(logicalPlan.treeString());
String union1 = "select * from t1 union (select * from t2 union all
select * from t3)";
- NereidsParser nereidsParser1 = new NereidsParser();
- LogicalPlan logicalPlan1 = nereidsParser1.parseSingle(union1);
+ LogicalPlan logicalPlan1 = nereidsParser.parseSingle(union1);
System.out.println(logicalPlan1.treeString());
+
+ String union2 = "(SELECT K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11
FROM test WHERE K1 > 0)"
+ + " UNION ALL (SELECT 1, 2, 3, 4, 3.14, 'HELLO', 'WORLD', 0.0,
1.1, CAST('1989-03-21' AS DATE), CAST('1989-03-21 13:00:00' AS DATETIME))"
+ + " UNION ALL (SELECT K1, K2, K3, K4, K5, K6, K7, K8, K9, K10,
K11 FROM baseall WHERE K3 > 0)"
+ + " ORDER BY K1, K2, K3, K4";
+ LogicalPlan logicalPlan2 = nereidsParser.parseSingle(union2);
+ System.out.println(logicalPlan2.treeString());
+
+ String union3 = "select a.k1, a.k2, a.k3, b.k1, b.k2, b.k3 from test a
left outer join baseall b"
+ + " on a.k1 = b.k1 and a.k2 > b.k2 union (select a.k1, a.k2,
a.k3, b.k1, b.k2, b.k3"
+ + " from test a right outer join baseall b on a.k1 = b.k1 and
a.k2 > b.k2)"
+ + " order by isnull(a.k1), 1, 2, 3, 4, 5 limit 65535";
+ LogicalPlan logicalPlan3 = nereidsParser.parseSingle(union3);
+ System.out.println(logicalPlan3.treeString());
}
@Test
diff --git a/regression-test/suites/nereids_p0/join/test_join.groovy
b/regression-test/suites/nereids_p0/join/test_join.groovy
index 9593382257..2ea851a372 100644
--- a/regression-test/suites/nereids_p0/join/test_join.groovy
+++ b/regression-test/suites/nereids_p0/join/test_join.groovy
@@ -398,12 +398,12 @@ suite("test_join", "nereids_p0") {
sql"""select ${s} from ${tbName1} a left outer join ${tbName2} b on
a.k1 = b.k1
left outer join ${tbName3} c on a.k2 = c.k2 order by 1, 2, 3,
4, 5 limit 65535"""
}
- sql"""select a.k1 k1, a.k2, a.k3, b.k1, b.k2, b.k3 from ${tbName1} a full
outer join ${tbName2} b
- on a.k1 = b.k1 and a.k2 > b.k2 order by isnull(k1), 1, 2, 3, 4, 5
limit 65535"""
- sql"""select a.k1 k1, a.k2, a.k3, b.k1, b.k2, b.k3 from ${tbName1} a left
outer join ${tbName2} b
+ sql"""select a.k1 k, a.k2, a.k3, b.k1, b.k2, b.k3 from ${tbName1} a full
outer join ${tbName2} b
+ on a.k1 = b.k1 and a.k2 > b.k2 order by isnull(k), 1, 2, 3, 4, 5
limit 65535"""
+ sql"""select a.k1 k, a.k2, a.k3, b.k1, b.k2, b.k3 from ${tbName1} a left
outer join ${tbName2} b
on a.k1 = b.k1 and a.k2 > b.k2 union (select a.k1, a.k2, a.k3,
b.k1, b.k2, b.k3
from ${tbName1} a right outer join ${tbName2} b on a.k1 = b.k1
and a.k2 > b.k2)
- order by isnull(k1), 1, 2, 3, 4, 5 limit 65535"""
+ order by isnull(k), 1, 2, 3, 4, 5 limit 65535"""
sql"""select count(*) from ${tbName1} a full outer join ${tbName2} b on
a.k2 = b.k2 and a.k1 > 0
full outer join ${tbName3} c on a.k3 = c.k3 and b.k1 = c.k1 and
c.k3 > 0"""
sql"""select count(*) from ((select a.k1 as k1, b.k1 as k2, a.k2 as k3,
b.k2 as k4, a.k3 as k5, b.k3 as k6, c.k1 as k7, c.k2 as k8, c.k3 as k9 from
${tbName1} a
diff --git a/regression-test/suites/nereids_p0/union/test_union.groovy
b/regression-test/suites/nereids_p0/union/test_union.groovy
index 54810f141a..ec2ab9b945 100644
--- a/regression-test/suites/nereids_p0/union/test_union.groovy
+++ b/regression-test/suites/nereids_p0/union/test_union.groovy
@@ -205,7 +205,6 @@ suite("test_union") {
sql "(select k1, k1 from ${tbName2}) union (select k2, 1 from
${tbName1}) order by k1"
check{result, exception, startTime, endTime ->
assertTrue(exception != null)
- logger.info(exception.message)
}
}
test {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]