Search in sources :

Example 11 with PhysType

use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.

the class JdbcToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generate:
    // ResultSetEnumerable.of(schema.getDataSource(), "select ...")
    final BlockBuilder builder0 = new BlockBuilder(false);
    final JdbcRel child = (JdbcRel) getInput();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.prefer(JavaRowFormat.CUSTOM));
    final JdbcConvention jdbcConvention = (JdbcConvention) child.getConvention();
    String sql = generateSql(jdbcConvention.dialect);
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("[" + sql + "]");
    }
    Hook.QUERY_PLAN.run(sql);
    final Expression sql_ = builder0.append("sql", Expressions.constant(sql));
    final int fieldCount = getRowType().getFieldCount();
    BlockBuilder builder = new BlockBuilder();
    final ParameterExpression resultSet_ = Expressions.parameter(Modifier.FINAL, ResultSet.class, builder.newName("resultSet"));
    final SqlDialect.CalendarPolicy calendarPolicy = jdbcConvention.dialect.getCalendarPolicy();
    final Expression calendar_;
    switch(calendarPolicy) {
        case LOCAL:
            calendar_ = builder0.append("calendar", Expressions.call(Calendar.class, "getInstance", getTimeZoneExpression(implementor)));
            break;
        default:
            calendar_ = null;
    }
    if (fieldCount == 1) {
        final ParameterExpression value_ = Expressions.parameter(Object.class, builder.newName("value"));
        builder.add(Expressions.declare(Modifier.FINAL, value_, null));
        generateGet(implementor, physType, builder, resultSet_, 0, value_, calendar_, calendarPolicy);
        builder.add(Expressions.return_(null, value_));
    } else {
        final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(fieldCount)));
        for (int i = 0; i < fieldCount; i++) {
            generateGet(implementor, physType, builder, resultSet_, i, Expressions.arrayIndex(values_, Expressions.constant(i)), calendar_, calendarPolicy);
        }
        builder.add(Expressions.return_(null, values_));
    }
    final ParameterExpression e_ = Expressions.parameter(SQLException.class, builder.newName("e"));
    final Expression rowBuilderFactory_ = builder0.append("rowBuilderFactory", Expressions.lambda(Expressions.block(Expressions.return_(null, Expressions.lambda(Expressions.block(Expressions.tryCatch(builder.toBlock(), Expressions.catch_(e_, Expressions.throw_(Expressions.new_(RuntimeException.class, e_)))))))), resultSet_));
    final Expression enumerable = builder0.append("enumerable", Expressions.call(BuiltInMethod.RESULT_SET_ENUMERABLE_OF.method, Expressions.call(Schemas.unwrap(jdbcConvention.expression, JdbcSchema.class), BuiltInMethod.JDBC_SCHEMA_DATA_SOURCE.method), sql_, rowBuilderFactory_));
    builder0.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, builder0.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) SqlDialect(org.apache.calcite.sql.SqlDialect) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 12 with PhysType

use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.

the class EnumerableToSparkConverter method implementSpark.

public Result implementSpark(Implementor implementor) {
    // Generate:
    // Enumerable source = ...;
    // return SparkRuntime.createRdd(sparkContext, source);
    final BlockBuilder list = new BlockBuilder();
    final EnumerableRel child = (EnumerableRel) getInput();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
    // TODO:
    final Expression source = null;
    final Expression sparkContext = Expressions.call(SparkMethod.GET_SPARK_CONTEXT.method, implementor.getRootExpression());
    final Expression rdd = list.append("rdd", Expressions.call(SparkMethod.CREATE_RDD.method, sparkContext, source));
    list.add(Expressions.return_(null, rdd));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) EnumerableRel(org.apache.calcite.adapter.enumerable.EnumerableRel)

Example 13 with PhysType

use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.

the class PigToEnumerableConverter method implement.

