/** Returns references to fields for a given collation. */ public ImmutableList<RexNode> fields(RelCollation collation) { final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder(); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { RexNode node = field(fieldCollation.getFieldIndex()); switch (fieldCollation.direction) { case DESCENDING: node = desc(node); } switch (fieldCollation.nullDirection) { case FIRST: node = nullsFirst(node); break; case LAST: node = nullsLast(node); break; } nodes.add(node); } return nodes.build(); }
public RelNode align(Aggregate rel, List<RelFieldCollation> collations) { // 1) We extract the group by positions that are part of the collations and // sort them so they respect it LinkedHashSet<Integer> aggregateColumnsOrder = new LinkedHashSet<>(); ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder(); if (rel.getGroupType() == Group.SIMPLE && !collations.isEmpty()) { for (RelFieldCollation c : collations) { if (c.getFieldIndex() < rel.getGroupCount()) { // Group column found if (aggregateColumnsOrder.add(c.getFieldIndex())) { propagateCollations.add(c.copy(rel.getGroupSet().nth(c.getFieldIndex()))); } } } } for (int i = 0; i < rel.getGroupCount(); i++) { if (!aggregateColumnsOrder.contains(i)) { // Not included in the input collations, but can be propagated as this Aggregate // will enforce it propagateCollations.add(new RelFieldCollation(rel.getGroupSet().nth(i))); } } // 2) We propagate final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build()); // 3) We annotate the Aggregate operator with this info final HiveAggregate newAggregate = (HiveAggregate) rel.copy(rel.getTraitSet(), ImmutableList.of(child)); newAggregate.setAggregateColumnsOrder(aggregateColumnsOrder); return newAggregate; }
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)); }
RexNode equals = idxToConjuncts.get(c.getFieldIndex()); if (equals != null) { conjuncts.add(equals); idxToConjuncts.remove(c.getFieldIndex()); idxToConjuncts.remove(refToRef.get(c.getFieldIndex())); if (c.getFieldIndex() < nLeftColumns) { propagateCollationsLeft.add(c.copy(c.getFieldIndex())); propagateCollationsRight.add(c.copy(refToRef.get(c.getFieldIndex()) - nLeftColumns)); } else { propagateCollationsLeft.add(c.copy(refToRef.get(c.getFieldIndex()))); propagateCollationsRight.add(c.copy(c.getFieldIndex() - nLeftColumns));
RexNode equals = idxToConjuncts.get(c.getFieldIndex()); if (equals != null) { conjuncts.add(equals); idxToConjuncts.remove(c.getFieldIndex()); idxToConjuncts.remove(refToRef.get(c.getFieldIndex())); if (c.getFieldIndex() < nLeftColumns) { propagateCollationsLeft.add(c.copy(c.getFieldIndex())); propagateCollationsRight.add(c.copy(refToRef.get(c.getFieldIndex()) - nLeftColumns)); } else { propagateCollationsLeft.add(c.copy(refToRef.get(c.getFieldIndex()))); propagateCollationsRight.add(c.copy(c.getFieldIndex() - nLeftColumns));
public RelNode align(Aggregate rel, List<RelFieldCollation> collations) { // 1) We extract the group by positions that are part of the collations and // sort them so they respect it LinkedHashSet<Integer> aggregateColumnsOrder = new LinkedHashSet<>(); ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder(); if (!rel.indicator && !collations.isEmpty()) { for (RelFieldCollation c : collations) { if (c.getFieldIndex() < rel.getGroupCount()) { // Group column found if (aggregateColumnsOrder.add(c.getFieldIndex())) { propagateCollations.add(c.copy(rel.getGroupSet().nth(c.getFieldIndex()))); } } } } for (int i = 0; i < rel.getGroupCount(); i++) { if (!aggregateColumnsOrder.contains(i)) { // Not included in the input collations, but can be propagated as this Aggregate // will enforce it propagateCollations.add(new RelFieldCollation(rel.getGroupSet().nth(i))); } } // 2) We propagate final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build()); // 3) We annotate the Aggregate operator with this info final HiveAggregate newAggregate = (HiveAggregate) rel.copy(rel.getTraitSet(), ImmutableList.of(child)); newAggregate.setAggregateColumnsOrder(aggregateColumnsOrder); return newAggregate; }
Set<Integer> needed = new HashSet<>(); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { needed.add(fc.getFieldIndex()); final RexNode node = project.getProjects().get(map.getTarget(fc.getFieldIndex())); if (node.isA(SqlKind.CAST)) { fieldCollations.add(new RelFieldCollation(m.get(fc.getFieldIndex()), fc.direction, fc.nullDirection));
StringBuilder nullOrder = new StringBuilder(); for (RelFieldCollation sortInfo : sortRel.getCollation().getFieldCollations()) { int sortColumnPos = sortInfo.getFieldIndex(); ColumnInfo columnInfo = new ColumnInfo(inputOp.getSchema().getSignature() .get(sortColumnPos));
List<RelFieldCollation> fieldCollations = new ArrayList<>(); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { final int target = mapping.getTargetOpt(fc.getFieldIndex()); if (target < 0) {
StringBuilder nullOrder = new StringBuilder(); for (RelFieldCollation sortInfo : sortRel.getCollation().getFieldCollations()) { int sortColumnPos = sortInfo.getFieldIndex(); ColumnInfo columnInfo = new ColumnInfo(inputOp.getSchema().getSignature() .get(sortColumnPos));
@Override public void implementRewrite(RewriteImplementor implementor) { implementor.visitChild(this, getInput()); // No need to rewrite "order by" applied on non-olap context. // Occurs in sub-query like "select ... from (...) inner join (...) order by ..." if (this.context.realization == null) return; for (RelFieldCollation fieldCollation : this.collation.getFieldCollations()) { int index = fieldCollation.getFieldIndex(); SQLDigest.OrderEnum order = getOrderEnum(fieldCollation.getDirection()); OLAPRel olapChild = (OLAPRel) this.getInput(); TblColRef orderCol = olapChild.getColumnRowType().getAllColumns().get(index); this.context.addSort(orderCol, order); this.context.storageContext.markSort(); } this.rowType = this.deriveRowType(); this.columnRowType = buildColumnRowType(); }
List<RelFieldCollation> fieldCollations = new ArrayList<>(); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { final int target = mapping.getTargetOpt(fc.getFieldIndex()); if (target < 0) {
/** Returns references to fields for a given collation. */ public ImmutableList<RexNode> fields(RelCollation collation) { final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder(); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { RexNode node = field(fieldCollation.getFieldIndex()); switch (fieldCollation.direction) { case DESCENDING: node = desc(node); } switch (fieldCollation.nullDirection) { case FIRST: node = nullsFirst(node); break; case LAST: node = nullsLast(node); break; } nodes.add(node); } return nodes.build(); }
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)); }
obExpr = obRefToCallMap.get(c.getFieldIndex()); ColumnInfo cI = schema.get(c.getFieldIndex());
obExpr = obRefToCallMap.get(c.getFieldIndex()); ColumnInfo cI = schema.get(c.getFieldIndex());
for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) { return false; for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) { return false;
for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) { return false; for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) { return false;
public void onMatch(RelOptRuleCall call) { final HiveSortLimit sort = call.rel(0); final HiveProject project = call.rel(1); // Determine mapping between project input and output fields. If sort // relies on non-trivial expressions, we can't push. final Mappings.TargetMapping map = RelOptUtil.permutation( project.getProjects(), project.getInput().getRowType()); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { if (map.getTargetOpt(fc.getFieldIndex()) < 0) { return; } } // Create new collation final RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize( RexUtil.apply(map, sort.getCollation())); // New operators final HiveSortLimit newSort = sort.copy(sort.getTraitSet().replace(newCollation), project.getInput(), newCollation, sort.offset, sort.fetch); final RelNode newProject = project.copy(sort.getTraitSet(), ImmutableList.<RelNode>of(newSort)); call.transformTo(newProject); }
public void onMatch(RelOptRuleCall call) { final HiveProject project = call.rel(0); final HiveSortLimit sort = call.rel(1); // Determine mapping between project input and output fields. If sort // relies on non-trivial expressions, we can't push. final Mappings.TargetMapping map = RelOptUtil.permutation( project.getProjects(), project.getInput().getRowType()).inverse(); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { if (map.getTarget(fc.getFieldIndex()) < 0) { return; } } // Create new collation final RelCollation newCollation = RelCollationTraitDef.INSTANCE.canonize( RexUtil.apply(map, sort.getCollation())); // New operators final RelNode newProject = project.copy(sort.getInput().getTraitSet(), ImmutableList.<RelNode>of(sort.getInput())); final HiveSortLimit newSort = sort.copy(newProject.getTraitSet(), newProject, newCollation, sort.offset, sort.fetch); call.transformTo(newSort); }