StringBuilder builder = new StringBuilder(); builder.append(formatExpression(input.getSortKey(), parameters)); switch (input.getOrdering()) { case ASCENDING: builder.append(" ASC"); break; default: throw new UnsupportedOperationException("unknown ordering: " + input.getOrdering()); switch (input.getNullOrdering()) { case FIRST: builder.append(" NULLS FIRST"); throw new UnsupportedOperationException("unknown null ordering: " + input.getNullOrdering());
public static SortItem ascending(String name) { return new SortItem(identifier(name), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.UNDEFINED); }
public static SortOrder toSortOrder(SortItem sortItem) { if (sortItem.getOrdering() == Ordering.ASCENDING) { if (sortItem.getNullOrdering() == NullOrdering.FIRST) { return SortOrder.ASC_NULLS_FIRST; } else { return SortOrder.ASC_NULLS_LAST; } } else { if (sortItem.getNullOrdering() == NullOrdering.FIRST) { return SortOrder.DESC_NULLS_FIRST; } else { return SortOrder.DESC_NULLS_LAST; } } } }
@Override protected R visitSortItem(SortItem node, C context) { return process(node.getSortKey(), context); }
if (orderByOutside != null) { orderBy = orderByOutside.stream() .map(e -> new Ordering(e.getOrdering(), mapper.apply(e.getSortKey()), e.getSortKey().toString())) .collect(Collectors.toList()); } else { orderBy = queryBody.getOrderBy().map(v -> v.getSortItems().stream().map(item -> new Ordering(item.getOrdering(), mapper.apply(item.getSortKey()), item.getSortKey().toString())) .collect(Collectors.toList())).orElse(ImmutableList.of());
OrderBy ob = si.accept(orderOarser, state); if(state.hasException()) return new ParseResult(state.getException()); orderings.add(ob);
private void verifySelectDistinct(QuerySpecification node, List<Expression> outputExpressions) { for (SortItem item : node.getOrderBy().get().getSortItems()) { Expression expression = item.getSortKey(); if (expression instanceof LongLiteral) { continue; } Expression rewrittenOrderByExpression = ExpressionTreeRewriter.rewriteWith(new OrderByExpressionRewriter(extractNamedOutputExpressions(node.getSelect())), expression); int index = outputExpressions.indexOf(rewrittenOrderByExpression); if (index == -1) { throw new SemanticException(ORDER_BY_MUST_BE_IN_SELECT, node.getSelect(), "For SELECT DISTINCT, ORDER BY expressions must appear in select list"); } if (!isDeterministic(expression)) { throw new SemanticException(NONDETERMINISTIC_ORDER_BY_EXPRESSION_WITH_SELECT_DISTINCT, expression, "Non deterministic ORDER BY expression is not supported with SELECT DISTINCT"); } } }
node.getOrderBy().ifPresent((orderBy -> { for (SortItem sortItem : orderBy.getSortItems()) { String expression = RakamSqlFormatter.formatExpression(sortItem.getSortKey(), qualifiedNameStringFunction, columnMappingFunction, ' '); reportRequest.orderBys.add(new GARequest.ReportRequest.OrderBy(expression, sortItem.getOrdering()));
private SortOrder toSortOrder(SortItem sortItem) { if (sortItem.getOrdering() == Ordering.ASCENDING) { if (sortItem.getNullOrdering() == NullOrdering.FIRST) { return SortOrder.ASC_NULLS_FIRST; } else { return SortOrder.ASC_NULLS_LAST; } } else { if (sortItem.getNullOrdering() == NullOrdering.FIRST) { return SortOrder.DESC_NULLS_FIRST; } else { return SortOrder.DESC_NULLS_LAST; } } } }
private static Function<SortItem, String> sortItemFormatterFunction(Function<QualifiedName, String> tableNameMapper, Optional<Function<String, String>> columnNameMapper, List<String> queryWithTables, char escapeIdentifier) { return input -> { StringBuilder builder = new StringBuilder(); builder.append(formatExpression(input.getSortKey(), tableNameMapper, columnNameMapper, queryWithTables, escapeIdentifier)); switch (input.getOrdering()) { case ASCENDING: builder.append(" ASC"); break; case DESCENDING: builder.append(" DESC"); break; default: throw new UnsupportedOperationException("unknown ordering: " + input.getOrdering()); } switch (input.getNullOrdering()) { case FIRST: builder.append(" NULLS FIRST"); break; case LAST: builder.append(" NULLS LAST"); break; case UNDEFINED: // no op break; default: throw new UnsupportedOperationException("unknown null ordering: " + input.getNullOrdering()); } return builder.toString(); }; }
Expression expression = item.getSortKey();
@Override protected OrderBy visitSortItem(SortItem si, QueryState state){ String orderKey = null; if(si.getSortKey() instanceof DereferenceExpression){ orderKey = SelectParser.visitDereferenceExpression((DereferenceExpression)si.getSortKey()); }else if (si.getSortKey() instanceof FunctionCall){ orderKey = si.getSortKey().toString().replaceAll("\"",""); }else if(si.getSortKey() instanceof SearchedCaseExpression){ SearchedCaseExpression sce = (SearchedCaseExpression)si.getSortKey(); for(WhenClause when : sce.getWhenClauses()){ orderKey = SelectParser.visitDereferenceExpression( (DereferenceExpression)((IsNullPredicate)when.getOperand()).getValue()); }else if(si.getSortKey() instanceof Identifier){ orderKey = ((Identifier)si.getSortKey()).getName(); //.getValue(); }else { state.addException("Order statement with type '"+si.getSortKey().getClass().getName()+"' is not supported"); return null; if(si.getOrdering().toString().startsWith("ASC")){ return new OrderBy(column.getColumn(), SortOrder.ASC, column.getIndex()); }else{
@Override public Node visitSortItem(SqlBaseParser.SortItemContext context) { return new SortItem( getLocation(context), (Expression) visit(context.expression()), Optional.ofNullable(context.ordering) .map(AstBuilder::getOrderingType) .orElse(SortItem.Ordering.ASCENDING), Optional.ofNullable(context.nullOrdering) .map(AstBuilder::getNullOrderingType) .orElse(SortItem.NullOrdering.UNDEFINED)); }
StringBuilder builder = new StringBuilder(); builder.append(formatExpression(input.getSortKey(), unmangleNames)); switch (input.getOrdering()) { case ASCENDING: builder.append(" ASC"); break; default: throw new UnsupportedOperationException("unknown ordering: " + input.getOrdering()); switch (input.getNullOrdering()) { case FIRST: builder.append(" NULLS FIRST"); throw new UnsupportedOperationException("unknown null ordering: " + input.getNullOrdering());
@Override public Boolean visitWindow(Window node, Void context) { for (Expression expression : node.getPartitionBy()) { if (!process(expression, context)) { throw new SemanticException(MUST_BE_AGGREGATE_OR_GROUP_BY, expression, "PARTITION BY expression '%s' must be an aggregate expression or appear in GROUP BY clause", expression); } } for (SortItem sortItem : getSortItemsFromOrderBy(node.getOrderBy())) { Expression expression = sortItem.getSortKey(); if (!process(expression, context)) { throw new SemanticException(MUST_BE_AGGREGATE_OR_GROUP_BY, expression, "ORDER BY expression '%s' must be an aggregate expression or appear in GROUP BY clause", expression); } } if (node.getFrame().isPresent()) { process(node.getFrame().get(), context); } return true; }
aliased(new Values(rows.build()), "functions", ImmutableList.copyOf(columns.keySet())), ordering( new SortItem( functionCall("lower", identifier("function_name")), SortItem.Ordering.ASCENDING,
StringBuilder builder = new StringBuilder(); builder.append(formatExpression(input.getSortKey(), parameters)); switch (input.getOrdering()) { case ASCENDING: builder.append(" ASC"); break; default: throw new UnsupportedOperationException("unknown ordering: " + input.getOrdering()); switch (input.getNullOrdering()) { case FIRST: builder.append(" NULLS FIRST"); throw new UnsupportedOperationException("unknown null ordering: " + input.getNullOrdering());
Symbol symbol = subPlan.translate(item.getSortKey());
public FunctionCall getExpectedValue(SymbolAliases aliases) { List<Expression> symbolReferences = toSymbolReferences(args, aliases); if (isWindowFunction) { return new ExpectedWindowFunctionCall(symbolReferences); } Optional<OrderBy> orderByClause = Optional.empty(); if (!orderBy.isEmpty()) { orderByClause = Optional.of(new OrderBy(orderBy.stream() .map(item -> new SortItem( Symbol.from(aliases.get(item.getField())).toSymbolReference(), item.getOrdering(), item.getNullOrdering())) .collect(Collectors.toList()))); } return new FunctionCall(name, Optional.empty(), Optional.empty(), orderByClause, distinct, symbolReferences); }
StringBuilder builder = new StringBuilder(); builder.append(formatExpression(input.getSortKey(), unmangleNames)); switch (input.getOrdering()) { case ASCENDING: builder.append(" ASC"); break; default: throw new UnsupportedOperationException("unknown ordering: " + input.getOrdering()); switch (input.getNullOrdering()) { case FIRST: builder.append(" NULLS FIRST"); throw new UnsupportedOperationException("unknown null ordering: " + input.getNullOrdering());