Description
SqlToRelConverter generates wrong ordinal when flattening multiple input records during converting to RelNode. Issue can be reproduced as follows:
MockTable deptNestedTable = MockTable.create(this, salesSchema, "DEPT_NESTED", false, 4); deptNestedTable.addColumn("DEPTNO", f.intType, true); deptNestedTable.addColumn("NAME", f.varchar10Type); deptNestedTable.addColumn("SKILLRECORD", f.skillRecordType); deptNestedTable.addColumn("EMPLOYEES", f.empListType); registerTable(deptNestedTable);
Run the following test:
@Test public void testArrayOfRecord() { sql("select employees[1] from dept_nested").ok(); }
The following error gets thrown when run:
java.lang.AssertionError: type mismatch: ref: RecordType(INTEGER NOT NULL EMPNO, VARCHAR(10) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL ENAME, RecordType(VARCHAR(10) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL TYPE, VARCHAR(20) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL DESC) NOT NULL ARRAY NOT NULL SKILLS) NOT NULL ARRAY NOT NULL input: VARCHAR(20) CHARACTER SET "ISO-8859-1" COLLATE "ISO-8859-1$en_US$primary" NOT NULL at org.apache.calcite.util.Litmus$1.fail(Litmus.java:31) at org.apache.calcite.plan.RelOptUtil.eq(RelOptUtil.java:1838) at org.apache.calcite.rex.RexChecker.visitInputRef(RexChecker.java:125) at org.apache.calcite.rex.RexChecker.visitInputRef(RexChecker.java:57) at org.apache.calcite.rex.RexInputRef.accept(RexInputRef.java:112) at org.apache.calcite.rex.RexChecker.visitCall(RexChecker.java:140) at org.apache.calcite.rex.RexChecker.visitCall(RexChecker.java:57) at org.apache.calcite.rex.RexCall.accept(RexCall.java:107) at org.apache.calcite.rex.RexVisitorImpl.visitFieldAccess(RexVisitorImpl.java:98) at org.apache.calcite.rex.RexChecker.visitFieldAccess(RexChecker.java:149) at org.apache.calcite.rex.RexChecker.visitFieldAccess(RexChecker.java:57) at org.apache.calcite.rex.RexFieldAccess.accept(RexFieldAccess.java:81) at org.apache.calcite.rel.core.Project.isValid(Project.java:187) at org.apache.calcite.rel.core.Project.<init>(Project.java:84) at org.apache.calcite.rel.logical.LogicalProject.<init>(LogicalProject.java:65) at org.apache.calcite.rel.logical.LogicalProject.create(LogicalProject.java:120) at org.apache.calcite.rel.logical.LogicalProject.create(LogicalProject.java:103) at org.apache.calcite.rel.core.RelFactories$ProjectFactoryImpl.createProject(RelFactories.java:127) at org.apache.calcite.tools.RelBuilder.project(RelBuilder.java:1064) at org.apache.calcite.plan.RelOptUtil.createProject(RelOptUtil.java:2956) at org.apache.calcite.plan.RelOptUtil.createProject(RelOptUtil.java:2873) at org.apache.calcite.sql2rel.RelStructuredTypeFlattener.rewriteRel(RelStructuredTypeFlattener.java:477) 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.apache.calcite.util.ReflectUtil.invokeVisitorInternal(ReflectUtil.java:257) at org.apache.calcite.util.ReflectUtil.invokeVisitor(ReflectUtil.java:214) at org.apache.calcite.util.ReflectUtil$1.invokeVisitor(ReflectUtil.java:464) at org.apache.calcite.sql2rel.RelStructuredTypeFlattener$RewriteRelVisitor.visit(RelStructuredTypeFlattener.java:721) at org.apache.calcite.sql2rel.RelStructuredTypeFlattener.rewrite(RelStructuredTypeFlattener.java:177) at org.apache.calcite.sql2rel.SqlToRelConverter.flattenTypes(SqlToRelConverter.java:462) at org.apache.calcite.test.SqlToRelTestBase$TesterImpl.convertSqlToRel(SqlToRelTestBase.java:585) at org.apache.calcite.test.SqlToRelTestBase$TesterImpl.assertConvertsTo(SqlToRelTestBase.java:690) at org.apache.calcite.test.SqlToRelConverterTest$Sql.convertsTo(SqlToRelConverterTest.java:2784) at org.apache.calcite.test.SqlToRelConverterTest$Sql.ok(SqlToRelConverterTest.java:2776) at org.apache.calcite.test.SqlToRelConverterTest.testArrayOfRecord(SqlToRelConverterTest.java:1059) 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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Below is a brief analysis:
Calcite is trying to remove all structured type in the plan right below, so optimizer and codegen rules never have to deal with structured types.
LogicalProject(EXPR$0=[ITEM($3, 1)]) LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]])
First, it flatten the LogicalTableScan, and generate the following:
LogicalProject(DEPTNO=[$0], NAME=[$1], TYPE=[$2.TYPE], DESC=[$2.DESC], EMPLOYEES=[$3]) LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]])
Then it tries to flatten
"LogicalProject(EXPR$0=[ITEM($3, 1)])"
, and generate the following:
LogicalProject(EXPR$0$0=[ITEM($3, 1).EMPNO], EXPR$0$1=[ITEM($3, 1).ENAME], EXPR$0$2=[ITEM($3, 1).SKILLS])
However, when it combines the 2 flattening results, it did not correctly adjust the ordinal post-flattening, which should be $4 now, not $3. So this cause the exception since it is a type mismatch.
Attachments
Issue Links
- links to