我开始使用方解石。我想将 a 翻译SqlNode
成RelNode
,但由于类型不匹配的断言错误而失败:
ref:
CHAR(1) NOT NULL
input:
CHAR(1) NOT NULL
代码很简单:
private static final String sql = "select u.name as user_name\n" +
"from users u\n" +
"where u.age > 30";
我正在尝试解析-> 验证-> 转换此 sql。
SchemaPlus rootSchema = Frameworks.createRootSchema(true);
rootSchema.add("USERS", new AbstractTable() { //note: add a table
@Override
public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
RelDataTypeFactory.Builder builder = typeFactory.builder();
builder.add("ID", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.INTEGER));
builder.add("NAME", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.CHAR));
builder.add("AGE", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.INTEGER));
return builder.build();
}
});
rootSchema.add("JOBS", new AbstractTable() {
@Override
public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
RelDataTypeFactory.Builder builder = typeFactory.builder();
builder.add("ID", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.INTEGER));
builder.add("NAME", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.CHAR));
builder.add("COMPANY", new BasicSqlType(new RelDataTypeSystemImpl() {}, SqlTypeName.CHAR));
return builder.build();
}
});
FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder()
.defaultSchema(rootSchema)
.build();
final Planner planner = Frameworks.getPlanner(frameworkConfig);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
planner.rel(validate);
当我运行代码时,行planner.rel(validate)
出现错误并说:
java.lang.AssertionError: type mismatch:
ref:
CHAR(1) NOT NULL
input:
CHAR(1) NOT NULL
at org.apache.calcite.util.Litmus$1.fail(Litmus.java:31)
at org.apache.calcite.plan.RelOptUtil.eq(RelOptUtil.java:2000)
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.rel.core.Project.isValid(Project.java:192)
at org.apache.calcite.rel.core.Project.<init>(Project.java:83)
at org.apache.calcite.rel.logical.LogicalProject.<init>(LogicalProject.java:62)
at org.apache.calcite.rel.logical.LogicalProject.create(LogicalProject.java:112)
at org.apache.calcite.rel.logical.LogicalProject.create(LogicalProject.java:100)
at org.apache.calcite.rel.core.RelFactories$ProjectFactoryImpl.createProject(RelFactories.java:158)
at org.apache.calcite.tools.RelBuilder.project(RelBuilder.java:1414)
at org.apache.calcite.tools.RelBuilder.projectNamed(RelBuilder.java:1470)
at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectList(SqlToRelConverter.java:3955)
at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelectImpl(SqlToRelConverter.java:670)
at org.apache.calcite.sql2rel.SqlToRelConverter.convertSelect(SqlToRelConverter.java:627)
at org.apache.calcite.sql2rel.SqlToRelConverter.convertQueryRecursive(SqlToRelConverter.java:3181)
at org.apache.calcite.sql2rel.SqlToRelConverter.convertQuery(SqlToRelConverter.java:563)
at org.apache.calcite.prepare.PlannerImpl.rel(PlannerImpl.java:254)
at org.apache.calcite.test.MyTest.firstTest(MyTest.java:141)
实际上,如果我在user.id
或user.age
(INTEGER
类型)上进行项目,SqlNode
转换工作正常。但是,当我在user.name
( CHAR
type) 上进行投影时,转换会引发错误。
感谢您分享一些想法。