use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.
the class HiveSemiJoinRule method recreateAggregateOperator.
protected static RelNode recreateAggregateOperator(RelBuilder builder, int[] adjustments, Aggregate topAggregate, RelNode newInputOperator) {
builder.push(newInputOperator);
ImmutableBitSet.Builder newGroupSet = ImmutableBitSet.builder();
for (int pos : topAggregate.getGroupSet()) {
newGroupSet.set(pos + adjustments[pos]);
}
GroupKey groupKey;
if (topAggregate.getGroupType() == Group.SIMPLE) {
groupKey = builder.groupKey(newGroupSet.build());
} else {
List<ImmutableBitSet> newGroupSets = new ArrayList<>();
for (ImmutableBitSet groupingSet : topAggregate.getGroupSets()) {
ImmutableBitSet.Builder newGroupingSet = ImmutableBitSet.builder();
for (int pos : groupingSet) {
newGroupingSet.set(pos + adjustments[pos]);
}
newGroupSets.add(newGroupingSet.build());
}
groupKey = builder.groupKey(newGroupSet.build(), newGroupSets);
}
List<AggregateCall> newAggCallList = new ArrayList<>();
for (AggregateCall aggregateCall : topAggregate.getAggCallList()) {
List<Integer> newArgList = aggregateCall.getArgList().stream().map(pos -> pos + adjustments[pos]).collect(Collectors.toList());
int newFilterArg = aggregateCall.filterArg != -1 ? aggregateCall.filterArg + adjustments[aggregateCall.filterArg] : -1;
RelCollation newCollation = aggregateCall.getCollation() != null ? RelCollations.of(aggregateCall.getCollation().getFieldCollations().stream().map(fc -> fc.withFieldIndex(fc.getFieldIndex() + adjustments[fc.getFieldIndex()])).collect(Collectors.toList())) : null;
newAggCallList.add(aggregateCall.copy(newArgList, newFilterArg, newCollation));
}
return builder.push(newInputOperator).aggregate(groupKey, newAggCallList).build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.
the class HiveRelFieldTrimmer method trimFields.
public TrimResult trimFields(HiveSortExchange exchange, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
final RelDataType rowType = exchange.getRowType();
final int fieldCount = rowType.getFieldCount();
final RelCollation collation = exchange.getCollation();
final RelDistribution distribution = exchange.getDistribution();
final RelNode input = exchange.getInput();
// We use the fields used by the consumer, plus any fields used as exchange
// keys.
final ImmutableBitSet.Builder inputFieldsUsed = fieldsUsed.rebuild();
for (RelFieldCollation field : collation.getFieldCollations()) {
inputFieldsUsed.set(field.getFieldIndex());
}
for (int keyIndex : distribution.getKeys()) {
inputFieldsUsed.set(keyIndex);
}
// Create input with trimmed columns.
final Set<RelDataTypeField> inputExtraFields = Collections.emptySet();
TrimResult trimResult = trimChild(exchange, input, inputFieldsUsed.build(), inputExtraFields);
RelNode newInput = trimResult.left;
final Mapping inputMapping = trimResult.right;
// there's nothing we can do.
if (newInput == input && inputMapping.isIdentity() && fieldsUsed.cardinality() == fieldCount) {
return result(exchange, Mappings.createIdentity(fieldCount));
}
final RelBuilder relBuilder = REL_BUILDER.get();
relBuilder.push(newInput);
RelCollation newCollation = RexUtil.apply(inputMapping, collation);
RelDistribution newDistribution = distribution.apply(inputMapping);
relBuilder.sortExchange(newDistribution, newCollation);
return result(relBuilder.build(), inputMapping);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.
the class HiveRelDecorrelator method decorrelateRel.
/**
* Rewrite Sort.
*
* @param rel Sort to be rewritten
*/
public Frame decorrelateRel(Sort rel) {
// Sort itself should not reference cor vars.
assert !cm.mapRefRelToCorRef.containsKey(rel);
// Sort only references field positions in collations field.
// The collations field in the newRel now need to refer to the
// new output positions in its input.
// Its output does not change the input ordering, so there's no
// need to call propagateExpr.
final RelNode oldInput = rel.getInput();
final Frame frame = getInvoke(oldInput, rel);
if (frame == null) {
// If input has not been rewritten, do not rewrite this rel.
return null;
}
final RelNode newInput = frame.r;
Mappings.TargetMapping mapping = Mappings.target(frame.oldToNewOutputs, oldInput.getRowType().getFieldCount(), newInput.getRowType().getFieldCount());
RelCollation oldCollation = rel.getCollation();
RelCollation newCollation = RexUtil.apply(mapping, oldCollation);
final RelNode newSort = HiveSortLimit.create(newInput, newCollation, rel.offset, rel.fetch);
// Sort does not change input ordering
return register(rel, newSort, frame.oldToNewOutputs, frame.corDefOutputs);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.
the class HiveJoin method getSortedInputs.
public ImmutableBitSet getSortedInputs() throws CalciteSemanticException {
ImmutableBitSet.Builder sortedInputsBuilder = ImmutableBitSet.builder();
JoinPredicateInfo joinPredInfo = HiveCalciteUtil.JoinPredicateInfo.constructJoinPredicateInfo(this);
List<ImmutableIntList> joinKeysInChildren = new ArrayList<ImmutableIntList>();
joinKeysInChildren.add(ImmutableIntList.copyOf(joinPredInfo.getProjsFromLeftPartOfJoinKeysInChildSchema()));
joinKeysInChildren.add(ImmutableIntList.copyOf(joinPredInfo.getProjsFromRightPartOfJoinKeysInChildSchema()));
final RelMetadataQuery mq = this.left.getCluster().getMetadataQuery();
for (int i = 0; i < this.getInputs().size(); i++) {
List<RelCollation> collations = mq.collations(this.getInputs().get(i));
if (collations != null) {
boolean correctOrderFound = RelCollations.contains(collations, joinKeysInChildren.get(i));
if (correctOrderFound) {
sortedInputsBuilder.set(i);
}
}
}
return sortedInputsBuilder.build();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelCollation in project hive by apache.
the class HiveSortExchange method create.
public static HiveSortExchange create(RelNode input, RelDistribution distribution, RelCollation collation) {
RelOptCluster cluster = input.getCluster();
distribution = RelDistributionTraitDef.INSTANCE.canonize(distribution);
collation = RelCollationTraitDef.INSTANCE.canonize(collation);
RelTraitSet traitSet = getTraitSet(cluster, collation, distribution);
RelCollation canonizedCollation = traitSet.canonize(RelCollationImpl.of(collation.getFieldCollations()));
ImmutableList.Builder<RexNode> builder = ImmutableList.builder();
for (RelFieldCollation relFieldCollation : canonizedCollation.getFieldCollations()) {
int index = relFieldCollation.getFieldIndex();
builder.add(cluster.getRexBuilder().makeInputRef(input, index));
}
return new HiveSortExchange(cluster, traitSet, input, distribution, collation, builder.build());
}
Aggregations