Search in sources :

Example 76 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 CassandraToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generates a call to "query" with the appropriate fields and predicates
    final BlockBuilder list = new BlockBuilder();
    final CassandraRel.Implementor cassandraImplementor = new CassandraRel.Implementor();
    cassandraImplementor.visitChild(0, getInput());
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(CassandraRules.cassandraFieldNames(rowType), new AbstractList<Class>() {

        @Override
        public Class get(int index) {
            return physType.fieldClass(index);
        }

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    List<Map.Entry<String, String>> selectList = new ArrayList<Map.Entry<String, String>>();
    for (Map.Entry<String, String> entry : Pair.zip(cassandraImplementor.selectFields.keySet(), cassandraImplementor.selectFields.values())) {
        selectList.add(entry);
    }
    final Expression selectFields = list.append("selectFields", constantArrayList(selectList, Pair.class));
    final Expression table = list.append("table", cassandraImplementor.table.getExpression(CassandraTable.CassandraQueryable.class));
    final Expression predicates = list.append("predicates", constantArrayList(cassandraImplementor.whereClause, String.class));
    final Expression order = list.append("order", constantArrayList(cassandraImplementor.order, String.class));
    final Expression offset = list.append("offset", Expressions.constant(cassandraImplementor.offset));
    final Expression fetch = list.append("fetch", Expressions.constant(cassandraImplementor.fetch));
    Expression enumerable = list.append("enumerable", Expressions.call(table, CassandraMethod.CASSANDRA_QUERYABLE_QUERY.method, fields, selectFields, predicates, order, offset, fetch));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Cassandra: " + predicates);
    }
    Hook.QUERY_PLAN.run(predicates);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) Map(java.util.Map) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 77 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 ElasticsearchToEnumerableConverter method implement.

@Override
public Result implement(EnumerableRelImplementor implementor, Prefer prefer) {
    final BlockBuilder list = new BlockBuilder();
    final ElasticsearchRel.Implementor elasticsearchImplementor = new ElasticsearchRel.Implementor();
    elasticsearchImplementor.visitChild(0, getInput());
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, prefer.prefer(JavaRowFormat.ARRAY));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(ElasticsearchRules.elasticsearchFieldNames(rowType), new AbstractList<Class>() {

        @Override
        public Class get(int index) {
            return physType.fieldClass(index);
        }

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    final Expression table = list.append("table", elasticsearchImplementor.table.getExpression(AbstractElasticsearchTable.ElasticsearchQueryable.class));
    List<String> opList = elasticsearchImplementor.list;
    final Expression ops = list.append("ops", constantArrayList(opList, String.class));
    Expression enumerable = list.append("enumerable", Expressions.call(table, ElasticsearchMethod.ELASTICSEARCH_QUERYABLE_FIND.method, ops, fields));
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Elasticsearch: " + opList);
    }
    Hook.QUERY_PLAN.run(opList);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : EnumerableRelImplementor(org.apache.calcite.adapter.enumerable.EnumerableRelImplementor) RelDataType(org.apache.calcite.rel.type.RelDataType) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 78 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 PhysTypeImpl method generateAccessor.

public Expression generateAccessor(List<Integer> fields) {
    ParameterExpression v1 = Expressions.parameter(javaRowClass, "v1");
    switch(fields.size()) {
        case 0:
            return Expressions.lambda(Function1.class, Expressions.field(null, BuiltInMethod.COMPARABLE_EMPTY_LIST.field), v1);
        case 1:
            int field0 = fields.get(0);
            // new Function1<Employee, Res> {
            // public Res apply(Employee v1) {
            // return v1.<fieldN>;
            // }
            // }
            Class returnType = fieldClasses.get(field0);
            Expression fieldReference = Types.castIfNecessary(returnType, fieldReference(v1, field0));
            return Expressions.lambda(Function1.class, fieldReference, v1);
        default:
            // new Function1<Employee, List> {
            // public List apply(Employee v1) {
            // return Arrays.asList(
            // new Object[] {v1.<fieldN>, v1.<fieldM>});
            // }
            // }
            Expressions.FluentList<Expression> list = Expressions.list();
            for (int field : fields) {
                list.add(fieldReference(v1, field));
            }
            switch(list.size()) {
                case 2:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST2.method, list), v1);
                case 3:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST3.method, list), v1);
                case 4:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST4.method, list), v1);
                case 5:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST5.method, list), v1);
                case 6:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST6.method, list), v1);
                default:
                    return Expressions.lambda(Function1.class, Expressions.call(List.class, null, BuiltInMethod.LIST_N.method, Expressions.newArrayInit(Comparable.class, list)), v1);
            }
    }
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) EnumUtils.javaRowClass(org.apache.calcite.adapter.enumerable.EnumUtils.javaRowClass) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Expressions(org.apache.calcite.linq4j.tree.Expressions)

