加入计划节点,条件= true遇到错误

时间:2017-06-05 02:57:42

标签: apache-calcite

final RelBuilder builder = RelBuilder.create(config().build());
    RelNode root =
        builder.scan("EMP")
            .as("e")
            .scan("EMP")
            .as("m")
            .scan("DEPT")
            .join(JoinRelType.INNER)
            .join(JoinRelType.INNER)
            .filter(
                builder.equals(builder.field("e", "DEPTNO"),
                    builder.field("DEPT", "DEPTNO")),
                builder.equals(builder.field("m", "EMPNO"),
                    builder.field("e", "MGR")))
            .build();
    final String expected = ""
        + "LogicalFilter(condition=[AND(=($7, $16), =($8, $3))])\n"
        + "  LogicalJoin(condition=[true], joinType=[inner])\n"
        + "    LogicalTableScan(table=[[scott, EMP]])\n"
        + "    LogicalJoin(condition=[true], joinType=[inner])\n"
        + "      LogicalTableScan(table=[[scott, EMP]])\n"
        + "      LogicalTableScan(table=[[scott, DEPT]])\n";
    assertThat(str(root), is(expected));

上面的代码来自RelBuilderTest.java。为什么方解中生成LogicalJoin(condition=[true], joinType=[inner])节点?当我使用RelToSqlConverter将RelNode转换为SQL时,会遇到有关filter 'condition=[true]'

的错误
ava.lang.AssertionError: Internal error: While invoking method 'public org.apache.calcite.rel.rel2sql.SqlImplementor$Result org.apache.calcite.rel.rel2sql.RelToSqlConverter.visit(org.apache.calcite.rel.core.Filter)
at org.apache.calcite.util.Util.newInternal(Util.java:792)
at org.apache.calcite.util.ReflectUtil$2.invoke(ReflectUtil.java:535)
at org.apache.calcite.rel.rel2sql.RelToSqlConverter.dispatch(RelToSqlConverter.java:80

为什么会这样?

1 个答案:

答案 0 :(得分:1)

RelBuilder正在做的事情是:建立一个没有条件的连接。这类似于你可能写的方式

SELECT ...
FROM Emp, Dept

然后在WHERE子句中指定连接条件:

WHERE Emp.deptno = Dept.deptno

RelBuilder不会尝试进行优化(除了一些微优化,例如删除琐碎的Project)。将Filter折叠到Join上是FilterIntoJoinRule的工作,并且将在查询优化阶段适时发生。

RelToSqlConverter中的错误是known issue,将在下一个版本(1.13.0)中修复。