SqlNode fetch) { if (select.getOrderList() == null) { assert collation.getFieldCollations().isEmpty(); if (offset == null && fetch == null) { return;
/** Returns the indexes of the field collations in a given collation. */ public static List<Integer> ordinals(RelCollation collation) { return Lists.transform(collation.getFieldCollations(), new Function<RelFieldCollation, Integer>() { public Integer apply(RelFieldCollation input) { return input.getFieldIndex(); } }); } }
/** * Applies a mapping to a collation. * * @param mapping Mapping * @param collation Collation * @return collation with mapping applied */ public static RelCollation apply( Mappings.TargetMapping mapping, RelCollation collation) { List<RelFieldCollation> fieldCollations = applyFields(mapping, collation.getFieldCollations()); return fieldCollations.equals(collation.getFieldCollations()) ? collation : RelCollationImpl.of(fieldCollations); }
/** * Applies a mapping to a collation. * * @param mapping Mapping * @param collation Collation * @return collation with mapping applied */ public static RelCollation apply( Mappings.TargetMapping mapping, RelCollation collation) { List<RelFieldCollation> fieldCollations = applyFields(mapping, collation.getFieldCollations()); return fieldCollations.equals(collation.getFieldCollations()) ? collation : RelCollationImpl.of(fieldCollations); }
/** * Checks that a collection of collations is valid. * * @param rowType Row type of the relational expression * @param collationList List of collations * @param fail Whether to fail if invalid * @return Whether valid */ public static boolean isValid( RelDataType rowType, List<RelCollation> collationList, boolean fail) { final int fieldCount = rowType.getFieldCount(); for (RelCollation collation : collationList) { for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { final int index = fieldCollation.getFieldIndex(); if (index < 0 || index >= fieldCount) { assert !fail; return false; } } } return true; }
/** * Checks that a collection of collations is valid. * * @param rowType Row type of the relational expression * @param collationList List of collations * @param fail Whether to fail if invalid * * @return Whether valid */ public static boolean isValid( RelDataType rowType, List<RelCollation> collationList, boolean fail) { final int fieldCount = rowType.getFieldCount(); for (RelCollation collation : collationList) { for ( RelFieldCollation fieldCollation : collation.getFieldCollations()) { final int index = fieldCollation.getFieldIndex(); if ((index < 0) || (index >= fieldCount)) { assert !fail; return false; } } } return true; }
public RelOptPlanWriter explainTerms(RelOptPlanWriter pw) { super.explainTerms(pw); assert fieldExps.size() == collation.getFieldCollations().size(); 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; } }
private Pair<Expression, Expression> getRowCollationKey( BlockBuilder builder, PhysType inputPhysType, Window window, int windowIdx) { if (!(window.isRows || (window.upperBound.isUnbounded() && window.lowerBound.isUnbounded()))) { Pair<Expression, Expression> pair = inputPhysType.generateCollationKey( window.collation().getFieldCollations()); // optimize=false to prevent inlining of object create into for-loops return Pair.of( builder.append("keySelector" + windowIdx, pair.left, false), builder.append("keyComparator" + windowIdx, pair.right, false)); } else { return Pair.of(null, null); } }
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; } }
: "traits=" + traits + ", collation=" + collation; ImmutableList.Builder<RexNode> builder = ImmutableList.builder(); for (RelFieldCollation field : collation.getFieldCollations()) { int index = field.getFieldIndex(); builder.add(
public RelNode convert( RelOptPlanner planner, RelNode rel, RelCollation toCollation, boolean allowInfiniteCostConverters) { if (toCollation == RelCollationImpl.PRESERVE) { return null; } if (toCollation.getFieldCollations().isEmpty()) { // An empty sort doesn't make sense. return null; } // Create a logical sort, then ask the planner to convert its remaining // traits (e.g. convert it to an EnumerableSortRel if rel is enumerable // convention) final SortRel sort = new SortRel( rel.getCluster(), rel.getCluster().traitSetOf(Convention.NONE, toCollation), rel, toCollation); RelNode newRel = sort; final RelTraitSet newTraitSet = rel.getTraitSet().replace(toCollation); if (!newRel.getTraitSet().equals(newTraitSet)) { newRel = planner.changeTraits(sort, newTraitSet); } return newRel; }
private Fields createFields() { Fields fields = new Fields(); RelNode child = getChild(); RelDataType inputRowType = child.getRowType(); for( RexNode exp : fieldExps ) { int index = ( (RexInputRef) exp ).getIndex(); RelDataTypeField typeField = inputRowType.getFieldList().get( index ); String name = typeField.getName(); fields = fields.append( new Fields( name ) ); } for( RelFieldCollation fieldCollation : collation.getFieldCollations() ) { String name = inputRowType.getFieldList().get( fieldCollation.getFieldIndex() ).getName(); boolean isDescending = fieldCollation.getDirection() == RelFieldCollation.Direction.Descending; boolean isNullsFirst = fieldCollation.nullDirection == RelFieldCollation.NullDirection.FIRST; Comparator<Comparable> comparator = Functions.<Comparable>nullsComparator( isNullsFirst, isDescending ); if( comparator != null ) fields.setComparator( name, comparator ); } return fields; } }
public JdbcImplementor.Result implement(JdbcImplementor implementor) { final JdbcImplementor.Result x = implementor.visitChild(0, getChild()); final JdbcImplementor.Builder builder = x.builder(this, JdbcImplementor.Clause.ORDER_BY); List<SqlNode> orderByList = Expressions.list(); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { if (fieldCollation.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED && implementor.dialect.getDatabaseProduct() == SqlDialect.DatabaseProduct.MYSQL) { orderByList.add( ISNULL_FUNCTION.createCall(POS, builder.context.field(fieldCollation.getFieldIndex()))); fieldCollation = new RelFieldCollation(fieldCollation.getFieldIndex(), fieldCollation.getDirection()); } orderByList.add(builder.context.toSql(fieldCollation)); } builder.setOrderBy(new SqlNodeList(orderByList, POS)); return builder.result(); } }
public JdbcImplementor.Result implement(JdbcImplementor implementor) { final JdbcImplementor.Result x = implementor.visitChild(0, getChild()); final JdbcImplementor.Builder builder = x.builder(this, JdbcImplementor.Clause.ORDER_BY); List<SqlNode> orderByList = Expressions.list(); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { if (fieldCollation.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED && implementor.dialect.getDatabaseProduct() == SqlDialect.DatabaseProduct.MYSQL) { orderByList.add( ISNULL_FUNCTION.createCall(POS, builder.context.field(fieldCollation.getFieldIndex()))); fieldCollation = new RelFieldCollation(fieldCollation.getFieldIndex(), fieldCollation.getDirection()); } orderByList.add(builder.context.toSql(fieldCollation)); } builder.setOrderBy(new SqlNodeList(orderByList, POS)); return builder.result(); } }
@Override public void onMatch(RelOptRuleCall call) { final SortRel sort = call.rel(0); if (sort.offset == null && sort.fetch == null) { return; } final RelTraitSet traitSet = sort.getTraitSet().replace(EnumerableConvention.INSTANCE); RelNode input = sort.getChild(); if (!sort.getCollation().getFieldCollations().isEmpty()) { input = sort.copy( sort.getTraitSet().replace(RelCollationImpl.EMPTY), input, RelCollationImpl.EMPTY, null, null); } RelNode x = convert( input, input.getTraitSet().replace(EnumerableConvention.INSTANCE)); call.transformTo( new EnumerableLimitRel( sort.getCluster(), traitSet, x, sort.offset, sort.fetch)); } }
@Override public void onMatch(RelOptRuleCall call) { final SortRel sort = call.rel(0); if (sort.offset == null && sort.fetch == null) { return; } final RelTraitSet traitSet = sort.getTraitSet().replace(EnumerableConvention.INSTANCE); RelNode input = sort.getChild(); 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(EnumerableConvention.INSTANCE)); call.transformTo( new EnumerableLimitRel( sort.getCluster(), traitSet, x, sort.offset, sort.fetch)); } }
public void rewriteRel(SortRel rel) { RelCollation oldCollation = rel.getCollation(); final RelNode oldChild = rel.getChild(); 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"); } } SortRel newRel = new SortRel( rel.getCluster(), rel.getCluster().traitSetOf(Convention.NONE), newChild, RexUtil.apply(mapping, oldCollation)); setNewForOldRel(rel, newRel); }
public void rewriteRel(SortRel rel) { RelCollation oldCollation = rel.getCollation(); final RelNode oldChild = rel.getChild(); 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); SortRel newRel = new SortRel( rel.getCluster(), rel.getCluster().traitSetOf(Convention.NONE).plus(newCollation), newChild, newCollation, rel.offset, rel.fetch); setNewForOldRel(rel, newRel); }
public Result implement(EnumerableRelImplementor implementor, Prefer pref) { final BlockBuilder builder = new BlockBuilder(); final EnumerableRel child = (EnumerableRel) getChild(); final Result result = implementor.visitChild(this, 0, child, pref); final PhysType physType = PhysTypeImpl.of( implementor.getTypeFactory(), getRowType(), result.format); Expression childExp = builder.append( "child", result.block); PhysType inputPhysType = result.physType; final Pair<Expression, Expression> pair = inputPhysType.generateCollationKey( collation.getFieldCollations()); builder.add( Expressions.return_( null, Expressions.call( childExp, BuiltinMethod.ORDER_BY.method, Expressions.list( builder.append("keySelector", pair.left)) .appendIfNotNull(builder.appendIfNotNull("comparator", pair.right))))); return implementor.result(physType, builder.toBlock()); } }
public Result implement(EnumerableRelImplementor implementor, Prefer pref) { final BlockBuilder builder = new BlockBuilder(); final EnumerableRel child = (EnumerableRel) getChild(); final Result result = implementor.visitChild(this, 0, child, pref); final PhysType physType = PhysTypeImpl.of( implementor.getTypeFactory(), getRowType(), pref.prefer(result.format)); Expression childExp = builder.append( "child", result.block); PhysType inputPhysType = result.physType; final Pair<Expression, Expression> pair = inputPhysType.generateCollationKey( collation.getFieldCollations()); builder.add( Expressions.return_( null, Expressions.call( childExp, BuiltinMethod.ORDER_BY.method, Expressions.list( builder.append("keySelector", pair.left)) .appendIfNotNull(builder.appendIfNotNull("comparator", pair.right))))); return implementor.result(physType, builder.toBlock()); } }