This is an automated email from the ASF dual-hosted git repository.
asolimando pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 2ddf14e099 [CALCITE-5337] UnionPullUpConstantsRule produces an invalid
plan when pulling up constants for nullable fields
2ddf14e099 is described below
commit 2ddf14e09942819a32c6ae1196978034ba148ada
Author: Alessandro Solimando <[email protected]>
AuthorDate: Tue Oct 18 12:48:26 2022 +0200
[CALCITE-5337] UnionPullUpConstantsRule produces an invalid plan when
pulling up constants for nullable fields
---
.../rel/rules/UnionPullUpConstantsRule.java | 6 +++-
.../org/apache/calcite/test/RelOptRulesTest.java | 11 +++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 34 ++++++++++++++++++++++
3 files changed, 50 insertions(+), 1 deletion(-)
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/UnionPullUpConstantsRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/UnionPullUpConstantsRule.java
index 1e16f56643..e1d994571d 100644
---
a/core/src/main/java/org/apache/calcite/rel/rules/UnionPullUpConstantsRule.java
+++
b/core/src/main/java/org/apache/calcite/rel/rules/UnionPullUpConstantsRule.java
@@ -95,7 +95,11 @@ public class UnionPullUpConstantsRule
for (RelDataTypeField field : fields) {
final RexNode constant = constants.get(field.getIndex());
if (constant != null) {
- topChildExprs.add(constant);
+ if (constant.getType().equals(field.getType())) {
+ topChildExprs.add(constant);
+ } else {
+ topChildExprs.add(rexBuilder.makeCast(field.getType(), constant,
true));
+ }
topChildExprsFields.add(field.getName());
} else {
final RexNode expr = rexBuilder.makeInputRef(union, field.getIndex());
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 97de092806..4885460658 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -3941,6 +3941,17 @@ class RelOptRulesTest extends RelOptTestBase {
.check();
}
+ @Test void testPullConstantThroughUnionSameTypeNullableField() {
+ final String sql = "select deptno, ename from empnullables where deptno =
1\n"
+ + "union all\n"
+ + "select deptno, ename from empnullables where deptno = 1";
+ sql(sql)
+ .withTrim(true)
+ .withRule(CoreRules.UNION_PULL_UP_CONSTANTS,
+ CoreRules.PROJECT_MERGE)
+ .check();
+ }
+
@Test void testAggregateProjectMerge() {
final String sql = "select x, sum(z), y from (\n"
+ " select deptno as x, empno as y, sal as z, sal * 2 as zz\n"
diff --git
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index 863a8f1db2..a546d276c7 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -7487,6 +7487,40 @@ LogicalProject(EXPR$0=[2], EXPR$1=[3])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(EXPR$0=[2])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testPullConstantThroughUnionSameTypeNullableField">
+ <Resource name="sql">
+ <![CDATA[select deptno, ename from empnullables where deptno = 1
+union all
+select deptno, ename from empnullables where deptno = 1]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalUnion(all=[true])
+ LogicalProject(DEPTNO=[$1], ENAME=[$0])
+ LogicalFilter(condition=[=($1, 1)])
+ LogicalProject(ENAME=[$1], DEPTNO=[$7])
+ LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
+ LogicalProject(DEPTNO=[$1], ENAME=[$0])
+ LogicalFilter(condition=[=($1, 1)])
+ LogicalProject(ENAME=[$1], DEPTNO=[$7])
+ LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[CAST(1):INTEGER], ENAME=[$0])
+ LogicalUnion(all=[true])
+ LogicalProject(ENAME=[$0])
+ LogicalFilter(condition=[=($1, 1)])
+ LogicalProject(ENAME=[$1], DEPTNO=[$7])
+ LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
+ LogicalProject(ENAME=[$0])
+ LogicalFilter(condition=[=($1, 1)])
+ LogicalProject(ENAME=[$1], DEPTNO=[$7])
+ LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
]]>
</Resource>
</TestCase>