[Apache Calcite]如何在Apache Calcite里把关系表达式转换成SQL

512 阅读1分钟

 

使用SqlImplementor.Context实现类转换Rex表达式

org.apache.calcite.rel.rel2sql.SqlImplementor.BaseContext

org.apache.calcite.rel.rel2sql.SqlImplementor.AliasContext

org.apache.calcite.rel.rel2sql.SqlImplementor.MatchRecognizeContext

org.apache.calcite.rel.rel2sql.SqlImplementor.SimpleContext

SqlImplementor有成员方法构造上述对象
 SqlImplementor.Context context = new SqlImplementor.Context(MysqlSqlDialect.DEFAULT, JdbcTable.this.rowSignature.getColumnCount()) {
            @Override
            public SqlNode field(int ordinal) {
                String fieldName = xxxx.get(ordinal);
                return new SqlIdentifier(ImmutableList.of(schemaName, tableName, fieldName),
                        SqlImplementor.POS);
            }
        };
        SqlNode sqlNode = context.toSql(null, rexNode);

使用RexToSqlNodeConverter

        final RexSqlStandardConvertletTable convertletTable = new RexSqlStandardConvertletTable();
        RexToSqlNodeConverter converter = new RexToSqlNodeConverterImpl(convertletTable);
        SqlNode sqlNode = converter.convertNode(rexNode);
只能转换三种类型
RexCall
RexLiteral
RexInputRef

使用RelToSqlConverter

RelToSqlConverter relToSqlConverter = new RelToSqlConverter(MysqlSqlDialect.DEFAULT);
SqlImplementor.Result visit = relToSqlConverter.visitChild(0,logicalTableScan);
SqlNode sqlNode = visit.asStatement();

relToSqlConverter有多种访问访问

SqlImplementor.Result既有多种输出方法