Search in sources :

Example 21 with Expression

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);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 22 with Expression

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

Example 23 with Expression

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);
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 24 with Expression

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_));
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) ArrayList(java.util.ArrayList) Function1(org.apache.calcite.linq4j.function.Function1) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 25 with Expression

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());
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) FlatProductInputType(org.apache.calcite.runtime.SqlFunctions.FlatProductInputType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Expression(org.apache.calcite.linq4j.tree.Expression) MapSqlType(org.apache.calcite.sql.type.MapSqlType) 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)22 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 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 List (java.util.List)5