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;
}
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;
}
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());
}
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;
}
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);
}
Aggregations