前言
在使用Calcite查询数据时通常会用到这些代码获取schema
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
但是并不是使用Calcite的场景都需要这样用,例如作为作为查询优化器或解析器使用,就不需要通过Caclite JDBC驱动来访问数据源。
查询优化器或解析器场景不使用驱动
如果你只是使用 Calcite 来进行 SQL 解析、验证、优化等操作,而不需要通过 JDBC 接口来访问数据源,那么你并不需要使用 JDBC 连接代码。你可以直接使用 Calcite 提供的 API 来构建和操作查询计划。例如,使用 Frameworks 工具类来创建 Calcite 的环境和配置。解析器解析Sql的场景比较常见,下文不进行演示。
@Test
public void testSqlToRelNode() throws Exception{
Properties info = new Properties();
SchemaPlus rootSchema = Frameworks.createRootSchema(true);
Schema schema = new AbstractSchema() {
};
rootSchema.add("MY_SCHEMA", schema);
Table yourTable = new AbstractTable() {
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
// 如果要动态分析表,那么就自己去创建
return typeFactory.builder()
.add("id", typeFactory.createJavaType(int.class))
.add("name", typeFactory.createJavaType(String.class))
.add("age", typeFactory.createJavaType(int.class))
.build();
}
};
Table department_table = new AbstractTable() {
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
// 如果要动态分析表,那么就自己去创建
return typeFactory.builder()
.add("id", typeFactory.createJavaType(int.class))
.add("department", typeFactory.create