Search in sources :

Example 41 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.

the class HiveAlgorithmsUtil method getJoinCollation.

public static ImmutableList<RelCollation> getJoinCollation(JoinPredicateInfo joinPredInfo, MapJoinStreamingRelation streamingRelation) {
    // Compute collations
    ImmutableList.Builder<RelFieldCollation> collationListBuilder = new ImmutableList.Builder<RelFieldCollation>();
    ImmutableList.Builder<RelFieldCollation> leftCollationListBuilder = new ImmutableList.Builder<RelFieldCollation>();
    ImmutableList.Builder<RelFieldCollation> rightCollationListBuilder = new ImmutableList.Builder<RelFieldCollation>();
    for (int i = 0; i < joinPredInfo.getEquiJoinPredicateElements().size(); i++) {
        JoinLeafPredicateInfo joinLeafPredInfo = joinPredInfo.getEquiJoinPredicateElements().get(i);
        for (int leftPos : joinLeafPredInfo.getProjsFromLeftPartOfJoinKeysInJoinSchema()) {
            final RelFieldCollation leftFieldCollation = new RelFieldCollation(leftPos);
            collationListBuilder.add(leftFieldCollation);
            leftCollationListBuilder.add(leftFieldCollation);
        }
        for (int rightPos : joinLeafPredInfo.getProjsFromRightPartOfJoinKeysInJoinSchema()) {
            final RelFieldCollation rightFieldCollation = new RelFieldCollation(rightPos);
            collationListBuilder.add(rightFieldCollation);
            rightCollationListBuilder.add(rightFieldCollation);
        }
    }
    // Return join collations
    final ImmutableList<RelCollation> collation;
    switch(streamingRelation) {
        case LEFT_RELATION:
            collation = ImmutableList.of(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(leftCollationListBuilder.build())));
            break;
        case RIGHT_RELATION:
            collation = ImmutableList.of(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(rightCollationListBuilder.build())));
            break;
        default:
            collation = ImmutableList.of(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(collationListBuilder.build())));
            break;
    }
    return collation;
}
Also used : JoinLeafPredicateInfo(org.apache.hadoop.hive.ql.optimizer.calcite.HiveCalciteUtil.JoinLeafPredicateInfo) HiveRelCollation(org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelCollation) RelCollation(org.apache.calcite.rel.RelCollation) HiveRelCollation(org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelCollation) ImmutableList(com.google.common.collect.ImmutableList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Example 42 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project beam by apache.

the class CEPUtils method makeOrderKeysFromCollation.

/**
 * Transform a list of keys in Calcite to {@code ORDER BY} to {@code OrderKey}s.
 */
public static ArrayList<OrderKey> makeOrderKeysFromCollation(RelCollation orderKeys) {
    List<RelFieldCollation> relOrderKeys = orderKeys.getFieldCollations();
    ArrayList<OrderKey> orderKeysList = new ArrayList<>();
    for (RelFieldCollation i : relOrderKeys) {
        orderKeysList.add(OrderKey.of(i));
    }
    return orderKeysList;
}
Also used : RelFieldCollation(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList)

Example 43 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hazelcast by hazelcast.

the class IndexResolver method replaceCollationDirection.

/**
 * Replaces a direction in the collation trait of the rel
 *
 * @param rel       the rel
 * @param direction the collation
 * @return the rel with changed collation
 */
private static RelNode replaceCollationDirection(RelNode rel, Direction direction) {
    RelCollation collation = rel.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);
    List<RelFieldCollation> newFields = new ArrayList<>(collation.getFieldCollations().size());
    for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
        RelFieldCollation newFieldCollation = new RelFieldCollation(fieldCollation.getFieldIndex(), direction);
        newFields.add(newFieldCollation);
    }
    RelCollation newCollation = RelCollations.of(newFields);
    RelTraitSet traitSet = rel.getTraitSet();
    traitSet = OptUtils.traitPlus(traitSet, newCollation);
    return rel.copy(traitSet, rel.getInputs());
}
Also used : RelCollation(org.apache.calcite.rel.RelCollation) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 44 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hazelcast by hazelcast.

