Search in sources :

Example 56 with RexNode

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

the class FilterSelectivityEstimator method computeDisjunctionSelectivity.

/**
   * Disjunction Selectivity -> (1 D(1-m1/n)(1-m2/n)) where n is the total
   * number of tuples from child and m1 and m2 is the expected number of tuples
   * from each part of the disjunction predicate.
   * <p>
   * Note we compute m1. m2.. by applying selectivity of the disjunctive element
   * on the cardinality from child.
   * 
   * @param call
   * @return
   */
private Double computeDisjunctionSelectivity(RexCall call) {
    Double tmpCardinality;
    Double tmpSelectivity;
    double selectivity = 1;
    for (RexNode dje : call.getOperands()) {
        tmpSelectivity = dje.accept(this);
        if (tmpSelectivity == null) {
            tmpSelectivity = 0.99;
        }
        tmpCardinality = childCardinality * tmpSelectivity;
        if (tmpCardinality > 1 && tmpCardinality < childCardinality) {
            tmpSelectivity = (1 - tmpCardinality / childCardinality);
        } else {
            tmpSelectivity = 1.0;
        }
        selectivity *= tmpSelectivity;
    }
    if (selectivity < 0.0)
        selectivity = 0.0;
    return (1 - selectivity);
}
Also used : RexNode(org.apache.calcite.rex.RexNode)

Example 57 with RexNode

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

the class HiveFilterAggregateTransposeRule method matches.

@Override
public boolean matches(RelOptRuleCall call) {
    final Filter filterRel = call.rel(0);
    RexNode condition = filterRel.getCondition();
    if (!HiveCalciteUtil.isDeterministic(condition)) {
        return false;
    }
    return super.matches(call);
}
Also used : Filter(org.apache.calcite.rel.core.Filter) RexNode(org.apache.calcite.rex.RexNode)

Example 58 with RexNode

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

the class HiveFilterJoinRule method validateJoinFilters.

/*
   * Any predicates pushed down to joinFilters that aren't equality conditions:
   * put them back as aboveFilters because Hive doesn't support not equi join
   * conditions.
   */
