|
| 1 | +#include <format> |
1 | 2 | #include <Parsers/ASTLiteral.h> |
2 | | -#include <Parsers/IParserBase.h> |
3 | | -#include <Parsers/ExpressionListParsers.h> |
4 | 3 | #include <Parsers/ASTOrderByElement.h> |
| 4 | +#include <Parsers/ExpressionListParsers.h> |
| 5 | +#include <Parsers/IParserBase.h> |
5 | 6 | #include <Parsers/Kusto/ParserKQLQuery.h> |
6 | 7 | #include <Parsers/Kusto/ParserKQLSort.h> |
7 | 8 |
|
8 | 9 | namespace DB |
9 | 10 | { |
10 | 11 |
|
| 12 | +namespace ErrorCodes |
| 13 | +{ |
| 14 | + extern const int SYNTAX_ERROR; |
| 15 | +} |
| 16 | + |
11 | 17 | bool ParserKQLSort::parseImpl(Pos & pos, ASTPtr & node, Expected & expected) |
12 | 18 | { |
13 | | - bool has_dir = false; |
14 | | - std::vector <bool> has_directions; |
| 19 | + String order_list_str; |
15 | 20 | ParserOrderByExpressionList order_list; |
16 | 21 | ASTPtr order_expression_list; |
17 | 22 |
|
18 | | - auto expr = getExprFromToken(pos); |
19 | | - |
20 | | - Tokens tokens(expr.c_str(), expr.c_str() + expr.size()); |
21 | | - IParser::Pos new_pos(tokens, pos.max_depth); |
| 23 | + auto validate_column = [&](Pos & pos1, Pos & pos2) |
| 24 | + { |
| 25 | + if (pos2->type == TokenType::BareWord && pos1 != pos2) |
| 26 | + throw Exception( |
| 27 | + ErrorCodes::SYNTAX_ERROR, |
| 28 | + "{} does not refer to any known column, table, variable or function", |
| 29 | + String(pos2->begin, pos2->end)); |
22 | 30 |
|
23 | | - auto pos_backup = new_pos; |
24 | | - if (!order_list.parse(pos_backup, order_expression_list, expected)) |
25 | | - return false; |
| 31 | + return String(pos1->begin, pos2->end); |
| 32 | + }; |
26 | 33 |
|
27 | | - while (!new_pos->isEnd() && new_pos->type != TokenType::PipeMark && new_pos->type != TokenType::Semicolon) |
| 34 | + auto format_sort_expr = [&](const Pos & pos1, const Pos & pos2) |
28 | 35 | { |
29 | | - String tmp(new_pos->begin, new_pos->end); |
30 | | - if (tmp == "desc" or tmp == "asc") |
31 | | - has_dir = true; |
32 | | - |
33 | | - if (new_pos->type == TokenType::Comma) |
| 36 | + auto start_pos = pos1; |
| 37 | + auto end_pos = pos2; |
| 38 | + String column_expr, sort_dir, nulls_position; |
| 39 | + auto tmp_pos = start_pos; |
| 40 | + while (tmp_pos < end_pos) |
34 | 41 | { |
35 | | - has_directions.push_back(has_dir); |
36 | | - has_dir = false; |
| 42 | + String tmp(tmp_pos->begin, tmp_pos->end); |
| 43 | + if (tmp == "desc" || tmp == "asc") |
| 44 | + { |
| 45 | + if (!sort_dir.empty() || !nulls_position.empty()) |
| 46 | + throw Exception(ErrorCodes::SYNTAX_ERROR, "The incomplete fragment is unexpected"); |
| 47 | + --tmp_pos; |
| 48 | + column_expr = validate_column(start_pos, tmp_pos); |
| 49 | + sort_dir = tmp; |
| 50 | + ++tmp_pos; |
| 51 | + } |
| 52 | + if (tmp == "nulls") |
| 53 | + { |
| 54 | + if (!nulls_position.empty()) |
| 55 | + throw Exception(ErrorCodes::SYNTAX_ERROR, "The incomplete fragment is unexpected"); |
| 56 | + auto nulls_pos = tmp_pos; |
| 57 | + ++tmp_pos; |
| 58 | + tmp = String(tmp_pos->begin, tmp_pos->end); |
| 59 | + if (tmp_pos->isEnd() || (tmp != "first" && tmp != "last")) |
| 60 | + throw Exception(ErrorCodes::SYNTAX_ERROR, "Invalid nulls position of sort operator"); |
| 61 | + |
| 62 | + nulls_position = "nulls " + tmp; |
| 63 | + if (column_expr.empty()) |
| 64 | + { |
| 65 | + --nulls_pos; |
| 66 | + column_expr = validate_column(start_pos, nulls_pos); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + ++tmp_pos; |
37 | 71 | } |
| 72 | + --end_pos; |
| 73 | + if (column_expr.empty()) |
| 74 | + column_expr = validate_column(start_pos, end_pos); |
38 | 75 |
|
39 | | - ++new_pos; |
40 | | - } |
41 | | - has_directions.push_back(has_dir); |
| 76 | + if (sort_dir.empty()) |
| 77 | + sort_dir = "desc"; |
| 78 | + if (nulls_position.empty()) |
| 79 | + nulls_position = sort_dir == "desc" ? "nulls last" : "nulls first"; |
| 80 | + return std::format("{} {} {}", getExprFromToken(column_expr, pos.max_depth), sort_dir, nulls_position); |
| 81 | + }; |
42 | 82 |
|
43 | | - for (uint64_t i = 0; i < order_expression_list->children.size(); ++i) |
| 83 | + auto paren_count = 0; |
| 84 | + auto begin = pos; |
| 85 | + while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon) |
44 | 86 | { |
45 | | - if (!has_directions[i]) |
| 87 | + if (pos->type == TokenType::ClosingRoundBracket) |
| 88 | + --paren_count; |
| 89 | + if (pos->type == TokenType::OpeningRoundBracket) |
| 90 | + ++paren_count; |
| 91 | + if (pos->type == TokenType::Comma && paren_count == 0) |
46 | 92 | { |
47 | | - auto *order_expr = order_expression_list->children[i]->as<ASTOrderByElement>(); |
48 | | - order_expr->direction = -1; // default desc |
49 | | - if (!order_expr->nulls_direction_was_explicitly_specified) |
50 | | - order_expr->nulls_direction = -1; |
51 | | - else |
52 | | - order_expr->nulls_direction = order_expr->nulls_direction == 1 ? -1 : 1; |
| 93 | + auto single_sort_expr = format_sort_expr(begin, pos); |
| 94 | + order_list_str = order_list_str.empty() ? single_sort_expr : order_list_str + "," + single_sort_expr; |
| 95 | + begin = pos; |
| 96 | + ++begin; |
53 | 97 | } |
| 98 | + ++pos; |
54 | 99 | } |
55 | 100 |
|
| 101 | + auto single_sort_expr = format_sort_expr(begin, pos); |
| 102 | + order_list_str = order_list_str.empty() ? single_sort_expr : order_list_str + "," + single_sort_expr; |
| 103 | + |
| 104 | + Tokens tokens(order_list_str.c_str(), order_list_str.c_str() + order_list_str.size()); |
| 105 | + IParser::Pos new_pos(tokens, pos.max_depth); |
| 106 | + |
| 107 | + if (!order_list.parse(new_pos, order_expression_list, expected)) |
| 108 | + return false; |
| 109 | + |
56 | 110 | node->as<ASTSelectQuery>()->setExpression(ASTSelectQuery::Expression::ORDER_BY, std::move(order_expression_list)); |
57 | 111 | return true; |
58 | 112 | } |
|
0 commit comments