Refine search
public RelNode align(Sort rel, List<RelFieldCollation> collations) { final RelNode child = dispatchAlign(rel.getInput(), rel.collation.getFieldCollations()); return rel.copy(rel.getTraitSet(), ImmutableList.of(child)); }
final List<RelCollation> inputCollations = mq.collations(input); if (inputCollations == null || inputCollations.isEmpty()) { return ImmutableList.of(); final RexCall call = (RexCall) project.e; final RexCallBinding binding = RexCallBinding.create(input.getCluster().getTypeFactory(), call, inputCollations); targetsWithMonotonicity.put(project.i, call.getOperator().getMonotonicity(binding)); loop: for (RelCollation ic : inputCollations) { if (ic.getFieldCollations().isEmpty()) { continue; for (RelFieldCollation ifc : ic.getFieldCollations()) { final Collection<Integer> integers = targets.get(ifc.getFieldIndex()); if (integers.isEmpty()) { continue loop; // cannot do this collation fieldCollations.add(ifc.copy(integers.iterator().next())); default: fieldCollationsForRexCalls.add( new RelFieldCollation(entry.getKey(), RelFieldCollation.Direction.of(value))); break; return ImmutableList.copyOf(collations);
RelCollations.of(new RelFieldCollation(0), new RelFieldCollation(1)); collations = RelMdCollation.sort(collation); assertThat(collations.size(), equalTo(1)); assertThat(collations.get(0).getFieldCollations().size(), equalTo(2)); ImmutableList.of(rexBuilder.makeInputRef(empSort, 1), rexBuilder.makeLiteral("foo"), rexBuilder.makeInputRef(empSort, 0), collations = RelMdCollation.project(mq, empSort, projects); assertThat(collations.size(), equalTo(1)); assertThat(collations.get(0).getFieldCollations().size(), equalTo(2)); assertThat(collations.get(0).getFieldCollations().get(0).getFieldIndex(), equalTo(2)); assertThat(collations.get(0).getFieldCollations().get(1).getFieldIndex(), equalTo(0)); ImmutableList.of("a", "b", "c", "d")); ImmutableList.of()); assertThat(collations.toString(), equalTo("[[0, 1, 2, 3, 4, 5, 6, 7, 8], "
throw new IllegalArgumentException("Error planer:" + dumpPlan); RelDataType resultType = root.rel.getRowType(); boolean isDml = root.kind.belongsTo(SqlKind.DML); final Bindable bindable; parameterRowType, fieldOrigins, root.collation.getFieldCollations().isEmpty() ? ImmutableList.<RelCollation>of() : ImmutableList.of(root.collation), root.rel, mapTableModOp(isDml, root.kind),
final RelOptCluster cluster = project.getCluster(); if (sort.getConvention() != project.getConvention()) { return; project.getProjects(), project.getInput().getRowType()); for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { if (map.getTargetOpt(fc.getFieldIndex()) < 0) { return; final RexNode node = project.getProjects().get(fc.getFieldIndex()); if (node.isA(SqlKind.CAST)) { final RexCallBinding binding = RexCallBinding.create(cluster.getTypeFactory(), cast, ImmutableList.of(RelCollations.of(RexUtil.apply(map, fc)))); if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) { return; RexUtil.apply(map, sort.getCollation())); final Sort newSort = sort.copy( project.copy( sort.getTraitSet(), ImmutableList.of(newSort));
Expressions.parameter(int.class, "c"); final int mod = collation.getFieldCollations().size() == 1 ? Modifier.FINAL : 0; body.add(Expressions.declare(mod, parameterC, null)); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { final int index = fieldCollation.getFieldIndex(); Expression arg0 = fieldReference(parameterV0, index); Expression arg1 = fieldReference(parameterV1, index); == RelFieldCollation.NullDirection.FIRST; final boolean descending = fieldCollation.getDirection() == RelFieldCollation.Direction.DESCENDING; body.add( int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock())); overridingMethodDecl( BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock())); ImmutableList.of(), memberDeclarations);
final Sort sort = call.rel(1); final int count = sort.getInput().getRowType().getFieldCount(); if (count == 1) { final RexBuilder rexBuilder = sort.getCluster().getRexBuilder(); final RelMetadataQuery mq = call.getMetadataQuery(); final RelOptPredicateList predicates = mq.getPulledUpPredicates(sort.getInput()); if (predicates == null) { return; List<RelDataTypeField> fields = sort.getInput().getRowType().getFieldList(); List<Pair<RexNode, String>> newChildExprs = new ArrayList<>(); List<RexNode> topChildExprs = new ArrayList<>(); 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) { fieldCollations.add(fc.copy(target)); topChildExprs = ImmutableList.copyOf(RexUtil.apply(mapping, topChildExprs));
/** 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(); }
project.getProjects(), project.getInput().getRowType()).inverse(); 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)) { final RexCallBinding binding = RexCallBinding.create(cluster.getTypeFactory(), cast, ImmutableList.of(RexUtil.apply(map, sort.getCollation()))); if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) { return; for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) { fieldCollations.add(new RelFieldCollation(m.get(fc.getFieldIndex()), fc.direction, fc.nullDirection)); 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);
public static RelCollation shift(RelCollation collation, int offset) { if (offset == 0) { return collation; // save some effort } final ImmutableList.Builder<RelFieldCollation> fieldCollations = ImmutableList.builder(); for (RelFieldCollation fc : collation.getFieldCollations()) { fieldCollations.add(fc.shift(offset)); } return new RelCollationImpl(fieldCollations.build()); }
if (i < rels.size() && rels.get(i) instanceof Filter) { filterRel = (Filter) rels.get(i++); groupSet = aggregate.getGroupSet(); aggCalls = aggregate.getAggCallList(); aggNames = Util.skip(aggregate.getRowType().getFieldNames(), groupSet.cardinality()); collationIndexes = new ArrayList<>(); collationDirections = new ArrayList<>(); for (RelFieldCollation fCol : sort.collation.getFieldCollations()) { collationIndexes.add(fCol.getFieldIndex()); collationDirections.add(fCol.getDirection()); if (sort.getRowType().getFieldList().get(fCol.getFieldIndex()).getType().getFamily() == SqlTypeFamily.NUMERIC) { numericCollationBitSetBuilder.set(fCol.getFieldIndex());
final List<String> childFields = getInput().getRowType().getFieldNames(); checkState(groups.size() == 1, "Only one window is expected in WindowPrel"); Group window = groups.get(0); List<NamedExpression> withins = Lists.newArrayList(); List<NamedExpression> aggs = Lists.newArrayList(); for (RelFieldCollation fieldCollation : window.orderKeys.getFieldCollations()) { orderings.add(new Order.Ordering(fieldCollation.getDirection(), new FieldReference(childFields.get(fieldCollation.getFieldIndex())), fieldCollation.nullDirection));
if (root.rel instanceof LogicalProject) { rootPrj = (LogicalProject) root.rel; } else if (root.rel instanceof LogicalSort && root.rel.getInput(0) instanceof LogicalProject) { rootPrj = (LogicalProject) root.rel.getInput(0); rootSort = (LogicalSort) root.rel; } else { List<String> inFields = inType.getFieldNames(); List<RexNode> projExp = new ArrayList<>(); List<Pair<Integer, String>> projFields = new ArrayList<>(); for (int i = 0; i < root.validatedRowType.getFieldList().size(); i++) { if (root.validatedRowType.getFieldNames().get(i).startsWith("_KY_")) hiddenColumnExists = true; List<RelFieldCollation> fieldCollations = originalCollation.getFieldCollations(); ImmutableList.Builder<RelFieldCollation> newFieldCollations = ImmutableList.builder(); for (RelFieldCollation fieldCollation : fieldCollations) { if(projFieldMapping.containsKey(fieldCollation.getFieldIndex())) { newFieldCollations.add(fieldCollation.copy(projFieldMapping.get(fieldCollation.getFieldIndex()))); } else { newFieldCollations.add(fieldCollation);
final int fieldCount = rowType.getFieldCount(); final int inputFieldCount = input.getRowType().getFieldCount(); for(RelFieldCollation fieldCollation: group.collation().getFieldCollations()) { inputFinder.inputBitSet.set(fieldCollation.getFieldIndex()); final int newInputFieldCount = newInput.getRowType().getFieldCount(); final Mapping inputAndConstantsMapping = Mappings.create(MappingType.INVERSE_SURJECTION, inputFieldCount + window.constants.size(), newInputFieldCount + constantsUsed); for(int i = 0; i < window.constants.size(); i++) { int index = inputFieldCount + i; if (inputAndConstantsFieldsUsed.get(index)) { inputAndConstantsMapping.set(index, newInputFieldCount + newConstants.size()); newConstants.add(window.constants.get(i)); for(RelFieldCollation fieldCollation: group.collation().getFieldCollations()) { newFieldCollations.add(fieldCollation.copy(inputAndConstantsMapping.getTarget(fieldCollation.getFieldIndex())));
public RelWriter explainTerms(RelWriter pw) { super.explainTerms(pw); assert fieldExps.size() == collation.getFieldCollations().size(); if (pw.nest()) { pw.item("collation", collation); } else { for (Ord<RexNode> ord : Ord.zip(fieldExps)) { pw.item("sort" + ord.i, ord.e); } for (Ord<RelFieldCollation> ord : Ord.zip(collation.getFieldCollations())) { pw.item("dir" + ord.i, ord.e.shortString()); } } pw.itemIf("offset", offset, offset != null); pw.itemIf("fetch", fetch, fetch != null); return pw; } }
Operator<?> inputOp = inputOpAf.inputs.get(0); Operator<?> resultOp = inputOpAf.inputs.get(0); StringBuilder order = new StringBuilder(); StringBuilder nullOrder = new StringBuilder(); for (RelFieldCollation sortInfo : sortRel.getCollation().getFieldCollations()) { int sortColumnPos = sortInfo.getFieldIndex(); ColumnInfo columnInfo = new ColumnInfo(inputOp.getSchema().getSignature() .get(sortColumnPos)); columnInfo.getInternalName(), columnInfo.getTabAlias(), columnInfo.getIsVirtualCol()); sortCols.add(sortColumn); if (sortInfo.getDirection() == RelFieldCollation.Direction.DESCENDING) { order.append("-"); } else { } else { nullOrder.append(sortInfo.getDirection() == RelFieldCollation.Direction.DESCENDING ? "z" : "a");
if (sortLimit.getCollation() != RelCollations.EMPTY) { for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() >= join.getLeft().getRowType().getFieldCount()) { return false; if (sortLimit.getCollation() != RelCollations.EMPTY) { for (RelFieldCollation relFieldCollation : sortLimit.getCollation().getFieldCollations()) { if (relFieldCollation.getFieldIndex() < join.getLeft().getRowType().getFieldCount()) { return false;
public static List<Ordering> getOrdering(RelCollation collation, RelDataType rowType) { List<Ordering> orderExpr = Lists.newArrayList(); final List<String> childFields = rowType.getFieldNames(); for (RelFieldCollation fc: collation.getFieldCollations() ) { FieldReference fr = new FieldReference(childFields.get(fc.getFieldIndex()), false); orderExpr.add(new Ordering(fc.getDirection(), fr, fc.nullDirection)); } return orderExpr; }
@Override public void onMatch(RelOptRuleCall call) { final Sort sort = call.rel(0); if (sort.offset == null && sort.fetch == null) { return; } RelTraitSet origTraitSet = sort.getTraitSet(); RelTraitSet traitSet = origTraitSet.replace(OLAPRel.CONVENTION).simplify(); RelNode input = sort.getInput(); if (!sort.getCollation().getFieldCollations().isEmpty()) { // Create a sort with the same sort key, but no offset or fetch. input = sort.copy(sort.getTraitSet(), input, sort.getCollation(), null, null); } RelNode x = convert(input, input.getTraitSet().replace(OLAPRel.CONVENTION)); call.transformTo(new OLAPLimitRel(sort.getCluster(), traitSet, x, sort.offset, sort.fetch)); }
public void rewriteRel(Sort rel) { RelCollation oldCollation = rel.getCollation(); final RelNode oldChild = rel.getInput(); final RelNode newChild = getNewForOldRel(oldChild); final Mappings.TargetMapping mapping = getNewForOldInputMapping(oldChild); // validate for (RelFieldCollation field : oldCollation.getFieldCollations()) { int oldInput = field.getFieldIndex(); RelDataType sortFieldType = oldChild.getRowType().getFieldList().get(oldInput).getType(); if (sortFieldType.isStruct()) { // TODO jvs 10-Feb-2005 throw Util.needToImplement("sorting on structured types"); } } RelCollation newCollation = RexUtil.apply(mapping, oldCollation); Sort newRel = LogicalSort.create(newChild, newCollation, rel.offset, rel.fetch); setNewForOldRel(rel, newRel); }