@Override
protected void validateJoinFilters(List<RexNode> aboveFilters, List<RexNode> joinFilters, Join join, JoinRelType joinType) {
    if (joinType.equals(JoinRelType.INNER)) {
        ListIterator<RexNode> filterIter = joinFilters.listIterator();
        while (filterIter.hasNext()) {
            RexNode exp = filterIter.next();
            if (exp instanceof RexCall) {
                RexCall c = (RexCall) exp;
                boolean validHiveJoinFilter = false;
                if ((c.getOperator().getKind() == SqlKind.EQUALS)) {
                    validHiveJoinFilter = true;
                    for (RexNode rn : c.getOperands()) {
                        // (r1.x +r2.x)=(r1.y+r2.y) on join condition.
                        if (filterRefersToBothSidesOfJoin(rn, join)) {
                            validHiveJoinFilter = false;
                            break;
                        }
                    }
                } else if ((c.getOperator().getKind() == SqlKind.LESS_THAN) || (c.getOperator().getKind() == SqlKind.GREATER_THAN) || (c.getOperator().getKind() == SqlKind.LESS_THAN_OR_EQUAL) || (c.getOperator().getKind() == SqlKind.GREATER_THAN_OR_EQUAL)) {
                    validHiveJoinFilter = true;
                    // r2.x) on join condition.
                    if (filterRefersToBothSidesOfJoin(c, join)) {
                        validHiveJoinFilter = false;
                    }
                }
                if (validHiveJoinFilter)
                    continue;
            }
            aboveFilters.add(exp);
            filterIter.remove();
        }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexNode(org.apache.calcite.rex.RexNode)

Example 59 with RexNode

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

the class HiveFilterProjectTransposeRule method onMatch.

public void onMatch(RelOptRuleCall call) {
    final Filter filter = call.rel(0);
    final Project origproject = call.rel(1);
    RexNode filterCondToPushBelowProj = filter.getCondition();
    RexNode unPushedFilCondAboveProj = null;
    if (RexUtil.containsCorrelation(filterCondToPushBelowProj)) {
        // Correlate from being de-correlated.
        return;
    }
    if (RexOver.containsOver(origproject.getProjects(), null)) {
        RexNode origFilterCond = filterCondToPushBelowProj;
        filterCondToPushBelowProj = null;
        if (pushThroughWindowing) {
            Set<Integer> commonPartitionKeys = getCommonPartitionCols(origproject.getProjects());
            List<RexNode> newPartKeyFilConds = new ArrayList<RexNode>();
            List<RexNode> unpushedFilConds = new ArrayList<RexNode>();
            // from t1 where value < 10)t1)t2
            if (!commonPartitionKeys.isEmpty()) {
                for (RexNode ce : RelOptUtil.conjunctions(origFilterCond)) {
                    RexNode newCondition = RelOptUtil.pushPastProject(ce, origproject);
                    if (HiveCalciteUtil.isDeterministicFuncWithSingleInputRef(newCondition, commonPartitionKeys)) {
                        newPartKeyFilConds.add(newCondition);
                    } else {
                        unpushedFilConds.add(ce);
                    }
                }
                if (!newPartKeyFilConds.isEmpty()) {
                    filterCondToPushBelowProj = RexUtil.composeConjunction(filter.getCluster().getRexBuilder(), newPartKeyFilConds, true);
                }
                if (!unpushedFilConds.isEmpty()) {
                    unPushedFilCondAboveProj = RexUtil.composeConjunction(filter.getCluster().getRexBuilder(), unpushedFilConds, true);
                }
            }
        }
    }
    if (filterCondToPushBelowProj != null) {
        RelNode newProjRel = getNewProject(filterCondToPushBelowProj, unPushedFilCondAboveProj, origproject, filter.getCluster().getTypeFactory(), call.builder());
        call.transformTo(newProjRel);
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) ArrayList(java.util.ArrayList) RexNode(org.apache.calcite.rex.RexNode)

Example 60 with RexNode

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

the class HiveFilterProjectTransposeRule method getNewProject.

private static RelNode getNewProject(RexNode filterCondToPushBelowProj, RexNode unPushedFilCondAboveProj, Project oldProj, RelDataTypeFactory typeFactory, RelBuilder relBuilder) {
    // convert the filter to one that references the child of the project
    RexNode newPushedCondition = RelOptUtil.pushPastProject(filterCondToPushBelowProj, oldProj);
    // other rewrites.
    if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) {
        newPushedCondition = ((RexCall) newPushedCondition).getOperands().get(0);
    }
    RelNode newPushedFilterRel = relBuilder.push(oldProj.getInput()).filter(newPushedCondition).build();
    RelNode newProjRel = relBuilder.push(newPushedFilterRel).project(oldProj.getProjects(), oldProj.getRowType().getFieldNames()).build();
    if (unPushedFilCondAboveProj != null) {
        // other rewrites.
        if (RexUtil.isNullabilityCast(typeFactory, newPushedCondition)) {
            unPushedFilCondAboveProj = ((RexCall) unPushedFilCondAboveProj).getOperands().get(0);
        }
        newProjRel = relBuilder.push(newProjRel).filter(unPushedFilCondAboveProj).build();
    }
    return newProjRel;
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelNode(org.apache.calcite.rel.RelNode) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexNode (org.apache.calcite.rex.RexNode)204 RelNode (org.apache.calcite.rel.RelNode)75 ArrayList (java.util.ArrayList)68 RexBuilder (org.apache.calcite.rex.RexBuilder)52 RelDataType (org.apache.calcite.rel.type.RelDataType)48 RexInputRef (org.apache.calcite.rex.RexInputRef)43 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)41 RexCall (org.apache.calcite.rex.RexCall)32 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)28 Pair (org.apache.calcite.util.Pair)22 HashMap (java.util.HashMap)21 AggregateCall (org.apache.calcite.rel.core.AggregateCall)21 RexLiteral (org.apache.calcite.rex.RexLiteral)19 ImmutableList (com.google.common.collect.ImmutableList)18 Project (org.apache.calcite.rel.core.Project)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 Filter (org.apache.calcite.rel.core.Filter)12 HashSet (java.util.HashSet)11 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)11 BigDecimal (java.math.BigDecimal)10