Search in sources :

Example 51 with Expression

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.

the class BeamEnumerableConverter method implement.

@Override
public Result implement(EnumerableRelImplementor implementor, Prefer prefer) {
    final BlockBuilder list = new BlockBuilder();
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, prefer.preferArray());
    final Expression node = implementor.stash((BeamRelNode) getInput(), BeamRelNode.class);
    list.add(Expressions.call(BeamEnumerableConverter.class, "toEnumerable", node));
    return implementor.result(physType, list.toBlock());
}
Also used : PhysType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) BlockBuilder(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder)

Example 52 with Expression

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.

the class BigQueryFilter method isSupported.

/**
 * Check whether a {@code RexNode} is supported. As of right now BigQuery supports: 1. Complex
 * predicates (both conjunction and disjunction). 2. Comparison between a column and a literal.
 *
 * <p>TODO: Check if comparison between two columns is supported. Also over a boolean field.
 *
 * @param node A node to check for predicate push-down support.
 * @return A pair containing a boolean whether an expression is supported and the number of input
 *     references used by the expression.
 */
private Pair<Boolean, Integer> isSupported(RexNode node) {
    int numberOfInputRefs = 0;
    boolean isSupported = true;
    if (node instanceof RexCall) {
        RexCall compositeNode = (RexCall) node;
        // CAST, TRIM? and REVERSE? should be supported as well.
        if (!node.getKind().belongsTo(SUPPORTED_OPS)) {
            isSupported = false;
        } else {
            for (RexNode operand : compositeNode.getOperands()) {
                // All operands must be supported for a parent node to be supported.
                Pair<Boolean, Integer> childSupported = isSupported(operand);
                // (OR).
                if (!node.getKind().belongsTo(ImmutableSet.of(AND, OR))) {
                    numberOfInputRefs += childSupported.getRight();
                }
                // Predicate functions, where more than one field is involved are unsupported.
                isSupported = numberOfInputRefs < 2 && childSupported.getLeft();
            }
        }
    } else if (node instanceof RexInputRef) {
        numberOfInputRefs = 1;
    } else if (node instanceof RexLiteral) {
    // RexLiterals are expected, but no action is needed.
    } else {
        throw new UnsupportedOperationException("Encountered an unexpected node type: " + node.getClass().getSimpleName());
    }
    return Pair.of(isSupported, numberOfInputRefs);
}
Also used : RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) RexInputRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode)

Example 53 with Expression

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project lucene-solr by apache.

