Search in sources :

Example 81 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 RexToLixTranslator method handleNull.

/**
 * Adapts an expression with "normal" result to one that adheres to
 * this particular policy. Wraps the result expression into a new
 * parameter if need be.
 *
 * @param input Expression
 * @param nullAs If false, if expression is definitely not null at
 *   runtime. Therefore we can optimize. For example, we can cast to int
 *   using x.intValue().
 * @return Translated expression
 */
public Expression handleNull(Expression input, RexImpTable.NullAs nullAs) {
    final Expression nullHandled = nullAs.handle(input);
    // If we get ConstantExpression, just return it (i.e. primitive false)
    if (nullHandled instanceof ConstantExpression) {
        return nullHandled;
    }
    // then we can just reuse it
    if (nullHandled == input) {
        return input;
    }
    // If nullHandled is different, then it might be unsafe to compute
    // early (i.e. unbox of null value should not happen _before_ ternary).
    // Thus we wrap it into brand-new ParameterExpression,
    // and we are guaranteed that ParameterExpression will not be shared
    String unboxVarName = "v_unboxed";
    if (input instanceof ParameterExpression) {
        unboxVarName = ((ParameterExpression) input).name + "_unboxed";
    }
    ParameterExpression unboxed = Expressions.parameter(nullHandled.getType(), list.newName(unboxVarName));
    list.add(Expressions.declare(Modifier.FINAL, unboxed, nullHandled));
    return unboxed;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ByteString(org.apache.calcite.avatica.util.ByteString)

Example 82 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 RexToLixTranslator method translateList.

/**
 * Translates the list of {@code RexNode}, while optimizing for output
 * storage.
 * For instance, if the result of translation is going to be stored in
 * {@code Object[]}, and the input is {@code Object[]} as well,
 * then translator will avoid casting, boxing, etc.
 *
 * @param operandList list of RexNodes to translate
 * @param storageTypes hints of the java classes that will be used
 *                     to store translation results. Use null to use
 *                     default storage type
 *
 * @return translated expressions
 */
public List<Expression> translateList(List<? extends RexNode> operandList, List<? extends Type> storageTypes) {
    final List<Expression> list = new ArrayList<>(operandList.size());
    for (int i = 0; i < operandList.size(); i++) {
        RexNode rex = operandList.get(i);
        Type desiredType = null;
        if (storageTypes != null) {
            desiredType = storageTypes.get(i);
        }
        final Expression translate = translate(rex, desiredType);
        list.add(translate);
        // It is favourable to get the type matching desired type
        if (desiredType == null && !isNullable(rex)) {
            assert !Primitive.isBox(translate.getType()) : "Not-null boxed primitive should come back as primitive: " + rex + ", " + translate.getType();
        }
    }
    return list;
}
Also used : ExpressionType(org.apache.calcite.linq4j.tree.ExpressionType) Type(java.lang.reflect.Type) RelDataType(org.apache.calcite.rel.type.RelDataType) Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

Example 83 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 RexToLixTranslator method translate.

Expression translate(RexNode expr, RexImpTable.NullAs nullAs, Type storageType) {
    Expression expression = translate0(expr, nullAs, storageType);
    expression = EnumUtils.enforce(storageType, expression);
    assert expression != null;
    return list.append("v", expression);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 84 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 implementAdd.

public final void implementAdd(AggContext info, final AggAddContext add) {
    final List<RexNode> args = add.rexArguments();
    final RexToLixTranslator translator = add.rowTranslator();
    final List<Expression> conditions = new ArrayList<>();
    conditions.addAll(translator.translateList(args, RexImpTable.NullAs.IS_NOT_NULL));
    if (add.rexFilterArgument() != null) {
        conditions.add(translator.translate(add.rexFilterArgument(), RexImpTable.NullAs.FALSE));
    }
    Expression condition = Expressions.foldAnd(conditions);
    if (Expressions.constant(false).equals(condition)) {
        return;
    }
    boolean argsNotNull = Expressions.constant(true).equals(condition);
    final BlockBuilder thenBlock = argsNotNull ? add.currentBlock() : new BlockBuilder(true, add.currentBlock());
    if (trackNullsPerRow) {
        List<Expression> acc = add.accumulator();
        thenBlock.add(Expressions.statement(Expressions.assign(acc.get(acc.size() - 1), Expressions.constant(true))));
    }
    if (argsNotNull) {
        implementNotNullAdd(info, add);
        return;
    }
    final Map<RexNode, Boolean> nullables = new HashMap<>();
    for (RexNode arg : args) {
        if (translator.isNullable(arg)) {
            nullables.put(arg, false);
        }
    }
    add.nestBlock(thenBlock, nullables);
    implementNotNullAdd(info, add);
    add.exitBlock();
    add.currentBlock().add(Expressions.ifThen(condition, thenBlock.toBlock()));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 85 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 implementNotNullReset.

protected void implementNotNullReset(AggContext info, AggResetContext reset) {
    BlockBuilder block = reset.currentBlock();
    List<Expression> accumulator = reset.accumulator();
    for (int i = 0; i < getStateSize(); i++) {
        Expression exp = accumulator.get(i);
        block.add(Expressions.statement(Expressions.assign(exp, RexImpTable.getDefaultValue(exp.getType()))));
    }
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

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