Search in sources :

Example 51 with RexNode

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

the class HiveProjectFilterPullUpConstantsRule method onMatch.

public void onMatch(RelOptRuleCall call) {
    final Project project = call.rel(0);
    final Filter filter = call.rel(1);
    final RelBuilder builder = call.builder();
    List<RexNode> projects = project.getChildExps();
    List<RexNode> newProjects = rewriteProjects(projects, filter.getCondition(), builder);
    if (newProjects == null) {
        return;
    }
    RelNode newProjRel = builder.push(filter).project(newProjects, project.getRowType().getFieldNames()).build();
    call.transformTo(newProjRel);
}
Also used : Project(org.apache.calcite.rel.core.Project) HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) Filter(org.apache.calcite.rel.core.Filter) HiveFilter(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveFilter) RexNode(org.apache.calcite.rex.RexNode)

Example 52 with RexNode

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

the class HiveProjectFilterPullUpConstantsRule method matches.

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

Example 53 with RexNode

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

the class HiveFilter method traverseFilter.

//traverse the given node to find all correlated variables
// Note that correlated variables are supported in Filter only i.e. Where & Having
private static void traverseFilter(RexNode node, Set<CorrelationId> allVars) {
    if (node instanceof RexSubQuery) {
        //we expect correlated variables in HiveFilter only for now.
        // Also check for case where operator has 0 inputs .e.g TableScan
        RelNode input = ((RexSubQuery) node).rel.getInput(0);
        while (input != null && !(input instanceof HiveFilter) && input.getInputs().size() >= 1) {
            // we only expect cor vars in top level filter
            if (input.getInputs().size() > 1) {
                return;
            }
            input = input.getInput(0);
        }
        if (input != null && input instanceof HiveFilter) {
            findCorrelatedVar(((HiveFilter) input).getCondition(), allVars);
        }
        return;
    }
    //AND, NOT etc
    if (node instanceof RexCall) {
        int numOperands = ((RexCall) node).getOperands().size();
        for (int i = 0; i < numOperands; i++) {
            RexNode op = ((RexCall) node).getOperands().get(i);
            traverseFilter(op, allVars);
        }
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RelNode(org.apache.calcite.rel.RelNode) RexSubQuery(org.apache.calcite.rex.RexSubQuery) RexNode(org.apache.calcite.rex.RexNode)

Example 54 with RexNode

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

the class HiveTableScan method project.

@Override
public RelNode project(ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields, RelBuilder relBuilder) {
    // 1. If the schema is the same then bail out
    final int fieldCount = getRowType().getFieldCount();
    if (fieldsUsed.equals(ImmutableBitSet.range(fieldCount)) && extraFields.isEmpty()) {
        return this;
    }
    // 2. Make sure there is no dynamic addition of virtual cols
    if (extraFields != null && !extraFields.isEmpty()) {
        throw new RuntimeException("Hive TS does not support adding virtual columns dynamically");
    }
    // 3. Create new TS schema that is a subset of original
    final List<RelDataTypeField> fields = getRowType().getFieldList();
    List<RelDataType> fieldTypes = new LinkedList<RelDataType>();
    List<String> fieldNames = new LinkedList<String>();
    List<RexNode> exprList = new ArrayList<RexNode>();
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    for (int i : fieldsUsed) {
        RelDataTypeField field = fields.get(i);
        fieldTypes.add(field.getType());
        fieldNames.add(field.getName());
        exprList.add(rexBuilder.makeInputRef(this, i));
    }
    // 4. Build new TS
    HiveTableScan newHT = copy(getCluster().getTypeFactory().createStructType(fieldTypes, fieldNames));
    // 5. Add Proj on top of TS
    HiveProject hp = (HiveProject) relBuilder.push(newHT).project(exprList, new ArrayList<String>(fieldNames)).build();
    // 6. Set synthetic flag, so that we would push filter below this one
    hp.setSynthetic();
    return hp;
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) LinkedList(java.util.LinkedList) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexBuilder(org.apache.calcite.rex.RexBuilder) RexNode(org.apache.calcite.rex.RexNode)

Example 55 with RexNode

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

the class SubstitutionVisitor method splitFilter.

/**
   * Maps a condition onto a target.
   *
   * <p>If condition is stronger than target, returns the residue.
   * If it is equal to target, returns the expression that evaluates to
   * the constant {@code true}. If it is weaker than target, returns
   * {@code null}.</p>
   *
   * <p>The terms satisfy the relation</p>
   *
   * <pre>
   *     {@code condition = target AND residue}
   * </pre>
   *
   * <p>and {@code residue} must be as weak as possible.</p>
   *
   * <p>Example #1: condition stronger than target</p>
   * <ul>
   * <li>condition: x = 1 AND y = 2</li>
   * <li>target: x = 1</li>
   * <li>residue: y = 2</li>
   * </ul>
   *
   * <p>Note that residue {@code x &gt; 0 AND y = 2} would also satisfy the
   * relation {@code condition = target AND residue} but is stronger than
   * necessary, so we prefer {@code y = 2}.</p>
   *
   * <p>Example #2: target weaker than condition (valid, but not currently
   * implemented)</p>
   * <ul>
   * <li>condition: x = 1</li>
   * <li>target: x = 1 OR z = 3</li>
   * <li>residue: NOT (z = 3)</li>
   * </ul>
   *
   * <p>Example #3: condition and target are equivalent</p>
   * <ul>
   * <li>condition: x = 1 AND y = 2</li>
   * <li>target: y = 2 AND x = 1</li>
   * <li>residue: TRUE</li>
   * </ul>
   *
   * <p>Example #4: condition weaker than target</p>
   * <ul>
   * <li>condition: x = 1</li>
   * <li>target: x = 1 AND y = 2</li>
   * <li>residue: null (i.e. no match)</li>
   * </ul>
   *
   * <p>There are many other possible examples. It amounts to solving
   * whether {@code condition AND NOT target} can ever evaluate to
   * true, and therefore is a form of the NP-complete
   * <a href="http://en.wikipedia.org/wiki/Satisfiability">Satisfiability</a>
   * problem.</p>
   */
@VisibleForTesting
public static RexNode splitFilter(final RexBuilder rexBuilder, RexNode condition, RexNode target) {
    // First, try splitting into ORs.
    // Given target    c1 OR c2 OR c3 OR c4
    // and condition   c2 OR c4
    // residue is      NOT c1 AND NOT c3
    // Also deals with case target [x] condition [x] yields residue [true].
    RexNode z = splitOr(rexBuilder, condition, target);
    if (z != null) {
        return z;
    }
    RexNode x = andNot(rexBuilder, target, condition);
    if (mayBeSatisfiable(x)) {
        RexNode x2 = andNot(rexBuilder, condition, target);
        return simplify(rexBuilder, x2);
    }
    return null;
}
Also used : RexNode(org.apache.calcite.rex.RexNode) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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