Search in sources :

Example 21 with Expression

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

the class StrictAggImplementor method implementResult.

public final Expression implementResult(AggContext info, final AggResultContext result) {
    if (!needTrackEmptySet) {
        return RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
    }
    String tmpName = result.accumulator().isEmpty() ? "ar" : (result.accumulator().get(0) + "$Res");
    ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName(tmpName));
    List<Expression> acc = result.accumulator();
    final BlockBuilder thenBlock = result.nestBlock();
    Expression nonNull = RexToLixTranslator.convert(implementNotNullResult(info, result), info.returnType());
    result.exitBlock();
    thenBlock.add(Expressions.statement(Expressions.assign(res, nonNull)));
    BlockStatement thenBranch = thenBlock.toBlock();
    Expression seenNotNullRows = trackNullsPerRow ? acc.get(acc.size() - 1) : ((WinAggResultContext) result).hasRows();
    if (thenBranch.statements.size() == 1) {
        return Expressions.condition(seenNotNullRows, nonNull, RexImpTable.getDefaultValue(res.getType()));
    }
    result.currentBlock().add(Expressions.declare(0, res, null));
    result.currentBlock().add(Expressions.ifThenElse(seenNotNullRows, thenBranch, Expressions.statement(Expressions.assign(res, RexImpTable.getDefaultValue(res.getType())))));
    return res;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 22 with Expression

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

the class StrictAggImplementor method implementReset.

public final void implementReset(AggContext info, AggResetContext reset) {
    if (trackNullsPerRow) {
        List<Expression> acc = reset.accumulator();
        Expression flag = acc.get(acc.size() - 1);
        BlockBuilder block = reset.currentBlock();
        block.add(Expressions.statement(Expressions.assign(flag, RexImpTable.getDefaultValue(flag.getType()))));
    }
    implementNotNullReset(info, reset);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 23 with Expression

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

the class EnumerableTableModify method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    final Result result = implementor.visitChild(this, 0, (EnumerableRel) getInput(), pref);
    Expression childExp = builder.append("child", result.block);
    final ParameterExpression collectionParameter = Expressions.parameter(Collection.class, builder.newName("collection"));
    final Expression expression = table.getExpression(ModifiableTable.class);
    // TODO: user error in validator
    assert expression != null;
    assert ModifiableTable.class.isAssignableFrom(Types.toClass(expression.getType())) : expression.getType();
    builder.add(Expressions.declare(Modifier.FINAL, collectionParameter, Expressions.call(expression, BuiltInMethod.MODIFIABLE_TABLE_GET_MODIFIABLE_COLLECTION.method)));
    final Expression countParameter = builder.append("count", Expressions.call(collectionParameter, "size"), false);
    Expression convertedChildExp;
    if (!getInput().getRowType().equals(getRowType())) {
        final JavaTypeFactory typeFactory = (JavaTypeFactory) getCluster().getTypeFactory();
        final JavaRowFormat format = EnumerableTableScan.deduceFormat(table);
        PhysType physType = PhysTypeImpl.of(typeFactory, table.getRowType(), format);
        List<Expression> expressionList = new ArrayList<Expression>();
        final PhysType childPhysType = result.physType;
        final ParameterExpression o_ = Expressions.parameter(childPhysType.getJavaRowType(), "o");
        final int fieldCount = childPhysType.getRowType().getFieldCount();
        for (int i = 0; i < fieldCount; i++) {
            expressionList.add(childPhysType.fieldReference(o_, i, physType.getJavaFieldType(i)));
        }
        convertedChildExp = builder.append("convertedChild", Expressions.call(childExp, BuiltInMethod.SELECT.method, Expressions.lambda(physType.record(expressionList), o_)));
    } else {
        convertedChildExp = childExp;
    }
    final Method method;
    switch(getOperation()) {
        case INSERT:
            method = BuiltInMethod.INTO.method;
            break;
        case DELETE:
            method = BuiltInMethod.REMOVE_ALL.method;
            break;
        default:
            throw new AssertionError(getOperation());
    }
    builder.add(Expressions.statement(Expressions.call(convertedChildExp, method, collectionParameter)));
    final Expression updatedCountParameter = builder.append("updatedCount", Expressions.call(collectionParameter, "size"), false);
    builder.add(Expressions.return_(null, Expressions.call(BuiltInMethod.SINGLETON_ENUMERABLE.method, Expressions.convert_(Expressions.condition(Expressions.greaterThanOrEqual(updatedCountParameter, countParameter), Expressions.subtract(updatedCountParameter, countParameter), Expressions.subtract(countParameter, updatedCountParameter)), long.class))));
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref == Prefer.ARRAY ? JavaRowFormat.ARRAY : JavaRowFormat.SCALAR);
    return implementor.result(physType, builder.toBlock());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) ArrayList(java.util.ArrayList) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 24 with Expression

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

the class EnumerableTableScan method getExpression.

private Expression getExpression(PhysType physType) {
    final Expression expression = table.getExpression(Queryable.class);
    final Expression expression2 = toEnumerable(expression);
    assert Types.isAssignableFrom(Enumerable.class, expression2.getType());
    return toRows(physType, expression2);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 25 with Expression

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

the class EnumerableTableScan method toRows.

private Expression toRows(PhysType physType, Expression expression) {
    if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) {
        return Expressions.call(BuiltInMethod.SLICE0.method, expression);
    }
    JavaRowFormat oldFormat = format();
    if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) {
        return expression;
    }
    final ParameterExpression row_ = Expressions.parameter(elementType, "row");
    final int fieldCount = table.getRowType().getFieldCount();
    List<Expression> expressionList = new ArrayList<>(fieldCount);
    for (int i = 0; i < fieldCount; i++) {
        expressionList.add(fieldExpression(row_, i, physType, oldFormat));
    }
    return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) ArrayList(java.util.ArrayList) Function1(org.apache.calcite.linq4j.function.Function1) ScannableTable(org.apache.calcite.schema.ScannableTable)

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)26 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 RexNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)9 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