Support ORDER BY ALL

This commit is contained in:
zhongyuankai 2023-12-13 15:42:43 +08:00 committed by ubuntu
parent 2d58dc512c
commit 01f5b20517
8 changed files with 102 additions and 15 deletions

View File

@ -241,6 +241,22 @@ Result:
└───┴─────────┘
```
## ORDER BY ALL
`ORDER BY ALL` is sorts all selected columns in ascending order.
For example:
``` sql
SELECT a, b, c FROM t ORDER BY ALL
```
is the same as
``` sql
SELECT a, b, c FROM t ORDER BY a, b, c
```
## Implementation Details
Less RAM is used if a small enough [LIMIT](../../../sql-reference/statements/select/limit.md) is specified in addition to `ORDER BY`. Otherwise, the amount of memory spent is proportional to the volume of data for sorting. For distributed query processing, if [GROUP BY](../../../sql-reference/statements/select/group-by.md) is omitted, sorting is partially done on remote servers, and the results are merged on the requestor server. This means that for distributed sorting, the volume of data to sort can be greater than the amount of memory on a single server.

View File

@ -61,6 +61,22 @@ sidebar_label: ORDER BY
我们只建议使用 `COLLATE` 对于少量行的最终排序,因为排序与 `COLLATE` 比正常的按字节排序效率低。
## ORDER BY ALL
`ORDER BY ALL` 对所有选定的列进行升序排序。
示例:
``` sql
SELECT a, b, c FROM t ORDER BY ALL
```
等同于:
``` sql
SELECT a, b, c FROM t ORDER BY a, b, c
```
## 实现细节 {#implementation-details}
更少的RAM使用如果一个足够小 [LIMIT](../../../sql-reference/statements/select/limit.md) 除了指定 `ORDER BY`. 否则,所花费的内存量与用于排序的数据量成正比。 对于分布式查询处理,如果 [GROUP BY](../../../sql-reference/statements/select/group-by.md) 省略排序,在远程服务器上部分完成排序,并将结果合并到请求者服务器上。 这意味着对于分布式排序,要排序的数据量可以大于单个服务器上的内存量。

View File

@ -776,6 +776,22 @@ void expandGroupByAll(ASTSelectQuery * select_query)
select_query->setExpression(ASTSelectQuery::Expression::GROUP_BY, group_expression_list);
}
void expandOrderByAll(ASTSelectQuery * select_query)
{
auto order_expression_list = std::make_shared<ASTExpressionList>();
for (const auto & expr : select_query->select()->children)
{
auto elem = std::make_shared<ASTOrderByElement>();
elem->direction = 1;
elem->nulls_direction = 1;
elem->children.push_back(expr);
order_expression_list->children.push_back(elem);
}
select_query->setExpression(ASTSelectQuery::Expression::ORDER_BY, order_expression_list);
}
ASTs getAggregates(ASTPtr & query, const ASTSelectQuery & select_query)
{
/// There can not be aggregate functions inside the WHERE and PREWHERE.
@ -1292,6 +1308,10 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect(
if (select_query->group_by_all)
expandGroupByAll(select_query);
// expand ORDER BY ALL
if (select_query->order_by_all)
expandOrderByAll(select_query);
/// Remove unneeded columns according to 'required_result_columns'.
/// Leave all selected columns in case of DISTINCT; columns that contain arrayJoin function inside.
/// Must be after 'normalizeTree' (after expanding aliases, for aliases not get lost)

View File

@ -144,7 +144,10 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
window()->as<ASTExpressionList &>().formatImplMultiline(s, state, frame);
}
if (orderBy())
if (order_by_all)
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY ALL" << (s.hilite ? hilite_none : "");
if (!order_by_all && orderBy())
{
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "ORDER BY" << (s.hilite ? hilite_none : "");
s.one_line

View File

@ -87,6 +87,7 @@ public:
bool group_by_with_cube = false;
bool group_by_with_constant_keys = false;
bool group_by_with_grouping_sets = false;
bool order_by_all = false;
bool limit_with_ties = false;
ASTPtr & refSelect() { return getExpression(Expression::SELECT); }

View File

@ -267,6 +267,12 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
/// ORDER BY expr ASC|DESC COLLATE 'locale' list
if (s_order_by.ignore(pos, expected))
{
if (s_all.ignore(pos, expected))
{
select_query->order_by_all = true;
}
else
{
if (!order_list.parse(pos, order_expression_list, expected))
return false;
@ -288,6 +294,7 @@ bool ParserSelectQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
}
}
}
}
/// This is needed for TOP expression, because it can also use WITH TIES.
bool limit_with_ties_occured = false;

View File

@ -0,0 +1,9 @@
abc1 3 2
abc2 3 2
abc3 2 3
abc4 1 4
abc1 1 1
abc2 1 1
abc3 1 1
abc4 1 1
abc 4

View File

@ -0,0 +1,15 @@
DROP TABLE IF EXISTS order_by_all;
CREATE TABLE order_by_all
(
a String,
b int,
c int
)
engine = Memory;
insert into order_by_all values ('abc2', 3, 2), ('abc3', 2, 3), ('abc4', 1, 4), ('abc1', 3, 2);
select a, b, c from order_by_all order by all;
select a, count(b), count(c) from order_by_all group by all order by all;
select substring(a, 1, 3), count(b) from order_by_all group by all order by all;