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
为什么会这样?
答案 0 :(得分:1)
RelBuilder
正在做的事情是:建立一个没有条件的连接。这类似于你可能写的方式
SELECT ...
FROM Emp, Dept
然后在WHERE
子句中指定连接条件:
WHERE Emp.deptno = Dept.deptno
RelBuilder
不会尝试进行优化(除了一些微优化,例如删除琐碎的Project
)。将Filter
折叠到Join
上是FilterIntoJoinRule
的工作,并且将在查询优化阶段适时发生。
RelToSqlConverter
中的错误是known issue,将在下一个版本(1.13.0)中修复。