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;
}
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;
}
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);
}
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()));
}
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()))));
}
}
Aggregations