++ordinal; if (converted.equalsDeep(stripAs(selectItem), false)) { return new RelFieldCollation( ordinal, direction, nullDirection); ++ordinal; if (converted.equalsDeep(extraExpr, false)) { return new RelFieldCollation( ordinal, direction, nullDirection); return new RelFieldCollation(ordinal + 1, direction, nullDirection);
public Object toJson(RelCollationImpl node) { final List<Object> list = new ArrayList<Object>(); for (RelFieldCollation fieldCollation : node.getFieldCollations()) { final Map<String, Object> map = jsonBuilder.map(); map.put("field", fieldCollation.getFieldIndex()); map.put("direction", fieldCollation.getDirection().name()); map.put("nulls", fieldCollation.nullDirection.name()); list.add(map); } return list; }
/** * Applies a mapping to a field collation. * * <p>If the field is not mapped, returns null. * * @param mapping Mapping * @param fieldCollation Field collation * @return collation with mapping applied */ public static RelFieldCollation apply( Mappings.TargetMapping mapping, RelFieldCollation fieldCollation) { final int target = mapping.getTargetOpt(fieldCollation.getFieldIndex()); if (target < 0) { return null; } return fieldCollation.copy(target); }
/** Returns a string representation of this collation, suitably terse given * that it will appear in plan traces. Examples: * "[]", "[2]", "[0 DESC, 1]", "[0 DESC, 1 ASC NULLS LAST]". */ public String toString() { Iterator<RelFieldCollation> it = fieldCollations.iterator(); if (! it.hasNext()) { return "[]"; } StringBuilder sb = new StringBuilder(); sb.append('['); for (;;) { RelFieldCollation e = it.next(); sb.append(e.getFieldIndex()); if (e.direction != RelFieldCollation.Direction.ASCENDING || e.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { sb.append(' ').append(e.shortString()); } if (!it.hasNext()) { return sb.append(']').toString(); } sb.append(',').append(' '); } }
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 Integer apply(RelFieldCollation input) { return input.getFieldIndex(); } });
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; } }
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(); } }
/** * 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 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; } }
/** Creates a copy of this RelFieldCollation against a different field. */ public RelFieldCollation copy(int target) { if (target == fieldIndex) { return this; } return new RelFieldCollation(target, direction, nullDirection); }
/** Converts a collation to an ORDER BY item. */ public SqlNode toSql(RelFieldCollation collation) { SqlNode node = field(collation.getFieldIndex()); switch (collation.getDirection()) { case DESCENDING: case STRICTLY_DESCENDING: node = SqlStdOperatorTable.DESC.createCall(POS, node); } switch (collation.nullDirection) { case FIRST: node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node); break; case LAST: node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node); break; } return node; } }
/** * Applies a mapping to a field collation. * * <p>If the field is not mapped, returns null. * * @param mapping Mapping * @param fieldCollation Field collation * @return collation with mapping applied */ public static RelFieldCollation apply( Mappings.TargetMapping mapping, RelFieldCollation fieldCollation) { final int target = mapping.getTargetOpt(fieldCollation.getFieldIndex()); if (target < 0) { return null; } return fieldCollation.copy(target); }
/** * 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; }
/** * Creates a copy of this RelFieldCollation against a different field. */ public RelFieldCollation copy(int target) { if (target == fieldIndex) { return this; } return new RelFieldCollation(target, direction, nullDirection); }
/** Converts a collation to an ORDER BY item. */ public SqlNode toSql(RelFieldCollation collation) { SqlNode node = field(collation.getFieldIndex()); switch (collation.getDirection()) { case Descending: case StrictlyDescending: node = SqlStdOperatorTable.descendingOperator.createCall(POS, node); } switch (collation.nullDirection) { case FIRST: node = SqlStdOperatorTable.nullsFirstOperator.createCall(POS, node); break; case LAST: node = SqlStdOperatorTable.nullsLastOperator.createCall(POS, node); break; } return node; } }
: collation.getFieldCollations()) final int source = fieldCollation.getFieldIndex(); final int target = targets[source]; if (target < 0) { fieldCollation.copy(target));
ImmutableList.Builder<RexNode> builder = ImmutableList.builder(); for (RelFieldCollation field : collation.getFieldCollations()) { int index = field.getFieldIndex(); builder.add(cluster.getRexBuilder().makeInputRef(child, index));
/** * Creates a list containing one collation containing one field. */ public static List<RelCollation> createSingleton(int fieldIndex) { return ImmutableList.of( of( new RelFieldCollation(fieldIndex, RelFieldCollation.Direction.ASCENDING, RelFieldCollation.NullDirection.UNSPECIFIED))); }
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; } }