final StringComparator comparator; if (collation.getDirection() == RelFieldCollation.Direction.ASCENDING) { direction = OrderByColumnSpec.Direction.ASCENDING; } else if (collation.getDirection() == RelFieldCollation.Direction.DESCENDING) { direction = OrderByColumnSpec.Direction.DESCENDING; } else { throw new ISE("WTF?! Don't know what to do with direction[%s]", collation.getDirection());
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");
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");
@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(); }
ASTNode directionAST = c.getDirection() == RelFieldCollation.Direction.ASCENDING ? ASTBuilder .createAST(HiveParser.TOK_TABSORTCOLNAMEASC, "TOK_TABSORTCOLNAMEASC") : ASTBuilder .createAST(HiveParser.TOK_TABSORTCOLNAMEDESC, "TOK_TABSORTCOLNAMEDESC"); } else { if (c.getDirection() == RelFieldCollation.Direction.ASCENDING) { nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_FIRST, "TOK_NULLS_FIRST"); directionAST.addChild(nullDirectionAST);
ASTNode directionAST = c.getDirection() == RelFieldCollation.Direction.ASCENDING ? ASTBuilder .createAST(HiveParser.TOK_TABSORTCOLNAMEASC, "TOK_TABSORTCOLNAMEASC") : ASTBuilder .createAST(HiveParser.TOK_TABSORTCOLNAMEDESC, "TOK_TABSORTCOLNAMEDESC"); } else { if (c.getDirection() == RelFieldCollation.Direction.ASCENDING) { nullDirectionAST = ASTBuilder.createAST(HiveParser.TOK_NULLS_FIRST, "TOK_NULLS_FIRST"); directionAST.addChild(nullDirectionAST);
public Object toJson(RelCollationImpl node) { final List<Object> list = new ArrayList<>(); 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; }
public Object toJson(RelCollationImpl node) { final List<Object> list = new ArrayList<>(); 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; }
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; }
private RelCollation convertRelCollation(RelCollation src, Map<Integer, Integer> inToOut) { List<RelFieldCollation> newFields = Lists.newArrayList(); // If we find a collation field that doesn't survive in the output, that field and all collations following that // field should be dropped (since sorting by A then B is different than just sorting by B) for ( RelFieldCollation field : src.getFieldCollations()) { if (!inToOut.containsKey(field.getFieldIndex())) { break; } newFields.add(new RelFieldCollation(inToOut.get(field.getFieldIndex()), field.getDirection(), field.nullDirection)); } if (newFields.isEmpty()) { return RelCollationImpl.of(); } else { return RelCollationImpl.of(newFields); } }
private RelCollation convertRelCollation(RelCollation src, Map<Integer, Integer> inToOut) { List<RelFieldCollation> newFields = Lists.newArrayList(); for ( RelFieldCollation field : src.getFieldCollations()) { if (inToOut.containsKey(field.getFieldIndex())) { newFields.add(new RelFieldCollation(inToOut.get(field.getFieldIndex()), field.getDirection(), field.nullDirection)); } } if (newFields.isEmpty()) { return RelCollations.of(); } else { return RelCollations.of(newFields); } }
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()), ExpressionPosition.UNKNOWN, false); orderExpr.add(new Ordering(fc.getDirection(), fr, fc.nullDirection)); } return orderExpr; }
private PlannerOp planSort(EnumerableSort op, RelDataType rowType) { PlannerOp input = convertRelNode(op.getInput(), rowType, false); RelCollation collation = op.getCollation(); List<RelFieldCollation> fieldCollations = collation.getFieldCollations(); boolean[] directions = new boolean[fieldCollations.size()]; int[] fields = new int[fieldCollations.size()]; int i = 0; for (RelFieldCollation col : fieldCollations) { RelFieldCollation.Direction direction = col.getDirection(); int index = col.getFieldIndex(); directions[i] = direction == RelFieldCollation.Direction.ASCENDING || direction == RelFieldCollation.Direction.STRICTLY_ASCENDING; fields[i++] = index; } return new SortOp(input, directions, fields); }
public void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(context.field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(context.toSql(field)); }
void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { final boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(toSql(field)); }
/** 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); } if (collation.nullDirection != dialect.defaultNullDirection(collation.direction)) { 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; }
@Override public LogicalOperator implement(LogicalPlanImplementor implementor) { final Order.Builder builder = Order.builder(); builder.setInput(implementor.visitChild(this, 0, getInput())); final List<String> childFields = getInput().getRowType().getFieldNames(); for(RelFieldCollation fieldCollation : this.collation.getFieldCollations()){ builder.addOrdering(fieldCollation.getDirection(), new FieldReference(childFields.get(fieldCollation.getFieldIndex())), fieldCollation.nullDirection); } return builder.build(); }
@Override public LogicalOperator implement(DrillImplementor implementor) { final Order.Builder builder = Order.builder(); builder.setInput(implementor.visitChild(this, 0, getInput())); final List<String> childFields = getInput().getRowType().getFieldNames(); for(RelFieldCollation fieldCollation : this.collation.getFieldCollations()){ builder.addOrdering(fieldCollation.getDirection(), new FieldReference(childFields.get(fieldCollation.getFieldIndex())), fieldCollation.nullDirection); } return builder.build(); }
@Override public void implement(Implementor implementor) { implementor.visitChild(0, getInput()); final List<RelDataTypeField> fields = getRowType().getFieldList(); for (RelFieldCollation fieldCollation : collation.getFieldCollations()) { final String name = fields.get(fieldCollation.getFieldIndex()).getName(); implementor.addSort(name, fieldCollation.getDirection()); } if (offset != null) { implementor.offset(((RexLiteral) offset).getValueAs(Long.class)); } if (fetch != null) { implementor.fetch(((RexLiteral) fetch).getValueAs(Long.class)); } }
@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(); }