use of 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.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.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.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_));
}
use of org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class EnumerableUncollect method implement.
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
final BlockBuilder builder = new BlockBuilder();
final EnumerableRel child = (EnumerableRel) getInput();
final Result result = implementor.visitChild(this, 0, child, pref);
final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.LIST);
// final Enumerable<List<Employee>> child = <<child adapter>>;
// return child.selectMany(FLAT_PRODUCT);
final Expression child_ = builder.append("child", result.block);
final List<Integer> fieldCounts = new ArrayList<>();
final List<FlatProductInputType> inputTypes = new ArrayList<>();
for (RelDataTypeField field : child.getRowType().getFieldList()) {
final RelDataType type = field.getType();
if (type instanceof MapSqlType) {
fieldCounts.add(2);
inputTypes.add(FlatProductInputType.MAP);
} else {
final RelDataType elementType = type.getComponentType();
if (elementType.isStruct()) {
fieldCounts.add(elementType.getFieldCount());
inputTypes.add(FlatProductInputType.LIST);
} else {
fieldCounts.add(-1);
inputTypes.add(FlatProductInputType.SCALAR);
}
}
}
final Expression lambda = Expressions.call(BuiltInMethod.FLAT_PRODUCT.method, Expressions.constant(Ints.toArray(fieldCounts)), Expressions.constant(withOrdinality), Expressions.constant(inputTypes.toArray(new FlatProductInputType[inputTypes.size()])));
builder.add(Expressions.return_(null, Expressions.call(child_, BuiltInMethod.SELECT_MANY.method, lambda)));
return implementor.result(physType, builder.toBlock());
}
Aggregations