Example 79 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 PhysTypeImpl method generateSelector.

public Expression generateSelector(final ParameterExpression parameter, final List<Integer> fields, List<Integer> usedFields, JavaRowFormat targetFormat) {
    final PhysType targetPhysType = project(fields, true, targetFormat);
    final List<Expression> expressions = Lists.newArrayList();
    for (Ord<Integer> ord : Ord.zip(fields)) {
        final Integer field = ord.e;
        if (usedFields.contains(field)) {
            expressions.add(fieldReference(parameter, field));
        } else {
            final Primitive primitive = Primitive.of(targetPhysType.fieldClass(ord.i));
            expressions.add(Expressions.constant(primitive != null ? primitive.defaultValue : null));
        }
    }
    for (Integer field : fields) {
        expressions.add(Expressions.constant(!usedFields.contains(field)));
    }
    return Expressions.lambda(Function1.class, targetPhysType.record(expressions), parameter);
}
Also used : Primitive(org.apache.calcite.linq4j.tree.Primitive) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression)

Example 80 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 RexImpTable method implementNullSemantics.

private static Expression implementNullSemantics(RexToLixTranslator translator, RexCall call, NullAs nullAs, NullPolicy nullPolicy, NotNullImplementor implementor) {
    final List<Expression> list = new ArrayList<>();
    switch(nullAs) {
        case NULL:
            // v0 == null || v1 == null ? null : f(v0, v1)
            for (Ord<RexNode> operand : Ord.zip(call.getOperands())) {
                if (translator.isNullable(operand.e)) {
                    list.add(translator.translate(operand.e, NullAs.IS_NULL));
                    translator = translator.setNullable(operand.e, false);
                }
            }
            final Expression box = Expressions.box(implementCall(translator, call, implementor, nullAs));
            return optimize(Expressions.condition(Expressions.foldOr(list), Types.castIfNecessary(box.getType(), NULL_EXPR), box));
        case FALSE:
            // v0 != null && v1 != null && f(v0, v1)
            for (Ord<RexNode> operand : Ord.zip(call.getOperands())) {
                if (translator.isNullable(operand.e)) {
                    list.add(translator.translate(operand.e, NullAs.IS_NOT_NULL));
                    translator = translator.setNullable(operand.e, false);
                }
            }
            list.add(implementCall(translator, call, implementor, nullAs));
            return Expressions.foldAnd(list);
        case TRUE:
            // v0 == null || v1 == null || f(v0, v1)
            for (Ord<RexNode> operand : Ord.zip(call.getOperands())) {
                if (translator.isNullable(operand.e)) {
                    list.add(translator.translate(operand.e, NullAs.IS_NULL));
                    translator = translator.setNullable(operand.e, false);
                }
            }
            list.add(implementCall(translator, call, implementor, nullAs));
            return Expressions.foldOr(list);
        case NOT_POSSIBLE:
            // Need to transmit to the implementor the fact that call cannot
            // return null. In particular, it should return a primitive (e.g.
            // int) rather than a box type (Integer).
            // The cases with setNullable above might not help since the same
            // RexNode can be referred via multiple ways: RexNode itself, RexLocalRef,
            // and may be others.
            final Map<RexNode, Boolean> nullable = new HashMap<>();
            switch(nullPolicy) {
                case STRICT:
                    // in nulls NOT_POSSIBLE mode
                    for (RexNode arg : call.getOperands()) {
                        if (translator.isNullable(arg) && !nullable.containsKey(arg)) {
                            nullable.put(arg, false);
                        }
                    }
            }
            nullable.put(call, false);
            translator = translator.setNullable(nullable);
        // fall through
        default:
            return implementCall(translator, call, implementor, nullAs);
    }
}
Also used : UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Expression(org.apache.calcite.linq4j.tree.Expression) MemberExpression(org.apache.calcite.linq4j.tree.MemberExpression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

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