Search in sources :

Example 21 with RexCall

use of org.apache.calcite.rex.RexCall in project calcite by apache.

the class RelDecorrelator method findCorrelationEquivalent.

/**
 * Finds a {@link RexInputRef} that is equivalent to a {@link CorRef},
 * and if found, throws a {@link org.apache.calcite.util.Util.FoundOne}.
 */
private void findCorrelationEquivalent(CorRef correlation, RexNode e) throws Util.FoundOne {
    switch(e.getKind()) {
        case EQUALS:
            final RexCall call = (RexCall) e;
            final List<RexNode> operands = call.getOperands();
            if (references(operands.get(0), correlation)) {
                throw new Util.FoundOne(operands.get(1));
            }
            if (references(operands.get(1), correlation)) {
                throw new Util.FoundOne(operands.get(0));
            }
            break;
        case AND:
            for (RexNode operand : ((RexCall) e).getOperands()) {
                findCorrelationEquivalent(correlation, operand);
            }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexNode(org.apache.calcite.rex.RexNode)

Example 22 with RexCall

use of org.apache.calcite.rex.RexCall in project calcite by apache.

the class RelBuilder method inferAlias.

/**
 * Infers the alias of an expression.
 *
 * <p>If the expression was created by {@link #alias}, replaces the expression
 * in the project list.
 */
private String inferAlias(List<RexNode> exprList, RexNode expr) {
    switch(expr.getKind()) {
        case INPUT_REF:
            final RexInputRef ref = (RexInputRef) expr;
            return stack.peek().fields.get(ref.getIndex()).getValue().getName();
        case CAST:
            return inferAlias(exprList, ((RexCall) expr).getOperands().get(0));
        case AS:
            final RexCall call = (RexCall) expr;
            for (; ; ) {
                final int i = exprList.indexOf(expr);
                if (i < 0) {
                    break;
                }
                exprList.set(i, call.getOperands().get(0));
            }
            return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()).getValue();
        default:
            return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) NlsString(org.apache.calcite.util.NlsString)

Example 23 with RexCall

use of org.apache.calcite.rex.RexCall in project calcite by apache.

the class RelBuilder method match.

/**
 * Creates a {@link org.apache.calcite.rel.core.Match}.
 */
public RelBuilder match(RexNode pattern, boolean strictStart, boolean strictEnd, Map<String, RexNode> patternDefinitions, Iterable<? extends RexNode> measureList, RexNode after, Map<String, ? extends SortedSet<String>> subsets, boolean allRows, Iterable<? extends RexNode> partitionKeys, Iterable<? extends RexNode> orderKeys, RexNode interval) {
    final List<RelFieldCollation> fieldCollations = new ArrayList<>();
    for (RexNode orderKey : orderKeys) {
        final RelFieldCollation.Direction direction;
        switch(orderKey.getKind()) {
            case DESCENDING:
                direction = RelFieldCollation.Direction.DESCENDING;
                orderKey = ((RexCall) orderKey).getOperands().get(0);
                break;
            case NULLS_FIRST:
            case NULLS_LAST:
                throw new AssertionError();
            default:
                direction = RelFieldCollation.Direction.ASCENDING;
                break;
        }
        final RelFieldCollation.NullDirection nullDirection = direction.defaultNullDirection();
        final RexInputRef ref = (RexInputRef) orderKey;
        fieldCollations.add(new RelFieldCollation(ref.getIndex(), direction, nullDirection));
    }
    final RelDataTypeFactory.Builder typeBuilder = cluster.getTypeFactory().builder();
    for (RexNode partitionKey : partitionKeys) {
        typeBuilder.add(partitionKey.toString(), partitionKey.getType());
    }
    if (allRows) {
        for (RexNode orderKey : orderKeys) {
            if (!typeBuilder.nameExists(orderKey.toString())) {
                typeBuilder.add(orderKey.toString(), orderKey.getType());
            }
        }
        final RelDataType inputRowType = peek().getRowType();
        for (RelDataTypeField fs : inputRowType.getFieldList()) {
            if (!typeBuilder.nameExists(fs.getName())) {
                typeBuilder.add(fs);
            }
        }
    }
    final ImmutableMap.Builder<String, RexNode> measures = ImmutableMap.builder();
    for (RexNode measure : measureList) {
        List<RexNode> operands = ((RexCall) measure).getOperands();
        String alias = operands.get(1).toString();
        typeBuilder.add(alias, operands.get(0).getType());
        measures.put(alias, operands.get(0));
    }
    final RelNode match = matchFactory.createMatch(peek(), pattern, typeBuilder.build(), strictStart, strictEnd, patternDefinitions, measures.build(), after, subsets, allRows, ImmutableList.copyOf(partitionKeys), RelCollations.of(fieldCollations), interval);
    stack.push(new Frame(match));
    return this;
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) NlsString(org.apache.calcite.util.NlsString) ImmutableMap(com.google.common.collect.ImmutableMap) RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 24 with RexCall

use of org.apache.calcite.rex.RexCall in project calcite by apache.

the class RelStructuredTypeFlattener method flattenProjection.

private void flattenProjection(RewriteRexShuttle shuttle, RexNode exp, String fieldName, List<Pair<RexNode, String>> flattenedExps) {
    if (exp.getType().isStruct()) {
        if (exp instanceof RexInputRef) {
            RexInputRef inputRef = (RexInputRef) exp;
            int newOffset = getNewForOldInput(inputRef.getIndex());
            // expand to range
            RelDataType flattenedType = SqlTypeUtil.flattenRecordType(rexBuilder.getTypeFactory(), exp.getType(), null);
            List<RelDataTypeField> fieldList = flattenedType.getFieldList();
            int n = fieldList.size();
            for (int j = 0; j < n; ++j) {
                RelDataTypeField field = fieldList.get(j);
                flattenedExps.add(Pair.<RexNode, String>of(new RexInputRef(newOffset + j, field.getType()), fieldName));
            }
        } else if (isConstructor(exp) || exp.isA(SqlKind.CAST)) {
            // REVIEW jvs 27-Feb-2005:  for cast, see corresponding note
            // in RewriteRexShuttle
            RexCall call = (RexCall) exp;
            if (exp.isA(SqlKind.NEW_SPECIFICATION)) {
                // For object constructors, prepend a FALSE null
                // indicator.
                flattenedExps.add(Pair.<RexNode, String>of(rexBuilder.makeLiteral(false), fieldName));
            } else if (exp.isA(SqlKind.CAST)) {
                if (RexLiteral.isNullLiteral(((RexCall) exp).operands.get(0))) {
                    // Translate CAST(NULL AS UDT) into
                    // the correct number of null fields.
                    flattenNullLiteral(exp.getType(), flattenedExps);
                    return;
                }
            }
            flattenProjections(new RewriteRexShuttle(), call.getOperands(), Collections.<String>nCopies(call.getOperands().size(), null), fieldName, flattenedExps);
        } else if (exp instanceof RexCall) {
            // NOTE jvs 10-Feb-2005:  This is a lame hack to keep special
            // functions which return row types working.
            int j = 0;
            RexNode newExp = exp;
            List<RexNode> oldOperands = ((RexCall) exp).getOperands();
            if (oldOperands.get(0) instanceof RexInputRef) {
                RexInputRef inputRef = (RexInputRef) oldOperands.get(0);
                int newOffset = getNewForOldInput(inputRef.getIndex());
                newExp = rexBuilder.makeCall(exp.getType(), ((RexCall) exp).getOperator(), ImmutableList.of(rexBuilder.makeInputRef(inputRef.getType(), newOffset), oldOperands.get(1)));
            }
            for (RelDataTypeField field : newExp.getType().getFieldList()) {
                flattenedExps.add(Pair.of(rexBuilder.makeFieldAccess(newExp, field.getIndex()), fieldName + "$" + (j++)));
            }
        } else {
            throw Util.needToImplement(exp);
        }
    } else {
        flattenedExps.add(Pair.of(exp.accept(shuttle), fieldName));
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 25 with RexCall

use of org.apache.calcite.rex.RexCall in project calcite by apache.

the class RelOptUtil method analyzeSimpleEquiJoin.

// to be removed before 2.0
@Deprecated
public static boolean analyzeSimpleEquiJoin(LogicalJoin join, int[] joinFieldOrdinals) {
    RexNode joinExp = join.getCondition();
    if (joinExp.getKind() != SqlKind.EQUALS) {
        return false;
    }
    RexCall binaryExpression = (RexCall) joinExp;
    RexNode leftComparand = binaryExpression.operands.get(0);
    RexNode rightComparand = binaryExpression.operands.get(1);
    if (!(leftComparand instanceof RexInputRef)) {
        return false;
    }
    if (!(rightComparand instanceof RexInputRef)) {
        return false;
    }
    final int leftFieldCount = join.getLeft().getRowType().getFieldCount();
    RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
    if (!(leftFieldAccess.getIndex() < leftFieldCount)) {
        // left field must access left side of join
        return false;
    }
    RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
    if (!(rightFieldAccess.getIndex() >= leftFieldCount)) {
        // right field must access right side of join
        return false;
    }
    joinFieldOrdinals[0] = leftFieldAccess.getIndex();
    joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
    return true;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexCall (org.apache.calcite.rex.RexCall)213 RexNode (org.apache.calcite.rex.RexNode)172 RexInputRef (org.apache.calcite.rex.RexInputRef)61 ArrayList (java.util.ArrayList)59 RexLiteral (org.apache.calcite.rex.RexLiteral)44 Nullable (javax.annotation.Nullable)35 RelNode (org.apache.calcite.rel.RelNode)26 RelDataType (org.apache.calcite.rel.type.RelDataType)24 SqlOperator (org.apache.calcite.sql.SqlOperator)23 List (java.util.List)22 RexBuilder (org.apache.calcite.rex.RexBuilder)22 DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)19 SqlKind (org.apache.calcite.sql.SqlKind)14 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)14 RelOptUtil (org.apache.calcite.plan.RelOptUtil)11 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)11 RexCall (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexCall)10 RexTableInputRef (org.apache.calcite.rex.RexTableInputRef)10 TimeUnitRange (org.apache.calcite.avatica.util.TimeUnitRange)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9