/**
 * {@inheritDoc}
 *
 * <p>This implementation does not actually execute the associated Pig Latin
 * script and return results. Instead it returns an empty
 * {@link org.apache.calcite.adapter.enumerable.EnumerableRel.Result}
 * in order to allow for testing and verification of every step of query
 * processing up to actual physical execution and result verification.
 *
 * <p>Next step is to invoke Pig from here, likely in local mode, have it
 * store results in a predefined file so they can be read here and returned as
 * a {@code Result} object.
 */
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder list = new BlockBuilder();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    PigRel.Implementor impl = new PigRel.Implementor();
    impl.visitChild(0, getInput());
    // for script validation in tests
    Hook.QUERY_PLAN.run(impl.getScript());
    list.add(Expressions.return_(null, Expressions.call(BuiltInMethod.EMPTY_ENUMERABLE.method)));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 14 with PhysType

use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.

the class GeodeToEnumerableConverter method implement.

/**
 * {@inheritDoc}
 *
 * @param implementor GeodeImplementContext
 */
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // travers all relations form this to the scan leaf
    final GeodeImplementContext geodeImplementContext = new GeodeImplementContext();
    ((GeodeRel) getInput()).implement(geodeImplementContext);
    // PhysType is Enumerable Adapter class that maps SQL types (getRowType)
    // with physical Java types (getJavaTypes())
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final List<Class> physFieldClasses = new AbstractList<Class>() {

        public Class get(int index) {
            return physType.fieldClass(index);
        }

        public int size() {
            return rowType.getFieldCount();
        }
    };
    // Expression meta-program for calling the GeodeTable.GeodeQueryable#query
    // method form the generated code
    final BlockBuilder blockBuilder = new BlockBuilder().append(Expressions.call(geodeImplementContext.table.getExpression(GeodeTable.GeodeQueryable.class), GEODE_QUERY_METHOD, constantArrayList(Pair.zip(geodeFieldNames(rowType), physFieldClasses), Pair.class), // physical fields
    constantArrayList(toListMapPairs(geodeImplementContext.selectFields), Pair.class), // selected fields
    constantArrayList(toListMapPairs(geodeImplementContext.oqlAggregateFunctions), Pair.class), constantArrayList(geodeImplementContext.groupByFields, String.class), constantArrayList(geodeImplementContext.whereClause, String.class), constantArrayList(geodeImplementContext.orderByFields, String.class), Expressions.constant(geodeImplementContext.limitValue)));
    Hook.QUERY_PLAN.run(geodeImplementContext);
    return implementor.result(physType, blockBuilder.toBlock());
}
Also used : AbstractList(java.util.AbstractList) PhysType(org.apache.calcite.adapter.enumerable.PhysType) GeodeImplementContext(org.apache.calcite.adapter.geode.rel.GeodeRel.GeodeImplementContext) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 15 with PhysType

use of org.apache.calcite.adapter.enumerable.PhysType in project calcite by apache.

the class SplunkTableScan method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    Map map = ImmutableMap.builder().put("search", search).put("earliest", Util.first(earliest, "")).put("latest", Util.first(latest, "")).put("fieldList", fieldList).build();
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Splunk: " + map);
    }
    Hook.QUERY_PLAN.run(map);
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferCustom());
    final BlockBuilder builder = new BlockBuilder();
    return implementor.result(physType, builder.append(Expressions.call(table.getExpression(SplunkTable.SplunkTableQueryable.class), METHOD, Expressions.constant(search), Expressions.constant(earliest), Expressions.constant(latest), fieldList == null ? Expressions.constant(null) : constantStringList(fieldList))).toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

PhysType (org.apache.calcite.adapter.enumerable.PhysType)14 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)14 Expression (org.apache.calcite.linq4j.tree.Expression)11 RelDataType (org.apache.calcite.rel.type.RelDataType)5 EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)4 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)4 Function1 (org.apache.calcite.linq4j.function.Function1)4 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)4 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)3 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)3 Pair (org.apache.calcite.util.Pair)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RexProgram (org.apache.calcite.rex.RexProgram)2 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ExecutableExpression (com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression)1 AbstractList (java.util.AbstractList)1 PhysType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.PhysType)1 BlockBuilder (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder)1