use of org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class RelOptHiveTable method getCollationList.
@Override
public List<RelCollation> getCollationList() {
ImmutableList.Builder<RelFieldCollation> collationList = new ImmutableList.Builder<RelFieldCollation>();
for (Order sortColumn : this.hiveTblMetadata.getSortCols()) {
for (int i = 0; i < this.hiveTblMetadata.getSd().getCols().size(); i++) {
FieldSchema field = this.hiveTblMetadata.getSd().getCols().get(i);
if (field.getName().equals(sortColumn.getCol())) {
Direction direction;
NullDirection nullDirection;
if (sortColumn.getOrder() == BaseSemanticAnalyzer.HIVE_COLUMN_ORDER_ASC) {
direction = Direction.ASCENDING;
nullDirection = NullDirection.FIRST;
} else {
direction = Direction.DESCENDING;
nullDirection = NullDirection.LAST;
}
collationList.add(new RelFieldCollation(i, direction, nullDirection));
break;
}
}
}
return new ImmutableList.Builder<RelCollation>().add(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(collationList.build()))).build();
}
use of org.apache.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveRelMdCollation method collations.
public ImmutableList<RelCollation> collations(HiveAggregate aggregate, RelMetadataQuery mq) {
// Compute collations
ImmutableList.Builder<RelFieldCollation> collationListBuilder = new ImmutableList.Builder<RelFieldCollation>();
for (int pos : aggregate.getGroupSet().asList()) {
final RelFieldCollation fieldCollation = new RelFieldCollation(pos);
collationListBuilder.add(fieldCollation);
}
// Return aggregate collations
return ImmutableList.of(RelCollationTraitDef.INSTANCE.canonize(new HiveRelCollation(collationListBuilder.build())));
}
use of 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.getChildExps()) {
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.getChildExps().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.calcite.rel.RelFieldCollation in project hive by apache.
the class HiveSortLimitPullUpConstantsRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final RelNode parent = call.rel(0);
final Sort sort = call.rel(1);
final int count = sort.getInput().getRowType().getFieldCount();
if (count == 1) {
// Project operator.
return;
}
final RexBuilder rexBuilder = sort.getCluster().getRexBuilder();
final RelMetadataQuery mq = RelMetadataQuery.instance();
final RelOptPredicateList predicates = mq.getPulledUpPredicates(sort.getInput());
if (predicates == null) {
return;
}
Map<RexNode, RexNode> conditionsExtracted = HiveReduceExpressionsRule.predicateConstants(RexNode.class, rexBuilder, predicates);
Map<RexNode, RexNode> constants = new HashMap<>();
for (int i = 0; i < count; i++) {
RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
if (conditionsExtracted.containsKey(expr)) {
constants.put(expr, conditionsExtracted.get(expr));
}
}
// None of the expressions are constant. Nothing to do.
if (constants.isEmpty()) {
return;
}
if (count == constants.size()) {
// At least a single item in project is required.
constants.remove(constants.keySet().iterator().next());
}
// Create expressions for Project operators before and after the Sort
List<RelDataTypeField> fields = sort.getInput().getRowType().getFieldList();
List<Pair<RexNode, String>> newChildExprs = new ArrayList<>();
List<RexNode> topChildExprs = new ArrayList<>();
List<String> topChildExprsFields = new ArrayList<>();
for (int i = 0; i < count; i++) {
RexNode expr = rexBuilder.makeInputRef(sort.getInput(), i);
RelDataTypeField field = fields.get(i);
if (constants.containsKey(expr)) {
topChildExprs.add(constants.get(expr));
topChildExprsFields.add(field.getName());
} else {
newChildExprs.add(Pair.<RexNode, String>of(expr, field.getName()));
topChildExprs.add(expr);
topChildExprsFields.add(field.getName());
}
}
// Update field collations
final Mappings.TargetMapping mapping = RelOptUtil.permutation(Pair.left(newChildExprs), sort.getInput().getRowType()).inverse();
List<RelFieldCollation> fieldCollations = new ArrayList<>();
for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
final int target = mapping.getTargetOpt(fc.getFieldIndex());
if (target < 0) {
// It is a constant, we can ignore it
continue;
}
fieldCollations.add(fc.copy(target));
}
// Update top Project positions
topChildExprs = ImmutableList.copyOf(RexUtil.apply(mapping, topChildExprs));
// Create new Project-Sort-Project sequence
final RelBuilder relBuilder = call.builder();
relBuilder.push(sort.getInput());
relBuilder.project(Pair.left(newChildExprs), Pair.right(newChildExprs));
final ImmutableList<RexNode> sortFields = relBuilder.fields(RelCollations.of(fieldCollations));
relBuilder.sortLimit(sort.offset == null ? -1 : RexLiteral.intValue(sort.offset), sort.fetch == null ? -1 : RexLiteral.intValue(sort.fetch), sortFields);
// Create top Project fixing nullability of fields
relBuilder.project(topChildExprs, topChildExprsFields);
relBuilder.convert(sort.getRowType(), false);
List<RelNode> inputs = new ArrayList<>();
for (RelNode child : parent.getInputs()) {
if (!((HepRelVertex) child).getCurrentRel().equals(sort)) {
inputs.add(child);
} else {
inputs.add(relBuilder.build());
}
}
call.transformTo(parent.copy(parent.getTraitSet(), inputs));
}
use of 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);
if (offset + RexLiteral.intValue(sortLimit.fetch) >= RelMetadataQuery.instance().getRowCount(reducedInput)) {
return false;
}
return true;
}
Aggregations