Search in sources :

Example 56 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 EnumerableWindow method getBlockBuilderWinAggFrameResultContextFunction.

private Function<BlockBuilder, WinAggFrameResultContext> getBlockBuilderWinAggFrameResultContextFunction(final JavaTypeFactory typeFactory, final Result result, final List<Expression> translatedConstants, final Expression comparator_, final Expression rows_, final ParameterExpression i_, final Expression startX, final Expression endX, final Expression minX, final Expression maxX, final Expression hasRows, final Expression frameRowCount, final Expression partitionRowCount, final DeclarationStatement jDecl, final PhysType inputPhysType) {
    return new Function<BlockBuilder, WinAggFrameResultContext>() {

        public WinAggFrameResultContext apply(final BlockBuilder block) {
            return new WinAggFrameResultContext() {

                public RexToLixTranslator rowTranslator(Expression rowIndex) {
                    Expression row = getRow(rowIndex);
                    final RexToLixTranslator.InputGetter inputGetter = new WindowRelInputGetter(row, inputPhysType, result.physType.getRowType().getFieldCount(), translatedConstants);
                    return RexToLixTranslator.forAggregation(typeFactory, block, inputGetter);
                }

                public Expression computeIndex(Expression offset, WinAggImplementor.SeekType seekType) {
                    Expression index;
                    if (seekType == WinAggImplementor.SeekType.AGG_INDEX) {
                        index = jDecl.parameter;
                    } else if (seekType == WinAggImplementor.SeekType.SET) {
                        index = i_;
                    } else if (seekType == WinAggImplementor.SeekType.START) {
                        index = startX;
                    } else if (seekType == WinAggImplementor.SeekType.END) {
                        index = endX;
                    } else {
                        throw new IllegalArgumentException("SeekSet " + seekType + " is not supported");
                    }
                    if (!Expressions.constant(0).equals(offset)) {
                        index = block.append("idx", Expressions.add(index, offset));
                    }
                    return index;
                }

                private Expression checkBounds(Expression rowIndex, Expression minIndex, Expression maxIndex) {
                    if (rowIndex == i_ || rowIndex == startX || rowIndex == endX) {
                        // No additional bounds check required
                        return hasRows;
                    }
                    // noinspection UnnecessaryLocalVariable
                    Expression res = block.append("rowInFrame", Expressions.foldAnd(ImmutableList.of(hasRows, Expressions.greaterThanOrEqual(rowIndex, minIndex), Expressions.lessThanOrEqual(rowIndex, maxIndex))));
                    return res;
                }

                public Expression rowInFrame(Expression rowIndex) {
                    return checkBounds(rowIndex, startX, endX);
                }

                public Expression rowInPartition(Expression rowIndex) {
                    return checkBounds(rowIndex, minX, maxX);
                }

                public Expression compareRows(Expression a, Expression b) {
                    return Expressions.call(comparator_, BuiltInMethod.COMPARATOR_COMPARE.method, getRow(a), getRow(b));
                }

                public Expression getRow(Expression rowIndex) {
                    return block.append("jRow", RexToLixTranslator.convert(Expressions.arrayIndex(rows_, rowIndex), inputPhysType.getJavaRowType()));
                }

                public Expression index() {
                    return i_;
                }

                public Expression startIndex() {
                    return startX;
                }

                public Expression endIndex() {
                    return endX;
                }

                public Expression hasRows() {
                    return hasRows;
                }

                public Expression getFrameRowCount() {
                    return frameRowCount;
                }

                public Expression getPartitionRowCount() {
                    return partitionRowCount;
                }
            };
        }
    };
}
Also used : Function(com.google.common.base.Function) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 57 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 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 58 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 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 59 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 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 60 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 JaninoRexCompiler method baz.

/**
 * Given a method that implements {@link Scalar#execute(Context, Object[])},
 * adds a bridge method that implements {@link Scalar#execute(Context)}, and
 * compiles.
 */
static Scalar baz(ParameterExpression context_, ParameterExpression outputValues_, BlockStatement block) {
    final List<MemberDeclaration> declarations = Lists.newArrayList();
    // public void execute(Context, Object[] outputValues)
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, BuiltInMethod.SCALAR_EXECUTE2.method.getName(), ImmutableList.of(context_, outputValues_), block));
    // public Object execute(Context)
    final BlockBuilder builder = new BlockBuilder();
    final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(1)));
    builder.add(Expressions.statement(Expressions.call(Expressions.parameter(Scalar.class, "this"), BuiltInMethod.SCALAR_EXECUTE2.method, context_, values_)));
    builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, BuiltInMethod.SCALAR_EXECUTE1.method.getName(), ImmutableList.of(context_), builder.toBlock()));
    final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, "Buzz", null, ImmutableList.<Type>of(Scalar.class), declarations);
    String s = Expressions.toString(declarations, "\n", false);
    if (CalcitePrepareImpl.DEBUG) {
        Util.debugCode(System.out, s);
    }
    try {
        return getScalar(classDeclaration, s);
    } catch (CompileException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) 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