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