Search in sources :

Example 76 with Expression

use of org.apache.calcite.linq4j.tree.Expression 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 77 with Expression

use of org.apache.calcite.linq4j.tree.Expression 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 78 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class RelOptTableImpl method create.

public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, final CalciteSchema.TableEntry tableEntry, Double rowCount) {
    final Table table = tableEntry.getTable();
    Function<Class, Expression> expressionFunction = getClassExpressionFunction(tableEntry, table);
    return new RelOptTableImpl(schema, rowType, tableEntry.path(), table, expressionFunction, rowCount);
}
Also used : TranslatableTable(org.apache.calcite.schema.TranslatableTable) Table(org.apache.calcite.schema.Table) ModifiableTable(org.apache.calcite.schema.ModifiableTable) StreamableTable(org.apache.calcite.schema.StreamableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) QueryableTable(org.apache.calcite.schema.QueryableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) Expression(org.apache.calcite.linq4j.tree.Expression)

Example 79 with Expression

use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.

the class RelOptTableImpl method create.

public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, Table table, Path path) {
    final SchemaPlus schemaPlus = MySchemaPlus.create(path);
    Function<Class, Expression> expressionFunction = getClassExpressionFunction(schemaPlus, Util.last(path).left, table);
    return new RelOptTableImpl(schema, rowType, Pair.left(path), table, expressionFunction, table.getStatistic().getRowCount());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 80 with Expression

use of org.apache.calcite.linq4j.tree.Expression 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)

Aggregations

Expression (org.apache.calcite.linq4j.tree.Expression)91 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)64 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)56 ArrayList (java.util.ArrayList)22 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)19 RelDataType (org.apache.calcite.rel.type.RelDataType)19 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)16 Test (org.junit.Test)16 Type (java.lang.reflect.Type)11 PhysType (org.apache.calcite.adapter.enumerable.PhysType)11 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)11 RexNode (org.apache.calcite.rex.RexNode)10 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)9 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)8 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)8 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)8 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)7 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5