the class SolrToEnumerableConverter method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // Generates a call to "query" with the appropriate fields
    final BlockBuilder list = new BlockBuilder();
    final SolrRel.Implementor solrImplementor = new SolrRel.Implementor();
    solrImplementor.visitChild(0, getInput());
    final RelDataType rowType = getRowType();
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final Expression table = list.append("table", solrImplementor.table.getExpression(SolrTable.SolrQueryable.class));
    final Expression fields = list.append("fields", constantArrayList(Pair.zip(generateFields(SolrRules.solrFieldNames(rowType), solrImplementor.fieldMappings), new AbstractList<Class>() {

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

        @Override
        public int size() {
            return rowType.getFieldCount();
        }
    }), Pair.class));
    final Expression query = list.append("query", Expressions.constant(solrImplementor.query, String.class));
    final Expression orders = list.append("orders", constantArrayList(solrImplementor.orders, Pair.class));
    final Expression buckets = list.append("buckets", constantArrayList(solrImplementor.buckets, String.class));
    final Expression metricPairs = list.append("metricPairs", constantArrayList(solrImplementor.metricPairs, Pair.class));
    final Expression limit = list.append("limit", Expressions.constant(solrImplementor.limitValue));
    final Expression negativeQuery = list.append("negativeQuery", Expressions.constant(Boolean.toString(solrImplementor.negativeQuery), String.class));
    final Expression havingPredicate = list.append("havingTest", Expressions.constant(solrImplementor.havingPredicate, String.class));
    Expression enumerable = list.append("enumerable", Expressions.call(table, SolrMethod.SOLR_QUERYABLE_QUERY.method, fields, query, orders, buckets, metricPairs, limit, negativeQuery, havingPredicate));
    Hook.QUERY_PLAN.run(query);
    list.add(Expressions.return_(null, enumerable));
    return implementor.result(physType, list.toBlock());
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) Expression(org.apache.calcite.linq4j.tree.Expression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Pair(org.apache.calcite.util.Pair)

Example 54 with Expression

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project streamline by hortonworks.

the class RexNodeToJavaCodeCompiler method baz.

/**
 * Given a method that implements {@link ExecutableExpression#execute(Context, Object[])},
 * adds a bridge method that implements {@link ExecutableExpression#execute(Context)}, and
 * compiles.
 */
static String baz(ParameterExpression context_, ParameterExpression outputValues_, BlockStatement block, String className) {
    final List<MemberDeclaration> declarations = Lists.newArrayList();
    // public void execute(Context, Object[] outputValues)
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, StreamlineBuiltInMethod.EXPR_EXECUTE2.method.getName(), ImmutableList.of(context_, outputValues_), block));
    // public Object execute(Context)
    final BlockBuilder builder = new BlockBuilder();
    final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(1)));
    builder.add(Expressions.statement(Expressions.call(Expressions.parameter(ExecutableExpression.class, "this"), StreamlineBuiltInMethod.EXPR_EXECUTE2.method, context_, values_)));
    builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, StreamlineBuiltInMethod.EXPR_EXECUTE1.method.getName(), ImmutableList.of(context_), builder.toBlock()));
    final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, className, null, ImmutableList.<Type>of(ExecutableExpression.class), declarations);
    return Expressions.toString(Lists.newArrayList(classDeclaration), "\n", false);
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) Expression(org.apache.calcite.linq4j.tree.Expression) ExecutableExpression(com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) ExecutableExpression(com.hortonworks.streamline.streams.sql.runtime.ExecutableExpression)

Example 55 with Expression

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project beam by apache.

the class BeamIOPushDownRule method findUtilizedInputRefs.

/**
 * Given a {@code RexNode}, find all {@code RexInputRef}s a node or it's children nodes use.
 *
 * @param inputRowType {@code RelDataType} used for looking up names of {@code RexInputRef}.
 * @param startNode A node to start at.
 * @param usedFields Names of {@code RexInputRef}s are added to this list.
 */
@VisibleForTesting
void findUtilizedInputRefs(RelDataType inputRowType, RexNode startNode, Set<String> usedFields) {
    Queue<RexNode> prerequisites = new ArrayDeque<>();
    prerequisites.add(startNode);
    // Assuming there are no cyclic nodes, traverse dependency tree until all RexInputRefs are found
    while (!prerequisites.isEmpty()) {
        RexNode node = prerequisites.poll();
        if (node instanceof RexCall) {
            // Composite expression, example: "=($t11, $t12)"
            RexCall compositeNode = (RexCall) node;
            // Expression from example above contains 2 operands: $t11, $t12
            prerequisites.addAll(compositeNode.getOperands());
        } else if (node instanceof RexInputRef) {
            // Input reference
            // Find a field in an inputRowType for the input reference
            int inputFieldIndex = ((RexInputRef) node).getIndex();
            RelDataTypeField field = inputRowType.getFieldList().get(inputFieldIndex);
            // If we have not seen it before - add it to the list (hash set)
            usedFields.add(field.getName());
        } else if (node instanceof RexLiteral) {
        // Does not contain information about columns utilized by a Calc
        } else {
            throw new UnsupportedOperationException("Unexpected RexNode encountered: " + node.getClass().getSimpleName());
        }
    }
}
Also used : RexCall(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall) RexLiteral(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexLiteral) RelDataTypeField(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexInputRef) ArrayDeque(java.util.ArrayDeque) RexNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

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