the class IndexResolver method createIndexScans.

/**
 * The main entry point for index planning.
 * <p>
 * Analyzes the filter of the input scan operator, and produces zero, one or more {@link IndexScanMapPhysicalRel}
 * operators.
 * <p>
 * First, the full index scans are created and the covered (prefix-based) scans are excluded.
 * Second, the lookups are created and if lookup's collation is equal to the full scan's collation,
 * the latter one is excluded.
 *
 * @param scan    scan operator to be analyzed
 * @param indexes indexes available on the map being scanned
 * @return zero, one or more index scan rels
 */
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity", "checkstyle:MethodLength" })
public static Collection<RelNode> createIndexScans(FullScanLogicalRel scan, List<MapTableIndex> indexes) {
    RexNode filter = OptUtils.extractHazelcastTable(scan).getFilter();
    // Filter out unsupported indexes. Only SORTED and HASH indexes are supported.
    List<MapTableIndex> supportedIndexes = new ArrayList<>(indexes.size());
    Set<Integer> allIndexedFieldOrdinals = new HashSet<>();
    for (MapTableIndex index : indexes) {
        if (isIndexSupported(index)) {
            supportedIndexes.add(index);
            allIndexedFieldOrdinals.addAll(index.getFieldOrdinals());
        }
    }
    // Early return if there are no indexes to consider.
    if (supportedIndexes.isEmpty()) {
        return Collections.emptyList();
    }
    List<RelNode> fullScanRels = new ArrayList<>(supportedIndexes.size());
    // possible ORDER BY clause on the upper level
    for (MapTableIndex index : supportedIndexes) {
        if (index.getType() == SORTED) {
            // Only for SORTED index create full index scans that might be potentially
            // utilized by sorting operator.
            List<Boolean> ascs = buildFieldDirections(index, true);
            RelNode relAscending = createFullIndexScan(scan, index, ascs, true);
            if (relAscending != null) {
                fullScanRels.add(relAscending);
                RelNode relDescending = replaceCollationDirection(relAscending, DESCENDING);
                fullScanRels.add(relDescending);
            }
        }
    }
    Map<RelCollation, RelNode> fullScanRelsMap = excludeCoveredCollations(fullScanRels);
    if (filter == null) {
        // Exclude prefix-based covered index scans
        return fullScanRelsMap.values();
    }
    // Convert expression into CNF. Examples:
    // - {a=1 AND b=2} is converted into {a=1}, {b=2}
    // - {a=1 OR b=2} is unchanged
    List<RexNode> conjunctions = createConjunctiveFilter(filter);
    // Create a map from a column to a list of expressions that could be used by indexes.
    // For example, for the expression {a>1 AND a<3 AND b=5 AND c>d}, three candidates will be created:
    // a -> {>1}, {<3}
    // b -> {=5}
    Map<Integer, List<IndexComponentCandidate>> candidates = prepareSingleColumnCandidates(conjunctions, getCluster(scan).getParameterMetadata(), allIndexedFieldOrdinals);
    if (candidates.isEmpty()) {
        return fullScanRelsMap.values();
    }
    List<RelNode> rels = new ArrayList<>(supportedIndexes.size());
    for (MapTableIndex index : supportedIndexes) {
        // Create index scan based on candidates, if possible. Candidates could be merged into more complex
        // filters whenever possible.
        List<Boolean> ascs = buildFieldDirections(index, true);
        RelNode relAscending = createIndexScan(scan, index, conjunctions, candidates, ascs);
        if (relAscending != null) {
            RelCollation relAscCollation = getCollation(relAscending);
            // Exclude a full scan that has the same collation
            fullScanRelsMap.remove(relAscCollation);
            rels.add(relAscending);
            if (relAscCollation.getFieldCollations().size() > 0) {
                RelNode relDescending = replaceCollationDirection(relAscending, DESCENDING);
                rels.add(relDescending);
                RelCollation relDescCollation = getCollation(relDescending);
                // Exclude a full scan that has the same collation
                fullScanRelsMap.remove(relDescCollation);
            }
        }
    }
    rels.addAll(fullScanRelsMap.values());
    return rels;
}
Also used : ArrayList(java.util.ArrayList) RelCollation(org.apache.calcite.rel.RelCollation) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) RelNode(org.apache.calcite.rel.RelNode) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

