Search in sources :

Example 71 with ImmutableBitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.

the class HiveRelBuilder method join.

/**
 * Creates a {@link Join} with correlating variables.
 */
@Override
public RelBuilder join(JoinRelType joinType, RexNode condition, Set<CorrelationId> variablesSet) {
    if (Bug.CALCITE_4574_FIXED) {
        throw new IllegalStateException("Method overriding should be removed once CALCITE-4574 is fixed");
    }
    RelNode right = this.peek(0);
    RelNode left = this.peek(1);
    final boolean correlate = variablesSet.size() == 1;
    RexNode postCondition = literal(true);
    if (correlate) {
        final CorrelationId id = Iterables.getOnlyElement(variablesSet);
        if (!RelOptUtil.notContainsCorrelation(left, id, Litmus.IGNORE)) {
            throw new IllegalArgumentException("variable " + id + " must not be used by left input to correlation");
        }
        // Correlate does not have an ON clause.
        switch(joinType) {
            case LEFT:
            case SEMI:
            case ANTI:
                // For a LEFT/SEMI/ANTI, predicate must be evaluated first.
                filter(condition.accept(new Shifter(left, id, right)));
                right = this.peek(0);
                break;
            case INNER:
                // For INNER, we can defer.
                postCondition = condition;
                break;
            default:
                throw new IllegalArgumentException("Correlated " + joinType + " join is not supported");
        }
        final ImmutableBitSet requiredColumns = RelOptUtil.correlationColumns(id, right);
        List<RexNode> leftFields = this.fields(2, 0);
        List<RexNode> requiredFields = new ArrayList<>();
        for (int i = 0; i < leftFields.size(); i++) {
            if (requiredColumns.get(i)) {
                requiredFields.add(leftFields.get(i));
            }
        }
        correlate(joinType, id, requiredFields);
        filter(postCondition);
    } else {
        // Frame etc. and we might lose existing aliases in the builder
        assert variablesSet.isEmpty();
        super.join(joinType, condition, variablesSet);
    }
    return this;
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ArrayList(java.util.ArrayList) CorrelationId(org.apache.calcite.rel.core.CorrelationId) RexNode(org.apache.calcite.rex.RexNode)

Example 72 with ImmutableBitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.

the class HiveRelMdRowCount method canHandleJoin.

/*
   * 1. Join condition must be an Equality Predicate.
   * 2. both sides must reference 1 column.
   * 3. If needed flip the columns.
   */
private static Pair<Integer, Integer> canHandleJoin(Join joinRel, List<RexNode> leftFilters, List<RexNode> rightFilters, List<RexNode> joinFilters) {
    /*
     * If after classifying filters there is more than 1 joining predicate, we
     * don't handle this. Return null.
     */
    if (joinFilters.size() != 1) {
        return null;
    }
    RexNode joinCond = joinFilters.get(0);
    int leftColIdx;
    int rightColIdx;
    if (!(joinCond instanceof RexCall)) {
        return null;
    }
    if (((RexCall) joinCond).getOperator() != SqlStdOperatorTable.EQUALS) {
        return null;
    }
    ImmutableBitSet leftCols = RelOptUtil.InputFinder.bits(((RexCall) joinCond).getOperands().get(0));
    ImmutableBitSet rightCols = RelOptUtil.InputFinder.bits(((RexCall) joinCond).getOperands().get(1));
    if (leftCols.cardinality() != 1 || rightCols.cardinality() != 1) {
        return null;
    }
    int nFieldsLeft = joinRel.getLeft().getRowType().getFieldList().size();
    int nFieldsRight = joinRel.getRight().getRowType().getFieldList().size();
    int nSysFields = joinRel.getSystemFieldList().size();
    ImmutableBitSet rightFieldsBitSet = ImmutableBitSet.range(nSysFields + nFieldsLeft, nSysFields + nFieldsLeft + nFieldsRight);
    /*
     * flip column references if join condition specified in reverse order to
     * join sources.
     */
    if (rightFieldsBitSet.contains(leftCols)) {
        ImmutableBitSet t = leftCols;
        leftCols = rightCols;
        rightCols = t;
    }
    leftColIdx = leftCols.nextSetBit(0) - nSysFields;
    rightColIdx = rightCols.nextSetBit(0) - (nSysFields + nFieldsLeft);
    return new Pair<Integer, Integer>(leftColIdx, rightColIdx);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RexNode(org.apache.calcite.rex.RexNode) Pair(org.apache.calcite.util.Pair)

Example 73 with ImmutableBitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.

the class HiveRelMdUniqueKeys method getUniqueKeys.

public Set<ImmutableBitSet> getUniqueKeys(HiveTableScan rel, RelMetadataQuery mq, boolean ignoreNulls) {
    RelOptHiveTable tbl = (RelOptHiveTable) rel.getTable();
    List<ImmutableBitSet> keyList = tbl.getNonNullableKeys();
    if (keyList != null) {
        Set<ImmutableBitSet> keySet = new HashSet<>(keyList);
        return keySet;
    }
    return null;
}
Also used : RelOptHiveTable(org.apache.hadoop.hive.ql.optimizer.calcite.RelOptHiveTable) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashSet(java.util.HashSet)

Example 74 with ImmutableBitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.

the class EstimateUniqueKeys method getUniqueKeys.

private static Set<ImmutableBitSet> getUniqueKeys(HiveJoin rel) {
    RelNode left = getRelNode(rel.getLeft());
    RelNode right = getRelNode(rel.getRight());
    // first add the different combinations of concatenated unique keys
    // from the left and the right, adjusting the right hand side keys to
    // reflect the addition of the left hand side
    // 
    // NOTE zfong 12/18/06 - If the number of tables in a join is large,
    // the number of combinations of unique key sets will explode.  If
    // that is undesirable, use RelMetadataQuery.areColumnsUnique() as
    // an alternative way of getting unique key information.
    final Set<ImmutableBitSet> retSet = new HashSet<>();
    final Set<ImmutableBitSet> leftSet = getUniqueKeys(left);
    Set<ImmutableBitSet> rightSet = null;
    final Set<ImmutableBitSet> tmpRightSet = getUniqueKeys(right);
    int nFieldsOnLeft = left.getRowType().getFieldCount();
    if (tmpRightSet != null) {
        rightSet = new HashSet<>();
        for (ImmutableBitSet colMask : tmpRightSet) {
            ImmutableBitSet.Builder tmpMask = ImmutableBitSet.builder();
            for (int bit : colMask) {
                tmpMask.set(bit + nFieldsOnLeft);
            }
            rightSet.add(tmpMask.build());
        }
        if (leftSet != null) {
            for (ImmutableBitSet colMaskRight : rightSet) {
                for (ImmutableBitSet colMaskLeft : leftSet) {
                    retSet.add(colMaskLeft.union(colMaskRight));
                }
            }
        }
    }
    // locate the columns that participate in equijoins
    final JoinInfo joinInfo = rel.analyzeCondition();
    RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    // determine if either or both the LHS and RHS are unique on the
    // equijoin columns
    final Boolean leftUnique = mq.areColumnsUnique(left, joinInfo.leftSet());
    final Boolean rightUnique = mq.areColumnsUnique(right, joinInfo.rightSet());
    // generating
    if ((rightUnique != null) && rightUnique && (leftSet != null) && !(rel.getJoinType().generatesNullsOnLeft())) {
        retSet.addAll(leftSet);
    }
    // same as above except left and right are reversed
    if ((leftUnique != null) && leftUnique && (rightSet != null) && !(rel.getJoinType().generatesNullsOnRight())) {
        retSet.addAll(rightSet);
    }
    return retSet;
}
Also used : JoinInfo(org.apache.calcite.rel.core.JoinInfo) RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) RelNode(org.apache.calcite.rel.RelNode) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashSet(java.util.HashSet)

Example 75 with ImmutableBitSet

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.

the class EstimateUniqueKeys method generateKeysUsingStatsEstimation.

// Infer Uniquenes if: - rowCount(col) = ndv(col) - TBD for numerics: max(col)
// - min(col) = rowCount(col)
private static Set<ImmutableBitSet> generateKeysUsingStatsEstimation(Project rel, HiveTableScan tScan) {
    Map<Integer, Integer> posMap = new HashMap<Integer, Integer>();
    int projectPos = 0;
    int colStatsPos = 0;
    BitSet projectedCols = new BitSet();
    for (RexNode r : rel.getProjects()) {
        if (r instanceof RexInputRef) {
            projectedCols.set(((RexInputRef) r).getIndex());
            posMap.put(colStatsPos, projectPos);
            colStatsPos++;
        }
        projectPos++;
    }
    RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    double numRows = mq.getRowCount(tScan);
    List<ColStatistics> colStats = tScan.getColStat(BitSets.toList(projectedCols));
    Set<ImmutableBitSet> keys = new HashSet<ImmutableBitSet>();
    colStatsPos = 0;
    for (ColStatistics cStat : colStats) {
        boolean isKey = false;
        if (!cStat.isEstimated()) {
            if (cStat.getCountDistint() >= numRows) {
                isKey = true;
            }
            if (!isKey && cStat.getRange() != null && cStat.getRange().maxValue != null && cStat.getRange().minValue != null) {
                double r = cStat.getRange().maxValue.doubleValue() - cStat.getRange().minValue.doubleValue() + 1;
                isKey = (Math.abs(numRows - r) < RelOptUtil.EPSILON);
            }
            if (isKey) {
                ImmutableBitSet key = ImmutableBitSet.of(posMap.get(colStatsPos));
                keys.add(key);
            }
        }
        colStatsPos++;
    }
    return keys;
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HashMap(java.util.HashMap) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) BitSet(java.util.BitSet) ColStatistics(org.apache.hadoop.hive.ql.plan.ColStatistics) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

Aggregations

ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)208 RexNode (org.apache.calcite.rex.RexNode)127 RelNode (org.apache.calcite.rel.RelNode)110 ArrayList (java.util.ArrayList)101 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)66 RexBuilder (org.apache.calcite.rex.RexBuilder)60 AggregateCall (org.apache.calcite.rel.core.AggregateCall)55 RexInputRef (org.apache.calcite.rex.RexInputRef)45 RelDataType (org.apache.calcite.rel.type.RelDataType)39 HashMap (java.util.HashMap)36 RelBuilder (org.apache.calcite.tools.RelBuilder)36 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)30 Mapping (org.apache.calcite.util.mapping.Mapping)30 Pair (org.apache.calcite.util.Pair)29 Aggregate (org.apache.calcite.rel.core.Aggregate)27 ImmutableList (com.google.common.collect.ImmutableList)23 LinkedHashSet (java.util.LinkedHashSet)23 List (java.util.List)22 HashSet (java.util.HashSet)20 RelOptUtil (org.apache.calcite.plan.RelOptUtil)18