Search in sources :

Example 91 with RexCall

use of org.apache.calcite.rex.RexCall in project druid by druid-io.

the class ThetaSketchSetBaseOperatorConversion method toPostAggregator.

@Nullable
@Override
public PostAggregator toPostAggregator(PlannerContext plannerContext, RowSignature rowSignature, RexNode rexNode, PostAggregatorVisitor postAggregatorVisitor) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final List<PostAggregator> inputPostAggs = new ArrayList<>();
    Integer size = null;
    int operandCounter = 0;
    for (RexNode operand : operands) {
        final PostAggregator convertedPostAgg = OperatorConversions.toPostAggregator(plannerContext, rowSignature, operand, postAggregatorVisitor);
        if (convertedPostAgg == null) {
            if (operandCounter == 0) {
                try {
                    if (!operand.isA(SqlKind.LITERAL)) {
                        return null;
                    }
                    size = RexLiteral.intValue(operand);
                } catch (ClassCastException cce) {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            inputPostAggs.add(convertedPostAgg);
            operandCounter++;
        }
    }
    return new SketchSetPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), getSetOperationName(), size, inputPostAggs);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SketchSetPostAggregator(org.apache.druid.query.aggregation.datasketches.theta.SketchSetPostAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) SketchSetPostAggregator(org.apache.druid.query.aggregation.datasketches.theta.SketchSetPostAggregator) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 92 with RexCall

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

the class HiveCalciteUtil method getTypeSafePred.

public static RexNode getTypeSafePred(RelOptCluster cluster, RexNode rex, RelDataType rType) {
    RexNode typeSafeRex = rex;
    if ((typeSafeRex instanceof RexCall) && HiveCalciteUtil.isComparisonOp((RexCall) typeSafeRex)) {
        RexBuilder rb = cluster.getRexBuilder();
        List<RexNode> fixedPredElems = new ArrayList<RexNode>();
        RelDataType commonType = cluster.getTypeFactory().leastRestrictive(RexUtil.types(((RexCall) rex).getOperands()));
        for (RexNode rn : ((RexCall) rex).getOperands()) {
            fixedPredElems.add(rb.ensureType(commonType, rn, true));
        }
        typeSafeRex = rb.makeCall(((RexCall) typeSafeRex).getOperator(), fixedPredElems);
    }
    return typeSafeRex;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 93 with RexCall

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

the class HiveCalciteUtil method checkMaterializable.

/**
 * Check if the expression is usable for query materialization, returning the first failing expression.
 */
public static RexCall checkMaterializable(RexNode expr) {
    RexCall failingCall = null;
    if (expr == null) {
        return null;
    }
    RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {

        @Override
        public Void visitCall(org.apache.calcite.rex.RexCall call) {
            // non-deterministic functions as well as runtime constants are not materializable.
            SqlOperator op = call.getOperator();
            if (!op.isDeterministic() || op.isDynamicFunction() || (op instanceof HiveSqlFunction && ((HiveSqlFunction) op).isRuntimeConstant())) {
                throw new Util.FoundOne(call);
            }
            return super.visitCall(call);
        }
    };
    try {
        expr.accept(visitor);
    } catch (Util.FoundOne e) {
        failingCall = (RexCall) e.getNode();
    }
    return failingCall;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) HiveSqlFunction(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveSqlFunction) SqlOperator(org.apache.calcite.sql.SqlOperator) RexUtil(org.apache.calcite.rex.RexUtil) SqlValidatorUtil(org.apache.calcite.sql.validate.SqlValidatorUtil) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Util(org.apache.calcite.util.Util) RexVisitorImpl(org.apache.calcite.rex.RexVisitorImpl)

Example 94 with RexCall

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

the class HiveRelOptUtil method getNewRelFieldCollations.

/**
 * Map Sort and SortExchange keys to the specified Project columns.
 * @param project the Project
 * @param sortCollation current collation in Sort
 * @param cluster RelOptCluster
 * @return new collation should be used in the Sort
 */
public static List<RelFieldCollation> getNewRelFieldCollations(HiveProject project, RelCollation sortCollation, RelOptCluster cluster) {
    // Determine mapping between project input and output fields.
    // In Hive, Sort is always based on RexInputRef
    // HiveSort*PullUpConstantsRule should remove constants (RexLiteral)
    // We only need to check if project can contain all the positions that sortCollation needs.
    final Mappings.TargetMapping map = RelOptUtil.permutationIgnoreCast(project.getProjects(), project.getInput().getRowType()).inverse();
    Set<Integer> needed = new HashSet<>();
    for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
        needed.add(fc.getFieldIndex());
        final RexNode node = project.getProjects().get(map.getTarget(fc.getFieldIndex()));
        if (node.isA(SqlKind.CAST)) {
            // Check whether it is a monotonic preserving cast, otherwise we cannot push
            final RexCall cast = (RexCall) node;
            final RexCallBinding binding = RexCallBinding.create(cluster.getTypeFactory(), cast, ImmutableList.of(RexUtil.apply(map, sortCollation)));
            if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) {
                return null;
            }
        }
    }
    Map<Integer, Integer> m = new HashMap<>();
    for (int projPos = 0; projPos < project.getProjects().size(); projPos++) {
        RexNode expr = project.getProjects().get(projPos);
        if (expr instanceof RexInputRef) {
            Set<Integer> positions = HiveCalciteUtil.getInputRefs(expr);
            if (positions.size() <= 1) {
                int parentPos = positions.iterator().next();
                if (needed.contains(parentPos)) {
                    m.put(parentPos, projPos);
                    needed.remove(parentPos);
                }
            }
        }
    }
    if (!needed.isEmpty()) {
        return null;
    }
    List<RelFieldCollation> fieldCollations = new ArrayList<>();
    for (RelFieldCollation fc : sortCollation.getFieldCollations()) {
        fieldCollations.add(new RelFieldCollation(m.get(fc.getFieldIndex()), fc.direction, fc.nullDirection));
    }
    return fieldCollations;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RelReferentialConstraint(org.apache.calcite.rel.RelReferentialConstraint) RexCall(org.apache.calcite.rex.RexCall) RexCallBinding(org.apache.calcite.rex.RexCallBinding) Mappings(org.apache.calcite.util.mapping.Mappings) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) RexInputRef(org.apache.calcite.rex.RexInputRef) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) RexNode(org.apache.calcite.rex.RexNode)

