Search in sources :

Example 31 with ParameterExpression

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

the class PhysTypeImpl method convertTo.

public Expression convertTo(Expression exp, PhysType targetPhysType) {
    final JavaRowFormat targetFormat = targetPhysType.getFormat();
    if (format == targetFormat) {
        return exp;
    }
    final ParameterExpression o_ = Expressions.parameter(javaRowClass, "o");
    final int fieldCount = rowType.getFieldCount();
    return Expressions.call(exp, BuiltInMethod.SELECT.method, generateSelector(o_, Util.range(fieldCount), targetFormat));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 32 with ParameterExpression

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

the class PhysTypeImpl method generateCollationKey.

public Pair<Expression, Expression> generateCollationKey(final List<RelFieldCollation> collations) {
    final Expression selector;
    if (collations.size() == 1) {
        RelFieldCollation collation = collations.get(0);
        ParameterExpression parameter = Expressions.parameter(javaRowClass, "v");
        selector = Expressions.lambda(Function1.class, fieldReference(parameter, collation.getFieldIndex()), parameter);
        return Pair.<Expression, Expression>of(selector, Expressions.call(BuiltInMethod.NULLS_COMPARATOR.method, Expressions.constant(collation.nullDirection == RelFieldCollation.NullDirection.FIRST), Expressions.constant(collation.getDirection() == RelFieldCollation.Direction.DESCENDING)));
    }
    selector = Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
    // int c;
    // c = Utilities.compare(v0, v1);
    // if (c != 0) return c; // or -c if descending
    // ...
    // return 0;
    BlockBuilder body = new BlockBuilder();
    final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
    final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
    final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
    final int mod = collations.size() == 1 ? Modifier.FINAL : 0;
    body.add(Expressions.declare(mod, parameterC, null));
    for (RelFieldCollation collation : collations) {
        final int index = collation.getFieldIndex();
        Expression arg0 = fieldReference(parameterV0, index);
        Expression arg1 = fieldReference(parameterV1, index);
        switch(Primitive.flavor(fieldClass(index))) {
            case OBJECT:
                arg0 = Types.castIfNecessary(Comparable.class, arg0);
                arg1 = Types.castIfNecessary(Comparable.class, arg1);
        }
        final boolean nullsFirst = collation.nullDirection == RelFieldCollation.NullDirection.FIRST;
        final boolean descending = collation.getDirection() == RelFieldCollation.Direction.DESCENDING;
        final Method method = (fieldNullable(index) ? (nullsFirst ^ descending ? BuiltInMethod.COMPARE_NULLS_FIRST : BuiltInMethod.COMPARE_NULLS_LAST) : BuiltInMethod.COMPARE).method;
        body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(method.getDeclaringClass(), method.getName(), arg0, arg1))));
        body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
    }
    body.add(Expressions.return_(null, Expressions.constant(0)));
    final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
    if (EnumerableRules.BRIDGE_METHODS) {
        final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
        final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
        BlockBuilder bridgeBody = new BlockBuilder();
        bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
        memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
    }
    return Pair.<Expression, Expression>of(selector, Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Function1(org.apache.calcite.linq4j.function.Function1) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Comparator(java.util.Comparator)

Example 33 with ParameterExpression

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

the class RexToLixTranslator method translateCondition.

public static Expression translateCondition(RexProgram program, JavaTypeFactory typeFactory, BlockBuilder list, InputGetter inputGetter, Function1<String, InputGetter> correlates) {
    if (program.getCondition() == null) {
        return RexImpTable.TRUE_EXPR;
    }
    final ParameterExpression root = DataContext.ROOT;
    RexToLixTranslator translator = new RexToLixTranslator(program, typeFactory, root, inputGetter, list);
    translator = translator.setCorrelates(correlates);
    return translator.translate(program.getCondition(), RexImpTable.NullAs.FALSE);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 34 with ParameterExpression

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

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

Aggregations

ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)83 Test (org.junit.Test)41 Expression (org.apache.calcite.linq4j.tree.Expression)38 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)32 ArrayList (java.util.ArrayList)12 RelDataType (org.apache.calcite.rel.type.RelDataType)12 Type (java.lang.reflect.Type)9 Function1 (org.apache.calcite.linq4j.function.Function1)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)8 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)6 Method (java.lang.reflect.Method)5 List (java.util.List)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)5 ClassDeclaration (org.apache.calcite.linq4j.tree.ClassDeclaration)5 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)5 RexNode (org.apache.calcite.rex.RexNode)5