use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveSortJoinReduceRule method matches.
// ~ Methods ----------------------------------------------------------------
@Override
public boolean matches(RelOptRuleCall call) {
final HiveSortLimit sortLimit = call.rel(0);
final HiveJoin join = call.rel(1);
// If sort does not contain a limit operation or limit is 0, we bail out
if (!HiveCalciteUtil.limitRelNode(sortLimit) || RexLiteral.intValue(sortLimit.fetch) == 0) {
return false;
}
// 1) If join is not a left or right outer, we bail out
// 2) If any sort column is not part of the input where the
// sort is pushed, we bail out
RelNode reducedInput;
if (join.getJoinType() == JoinRelType.LEFT) {
reducedInput = join.getLeft();
if (sortLimit.getCollation() != RelCollations.EMPTY) {
for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) {
if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) {
return false;
}
}
}
} else if (join.getJoinType() == JoinRelType.RIGHT) {
reducedInput = join.getRight();
if (sortLimit.getCollation() != RelCollations.EMPTY) {
for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) {
if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) {
return false;
}
}
}
} else {
return false;
}
// Finally, if we do not reduce the input size, we bail out
final int offset = sortLimit.offset == null ? 0 : RexLiteral.intValue(sortLimit.offset);
final RelMetadataQuery mq = call.getMetadataQuery();
if (offset + RexLiteral.intValue(sortLimit.fetch) >= mq.getRowCount(reducedInput)) {
return false;
}
return true;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveRelColumnsAlignment method align.
public RelNode align(Project rel, List<RelFieldCollation> collations) {
// 1) We extract the collations indices
boolean containsWindowing = false;
for (RexNode childExp : rel.getProjects()) {
if (childExp instanceof RexOver) {
// TODO: support propagation for partitioning/ordering in windowing
containsWindowing = true;
break;
}
}
ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder();
if (!containsWindowing) {
for (RelFieldCollation c : collations) {
RexNode rexNode = rel.getProjects().get(c.getFieldIndex());
if (rexNode instanceof RexInputRef) {
int newIdx = ((RexInputRef) rexNode).getIndex();
propagateCollations.add(c.copy((newIdx)));
}
}
}
// 2) We propagate
final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build());
// 3) Return new Project
return rel.copy(rel.getTraitSet(), ImmutableList.of(child));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveProjectSortExchangeTransposeRule method onMatch.
// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
final HiveProject project = call.rel(0);
final HiveSortExchange sortExchange = call.rel(1);
final RelOptCluster cluster = project.getCluster();
List<RelFieldCollation> fieldCollations = getNewRelFieldCollations(project, sortExchange.getCollation(), cluster);
if (fieldCollations == null) {
return;
}
RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize(RelCollationImpl.of(fieldCollations));
List<Integer> newDistributionKeys = getNewRelDistributionKeys(project, sortExchange.getDistribution());
RelDistribution newDistribution = RelDistributionTraitDef.INSTANCE.canonize(new HiveRelDistribution(sortExchange.getDistribution().getType(), newDistributionKeys));
RelTraitSet newTraitSet = TraitsUtil.getDefaultTraitSet(sortExchange.getCluster()).replace(newCollation).replace(newDistribution);
// New operators
final RelNode newProject = project.copy(sortExchange.getInput().getTraitSet(), ImmutableList.of(sortExchange.getInput()));
final SortExchange newSort = sortExchange.copy(newTraitSet, newProject, newDistribution, newCollation);
call.transformTo(newSort);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveProjectSortTransposeRule method onMatch.
// ~ Methods ----------------------------------------------------------------
// implement RelOptRule
public void onMatch(RelOptRuleCall call) {
final HiveProject project = call.rel(0);
final HiveSortLimit sort = call.rel(1);
final RelOptCluster cluster = project.getCluster();
List<RelFieldCollation> fieldCollations = getNewRelFieldCollations(project, sort.getCollation(), cluster);
if (fieldCollations == null) {
return;
}
RelTraitSet traitSet = sort.getCluster().traitSetOf(HiveRelNode.CONVENTION);
RelCollation newCollation = traitSet.canonize(RelCollationImpl.of(fieldCollations));
// New operators
final RelNode newProject = project.copy(sort.getInput().getTraitSet(), ImmutableList.of(sort.getInput()));
final HiveSortLimit newSort = sort.copy(newProject.getTraitSet(), newProject, newCollation, sort.offset, sort.fetch);
call.transformTo(newSort);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveAggregateSortLimitRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final HiveSortLimit sortLimit = call.rel(0);
final HiveAggregate aggregate = call.rel(1);
final RelBuilder relBuilder = call.builder();
if (sortLimit.getFetchExpr() == null && sortLimit.getOffsetExpr() == null) {
// No limit, offset -> No Top N -> all rows are forwarded from all RS to Reducers
return;
}
if (aggregate.getAggCallList().isEmpty()) {
// No aggregate functions, any Group By key can be in the final result
return;
}
if (!sortLimit.getSortExps().isEmpty()) {
// Sort keys already present
return;
}
ImmutableList.Builder<RelFieldCollation> newSortKeyBuilder = ImmutableList.builder();
for (int i : aggregate.getGroupSet()) {
RelFieldCollation fieldCollation = new RelFieldCollation(i, RelFieldCollation.Direction.ASCENDING, defaultAscNullDirection);
newSortKeyBuilder.add(fieldCollation);
}
HiveRelCollation newCollation = new HiveRelCollation(newSortKeyBuilder.build());
HiveSortLimit newSortLimit = sortLimit.copy(sortLimit.getTraitSet(), aggregate, newCollation, sortLimit.offset, sortLimit.fetch);
call.transformTo(newSortLimit);
}
Aggregations