Search in sources :

Example 26 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class SqlUserDefinedTableMacro method coerce.

private static Object coerce(Object o, RelDataType type) {
    if (o == null) {
        return null;
    }
    if (!(type instanceof RelDataTypeFactoryImpl.JavaType)) {
        return null;
    }
    final RelDataTypeFactoryImpl.JavaType javaType = (RelDataTypeFactoryImpl.JavaType) type;
    final Class clazz = javaType.getJavaClass();
    // noinspection unchecked
    if (clazz.isAssignableFrom(o.getClass())) {
        return o;
    }
    if (clazz == String.class && o instanceof NlsString) {
        return ((NlsString) o).getValue();
    }
    // We need optimization here for constant folding.
    // Not all the expressions can be interpreted (e.g. ternary), so
    // we rely on optimization capabilities to fold non-interpretable
    // expressions.
    BlockBuilder bb = new BlockBuilder();
    final Expression expr = RexToLixTranslator.convert(Expressions.constant(o), clazz);
    bb.add(Expressions.return_(null, expr));
    final FunctionExpression convert = Expressions.lambda(bb.toBlock(), Collections.<ParameterExpression>emptyList());
    return convert.compile().dynamicInvoke();
}
Also used : FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) Expression(org.apache.calcite.linq4j.tree.Expression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelDataTypeFactoryImpl(org.apache.calcite.rel.type.RelDataTypeFactoryImpl) NlsString(org.apache.calcite.util.NlsString) NlsString(org.apache.calcite.util.NlsString) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 27 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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 28 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class SparkToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generate:
    // RDD rdd = ...;
    // return SparkRuntime.asEnumerable(rdd);
    final BlockBuilder list = new BlockBuilder();
    final SparkRel child = (SparkRel) getInput();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM);
    SparkRel.Implementor sparkImplementor = new SparkImplementorImpl(implementor);
    final SparkRel.Result result = child.implementSpark(sparkImplementor);
    final Expression rdd = list.append("rdd", result.block);
    final Expression enumerable = list.append("enumerable", Expressions.call(SparkMethod.AS_ENUMERABLE.method, rdd));
    list.add(Expressions.return_(null, enumerable));
    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)

Example 29 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class MongoToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generates a call to "find" or "aggregate", depending upon whether
    // an aggregate is present.
    // 
    // ((MongoTable) schema.getTable("zips")).find(
    // "{state: 'CA'}",
    // "{city: 1, zipcode: 1}")
    // 
    // ((MongoTable) schema.getTable("zips")).aggregate(
    // "{$filter: {state: 'CA'}}",
    // "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: "$pop"}}")
    final BlockBuilder list = new BlockBuilder();
    final MongoRel.Implementor mongoImplementor = new MongoRel.Implementor();
    mongoImplementor.visitChild(0, getInput());
    int aggCount = 0;
    int findCount = 0;
    String project = null;
    String filter = null;
    for (Pair<String, String> op : mongoImplementor.list) {
        if (op.left == null) {
            ++aggCount;
        }
        if (op.right.startsWith("{$match:")) {
            filter = op.left;
            ++findCount;
        }
        if (op.right.startsWith("{$project:")) {
            project = op.left;
            ++findCount;
        }
    }
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(MongoRules.mongoFieldNames(rowType), new AbstractList<Class>() {

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

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    final Expression table = list.append("table", mongoImplementor.table.getExpression(MongoTable.MongoQueryable.class));
    List<String> opList = Pair.right(mongoImplementor.list);
    final Expression ops = list.append("ops", constantArrayList(opList, String.class));
    Expression enumerable = list.append("enumerable", Expressions.call(table, MongoMethod.MONGO_QUERYABLE_AGGREGATE.method, fields, ops));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Mongo: " + opList);
    }
    Hook.QUERY_PLAN.run(opList);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 30 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class RexExecutorImpl method reduce.

/**
 * Do constant reduction using generated code.
 */
public void reduce(RexBuilder rexBuilder, List<RexNode> constExps, List<RexNode> reducedValues) {
    final String code = compile(rexBuilder, constExps, new RexToLixTranslator.InputGetter() {

        public Expression field(BlockBuilder list, int index, Type storageType) {
            throw new UnsupportedOperationException();
        }
    });
    final RexExecutable executable = new RexExecutable(code, constExps);
    executable.setDataContext(dataContext);
    executable.reduce(rexBuilder, constExps, reducedValues);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) InputGetter(org.apache.calcite.adapter.enumerable.RexToLixTranslator.InputGetter) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) IndexExpression(org.apache.calcite.linq4j.tree.IndexExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)67 Expression (org.apache.calcite.linq4j.tree.Expression)56 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)40 RelDataType (org.apache.calcite.rel.type.RelDataType)15 PhysType (org.apache.calcite.adapter.enumerable.PhysType)14 ArrayList (java.util.ArrayList)12 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 Test (org.junit.Test)10 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)9 Type (java.lang.reflect.Type)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)6 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)6 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)5 RexNode (org.apache.calcite.rex.RexNode)5 EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)4 Function1 (org.apache.calcite.linq4j.function.Function1)4