@Override
public void onMatch(RelOptRuleCall call) {
LogicalProject project = call.rel(0);
JsonTableScan scan = call.rel(1);
Integer[] fields =
getProjectFields(project.getProjects());
if(fields == null || fields.length == 0)
return ;
for(int i = 0; i < fields.length; i++) {
if(fields[i] == null)
return;
}
call.transformTo(
new JsonTableScan(scan.getCluster(),
scan.getTable(), fields)
);
}
the onMatch method should check fields data, if they are not correct,
then should not call call.transformTo.
test1_2 now passes.
------------------ Original ------------------
From:
"xiaobo "
<[email protected]>;
Date: Mon, Mar 14, 2022 10:03 AM
To: "dev"<[email protected]>;
Subject: re:applying the wrong rule for calculated columns
after we change the String[] fields to Integer[] fields , the problem still
occurs.
------------
our runnable test is at
https://github.com/guxiaobo/calcite-json-adapter/blob/main/src/test/java/org/apache/calcite/adapter/json/test/JsonSchemaTest.java
-------
@Test
public void test1_2()throws SQLException{
Map<string, list<jsonobject="">&gt; map = makeJsonMap();
Schema schema = new JsonSchema&lt;&gt;("js", map);
CalciteConnection conn = this.openConn1(schema, "js");
System.out.println("c1 + c11 = " + exeGetLong(conn, "select c1 + c11 from
js.t1"));
conn.close();
}
-------
java.sql.SQLException: Error while executing SQL "select c1 + c11 from js.t1":
Error while applying rule JsonProjectTableScanRule, args
[rel#11:LogicalProject.NONE.[](input=RelSubset#10,exprs=[+($9, $3)]),
rel#1:JsonTableScan.ENUMERABLE.[](table=[js, t1])]
at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
at
org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:163)
at
org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:227)
at org.apache.calcite.adapter.json.test.BaseTest.exeGetLong(BaseTest.java:37)
at
org.apache.calcite.adapter.json.test.JsonSchemaTest.test1_2(JsonSchemaTest.java:76)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at
org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.RuntimeException: Error while applying rule
JsonProjectTableScanRule, args
[rel#11:LogicalProject.NONE.[](input=RelSubset#10,exprs=[+($9, $3)]),
rel#1:JsonTableScan.ENUMERABLE.[](table=[js, t1])]
at
org.apache.calcite.plan.volcano.VolcanoRuleCall.onMatch(VolcanoRuleCall.java:250)
at
org.apache.calcite.plan.volcano.IterativeRuleDriver.drive(IterativeRuleDriver.java:59)
at
org.apache.calcite.plan.volcano.VolcanoPlanner.findBestExp(VolcanoPlanner.java:523)
at org.apache.calcite.tools.Programs.lambda$standard$3(Programs.java:276)
at org.apache.calcite.tools.Programs$SequenceProgram.run(Programs.java:336)
at org.apache.calcite.prepare.Prepare.optimize(Prepare.java:177)
at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:312)
at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:220)
at
org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:647)
at
org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:513)
at
org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:483)
at
org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:249)
at
org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:623)
at
org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:674)
at
org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:156)
... 29 more
Caused by: java.lang.RuntimeException: Error occurred while applying rule
JsonProjectTableScanRule
at
org.apache.calcite.plan.volcano.VolcanoRuleCall.transformTo(VolcanoRuleCall.java:157)
at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
at
org.apache.calcite.adapter.json.JsonProjectTableScanRule.onMatch(JsonProjectTableScanRule.java:30)
at
org.apache.calcite.plan.volcano.VolcanoRuleCall.onMatch(VolcanoRuleCall.java:223)
... 43 more
Caused by: java.lang.NullPointerException
at
org.apache.calcite.adapter.json.JsonTableScan.lambda$0(JsonTableScan.java:45)
at
java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at
java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
at
org.apache.calcite.adapter.json.JsonTableScan.deriveRowType(JsonTableScan.java:45)
at org.apache.calcite.rel.AbstractRelNode.getRowType(AbstractRelNode.java:173)
at
org.apache.calcite.plan.volcano.VolcanoPlanner.register(VolcanoPlanner.java:587)
at
org.apache.calcite.plan.volcano.VolcanoPlanner.ensureRegistered(VolcanoPlanner.java:613)
at
org.apache.calcite.plan.volcano.VolcanoRuleCall.transformTo(VolcanoRuleCall.java:144)
... 47 more</string,>