Search in sources :

Example 1 with JdbcRel

use of org.apache.calcite.adapter.jdbc.JdbcRel in project calcite by apache.

the class JdbcToSparkConverter method implementSpark.

public SparkRel.Result implementSpark(SparkRel.Implementor implementor) {
    // Generate:
    // ResultSetEnumerable.of(schema.getDataSource(), "select ...")
    final BlockBuilder list = new BlockBuilder();
    final JdbcRel child = (JdbcRel) getInput();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
    final JdbcConvention jdbcConvention = (JdbcConvention) child.getConvention();
    String sql = generateSql(jdbcConvention.dialect);
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("[" + sql + "]");
    }
    final Expression sqlLiteral = list.append("sql", Expressions.constant(sql));
    final List<Primitive> primitives = new ArrayList<Primitive>();
    for (int i = 0; i < getRowType().getFieldCount(); i++) {
        final Primitive primitive = Primitive.ofBoxOr(physType.fieldClass(i));
        primitives.add(primitive != null ? primitive : Primitive.OTHER);
    }
    final Expression primitivesLiteral = list.append("primitives", Expressions.constant(primitives.toArray(new Primitive[primitives.size()])));
    final Expression enumerable = list.append("enumerable", Expressions.call(BuiltInMethod.RESULT_SET_ENUMERABLE_OF.method, Expressions.call(Expressions.convert_(jdbcConvention.expression, JdbcSchema.class), BuiltInMethod.JDBC_SCHEMA_DATA_SOURCE.method), sqlLiteral, primitivesLiteral));
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Primitive(org.apache.calcite.linq4j.tree.Primitive) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Expression(org.apache.calcite.linq4j.tree.Expression) ArrayList(java.util.ArrayList) JdbcRel(org.apache.calcite.adapter.jdbc.JdbcRel) JdbcConvention(org.apache.calcite.adapter.jdbc.JdbcConvention) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

ArrayList (java.util.ArrayList)1 PhysType (org.apache.calcite.adapter.enumerable.PhysType)1 JdbcConvention (org.apache.calcite.adapter.jdbc.JdbcConvention)1 JdbcRel (org.apache.calcite.adapter.jdbc.JdbcRel)1 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 Expression (org.apache.calcite.linq4j.tree.Expression)1 Primitive (org.apache.calcite.linq4j.tree.Primitive)1