Example 95 with RexCall

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

the class HiveRelOptUtil method splitCorrelatedFilterCondition.

private static void splitCorrelatedFilterCondition(Filter filter, RexNode condition, List<RexNode> joinKeys, List<RexNode> correlatedJoinKeys, List<RexNode> nonEquiList, boolean extractCorrelatedFieldAccess) {
    if (condition instanceof RexCall) {
        RexCall call = (RexCall) condition;
        if (call.getOperator().getKind() == SqlKind.AND) {
            for (RexNode operand : call.getOperands()) {
                splitCorrelatedFilterCondition(filter, operand, joinKeys, correlatedJoinKeys, nonEquiList, extractCorrelatedFieldAccess);
            }
            return;
        }
        if (call.getOperator().getKind() == SqlKind.EQUALS) {
            final List<RexNode> operands = call.getOperands();
            RexNode op0 = operands.get(0);
            RexNode op1 = operands.get(1);
            if (extractCorrelatedFieldAccess) {
                if (!RexUtil.containsFieldAccess(op0) && (op1 instanceof RexFieldAccess)) {
                    joinKeys.add(op0);
                    correlatedJoinKeys.add(op1);
                    return;
                } else if ((op0 instanceof RexFieldAccess) && !RexUtil.containsFieldAccess(op1)) {
                    correlatedJoinKeys.add(op0);
                    joinKeys.add(op1);
                    return;
                }
            } else {
                if (!(RexUtil.containsInputRef(op0)) && (op1 instanceof RexInputRef)) {
                    correlatedJoinKeys.add(op0);
                    joinKeys.add(op1);
                    return;
                } else if ((op0 instanceof RexInputRef) && !(RexUtil.containsInputRef(op1))) {
                    joinKeys.add(op0);
                    correlatedJoinKeys.add(op1);
                    return;
                }
            }
        }
    }
    // The operator is not of RexCall type
    // So we fail. Fall through.
    // Add this condition to the list of non-equi-join conditions.
    nonEquiList.add(condition);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexInputRef(org.apache.calcite.rex.RexInputRef) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) 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