Search in sources :

Example 1 with JdbcConvention

use of org.apache.calcite.adapter.jdbc.JdbcConvention 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)

Example 2 with JdbcConvention

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

the class PlannerTest method testPlanTransformWithDiffRuleSetAndConvention.

/**
 * Unit test that calls {@link Planner#transform} twice,
 * with different rule sets, with different conventions.
 *
 * <p>{@link org.apache.calcite.adapter.jdbc.JdbcConvention} is different
 * from the typical convention in that it is not a singleton. Switching to
 * a different instance causes problems unless planner state is wiped clean
 * between calls to {@link Planner#transform}.
 */
@Test
public void testPlanTransformWithDiffRuleSetAndConvention() throws Exception {
    Program program0 = Programs.ofRules(FilterMergeRule.INSTANCE, EnumerableRules.ENUMERABLE_FILTER_RULE, EnumerableRules.ENUMERABLE_PROJECT_RULE);
    JdbcConvention out = new JdbcConvention(null, null, "myjdbc");
    Program program1 = Programs.ofRules(new MockJdbcProjectRule(out), new MockJdbcTableRule(out));
    Planner planner = getPlanner(null, program0, program1);
    SqlNode parse = planner.parse("select T1.\"name\" from \"emps\" as T1 ");
    SqlNode validate = planner.validate(parse);
    RelNode convert = planner.rel(validate).project();
    RelTraitSet traitSet0 = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
    RelTraitSet traitSet1 = planner.getEmptyTraitSet().replace(out);
    RelNode transform = planner.transform(0, traitSet0, convert);
    RelNode transform2 = planner.transform(1, traitSet1, transform);
    assertThat(toString(transform2), equalTo("JdbcProject(name=[$2])\n" + "  MockJdbcTableScan(table=[[hr, emps]])\n"));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) JdbcConvention(org.apache.calcite.adapter.jdbc.JdbcConvention) SqlNode(org.apache.calcite.sql.SqlNode) Test(org.junit.Test)

Aggregations

JdbcConvention (org.apache.calcite.adapter.jdbc.JdbcConvention)2 ArrayList (java.util.ArrayList)1 PhysType (org.apache.calcite.adapter.enumerable.PhysType)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 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 RelTraitSet (org.apache.calcite.plan.RelTraitSet)1 RelNode (org.apache.calcite.rel.RelNode)1 SqlNode (org.apache.calcite.sql.SqlNode)1 Test (org.junit.Test)1