public static SortItem ascending(String name) { return new SortItem(identifier(name), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.UNDEFINED); }
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 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); }
@Override protected R visitSortItem(SortItem node, C context) { return process(node.getSortKey(), context); }
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; } } } }
public static SortItem ascending(String name) { return new SortItem(identifier(name), SortItem.Ordering.ASCENDING, SortItem.NullOrdering.UNDEFINED); }
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());
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"); } } }
@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)); }
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"); } } }
@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)); }
Expression expression = item.getSortKey();
aliased(new Values(rows.build()), "functions", ImmutableList.copyOf(columns.keySet())), ordering( new SortItem( functionCall("lower", identifier("function_name")), SortItem.Ordering.ASCENDING,
Expression expression = item.getSortKey();
aliased(new Values(rows.build()), "functions", ImmutableList.copyOf(columns.keySet())), ordering( new SortItem( functionCall("lower", identifier("function_name")), SortItem.Ordering.ASCENDING,
@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; }
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); }
@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; }
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); }