Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-5337

UnionPullUpConstantsRule produces an invalid plan when pulling up constants for nullable fields

    XMLWordPrintableJSON

Details

    Description

      The rule pulls up constants without checking/adjusting nullability to match that of the field type.

      Here is the stack-trace when a nullable type is involved:

      java.lang.AssertionError: Cannot add expression of different type to set:
      set type is RecordType(INTEGER DEPTNO, VARCHAR(20) ENAME) NOT NULL
      expression type is RecordType(INTEGER NOT NULL DEPTNO, VARCHAR(20) ENAME) NOT NULL
      set is rel#37:LogicalUnion.(input#0=HepRelVertex#33,input#1=HepRelVertex#33,all=true)
      expression is LogicalProject(DEPTNO=[1], ENAME=[$0])
        LogicalUnion(all=[true])
          LogicalProject(ENAME=[$1])
            LogicalProject(DEPTNO=[$1], ENAME=[$0])
              LogicalFilter(condition=[=($1, 1)])
                LogicalProject(ENAME=[$1], DEPTNO=[$7])
                  LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
          LogicalProject(ENAME=[$1])
            LogicalProject(DEPTNO=[$1], ENAME=[$0])
              LogicalFilter(condition=[=($1, 1)])
                LogicalProject(ENAME=[$1], DEPTNO=[$7])
                  LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])    at org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
          at org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
          at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
          at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
          at org.apache.calcite.rel.rules.UnionPullUpConstantsRule.onMatch(UnionPullUpConstantsRule.java:138)
          at org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:337)
          at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:556)
          at org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:420)
          at org.apache.calcite.plan.hep.HepPlanner.executeRuleInstance(HepPlanner.java:243)
          at org.apache.calcite.plan.hep.HepInstruction$RuleInstance$State.execute(HepInstruction.java:178)
          at org.apache.calcite.plan.hep.HepPlanner.lambda$executeProgram$0(HepPlanner.java:211)
          at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:422)
          at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:210)
          at org.apache.calcite.plan.hep.HepProgram$State.execute(HepProgram.java:118)
          at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:205)
          at org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:191)
          at org.apache.calcite.test.RelOptFixture.checkPlanning(RelOptFixture.java:378)
          at org.apache.calcite.test.RelOptFixture.check(RelOptFixture.java:330)
          at org.apache.calcite.test.RelOptFixture.check(RelOptFixture.java:314)
          at org.apache.calcite.test.RelOptRulesTest.testPullConstantThroughUnion(RelOptRulesTest.java:3745)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
          at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
          at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
          at org.junit.jupiter.engine.extension.TimeoutInvocation.proceed(TimeoutInvocation.java:46)
          at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
          at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
          at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
          at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
          at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
          at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
          at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
          at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
          at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
          at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
          at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
          at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
          at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
          at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
          at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
          at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
          at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
          at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
          at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
          at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService.invokeAll(ForkJoinPoolHierarchicalTestExecutorService.java:129)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
          at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
          at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
          at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
          at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:185)
          at java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
          at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
          at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
          at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
          at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)

       

      The stack-trace above can be obtained by adding the following test to RelOptRulesTest:

      @Test void testPullConstantThroughUnionForNullableField() {
        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();
      } 

      Attachments

        Issue Links

          Activity

            People

              asolimando Alessandro Solimando
              asolimando Alessandro Solimando
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m