Example 45 with RelCollation

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hazelcast by hazelcast.

the class IndexResolver method createIndexScan.

private static IndexScanMapPhysicalRel createIndexScan(FullScanLogicalRel scan, MapTableIndex index, List<RexNode> conjunctions, List<IndexComponentFilter> filterDescriptors, List<Boolean> ascs) {
    // Collect filters and relevant expressions
    List<IndexFilter> filters = new ArrayList<>(filterDescriptors.size());
    Set<RexNode> exps = new HashSet<>();
    for (IndexComponentFilter filterDescriptor : filterDescriptors) {
        filters.add(filterDescriptor.getFilter());
        exps.addAll(filterDescriptor.getExpressions());
    }
    // Construct Calcite expressions.
    RexBuilder rexBuilder = scan.getCluster().getRexBuilder();
    RexNode exp = RexUtil.composeConjunction(rexBuilder, exps);
    List<RexNode> remainderConjunctiveExps = excludeNodes(conjunctions, exps);
    RexNode remainderExp = remainderConjunctiveExps.isEmpty() ? null : RexUtil.composeConjunction(rexBuilder, remainderConjunctiveExps);
    // Prepare traits
    RelTraitSet traitSet = scan.getTraitSet();
    // Make a collation trait
    RelCollation relCollation = buildCollationTrait(scan, index, ascs);
    traitSet = OptUtils.traitPlus(traitSet, relCollation);
    // Prepare table
    HazelcastRelOptTable originalRelTable = (HazelcastRelOptTable) scan.getTable();
    HazelcastTable originalHazelcastTable = OptUtils.extractHazelcastTable(scan);
    RelOptTable newRelTable = createRelTable(originalRelTable, originalHazelcastTable.withFilter(null), scan.getCluster().getTypeFactory());
    // Try composing the final filter out of the isolated component filters if possible
    IndexFilter filter = composeFilter(filters, index.getType(), index.getComponentsCount());
    if (filter == null) {
        return null;
    }
    // Construct the scan
    return new IndexScanMapPhysicalRel(scan.getCluster(), OptUtils.toPhysicalConvention(traitSet), newRelTable, index, filter, exp, remainderExp);
}
Also used : ArrayList(java.util.ArrayList) RelTraitSet(org.apache.calcite.plan.RelTraitSet) IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) RelCollation(org.apache.calcite.rel.RelCollation) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) RexBuilder(org.apache.calcite.rex.RexBuilder) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter) RelOptTable(org.apache.calcite.plan.RelOptTable) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

Aggregations

RelCollation (org.apache.calcite.rel.RelCollation)83 RelNode (org.apache.calcite.rel.RelNode)40 RelTraitSet (org.apache.calcite.plan.RelTraitSet)37 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)33 RexNode (org.apache.calcite.rex.RexNode)24 ArrayList (java.util.ArrayList)19 RelDataType (org.apache.calcite.rel.type.RelDataType)17 RelOptCluster (org.apache.calcite.plan.RelOptCluster)15 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)13 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)12 List (java.util.List)11 Sort (org.apache.calcite.rel.core.Sort)11 ImmutableList (com.google.common.collect.ImmutableList)8 RelDistribution (org.apache.calcite.rel.RelDistribution)8 RexInputRef (org.apache.calcite.rex.RexInputRef)8 Map (java.util.Map)7 Mappings (org.apache.calcite.util.mapping.Mappings)7 Supplier (com.google.common.base.Supplier)6 HashMap (java.util.HashMap)6