forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from ClibMouse/Kusto-p3-tophitter
Kusto p3 tophitter
- Loading branch information
Showing
10 changed files
with
392 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <Parsers/ExpressionListParsers.h> | ||
#include <Parsers/Kusto/ParserKQLQuery.h> | ||
#include <Parsers/Kusto/ParserKQLTop.h> | ||
#include <format> | ||
|
||
namespace DB | ||
{ | ||
namespace ErrorCodes | ||
{ | ||
extern const int SYNTAX_ERROR; | ||
} | ||
|
||
bool ParserKQLTop::parseImpl(Pos & /*pos*/, ASTPtr & /*node*/, Expected & /*expected*/) | ||
{ | ||
return true; | ||
} | ||
|
||
bool ParserKQLTop::updatePipeLine (OperationsPos & operations, String & query) | ||
{ | ||
Pos pos = operations.back().second; | ||
|
||
if (pos->isEnd() || pos->type == TokenType::PipeMark || pos->type == TokenType::Semicolon) | ||
throw Exception("Syntax error near top operator", ErrorCodes::SYNTAX_ERROR); | ||
|
||
Pos start_pos = operations.front().second; | ||
Pos end_pos = pos; | ||
--end_pos; | ||
--end_pos; | ||
|
||
String prev_query(start_pos->begin, end_pos->end); | ||
|
||
String limit_expr, sort_expr; | ||
start_pos = pos; | ||
end_pos = pos; | ||
while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon) | ||
{ | ||
if (String(pos->begin, pos->end) == "by") | ||
{ | ||
auto limt_end_pos = pos; | ||
--limt_end_pos; | ||
limit_expr = String(start_pos->begin, limt_end_pos->end); | ||
start_pos = pos; | ||
++start_pos; | ||
} | ||
end_pos = pos; | ||
++pos; | ||
} | ||
sort_expr = (start_pos <= end_pos) ? String(start_pos->begin, end_pos->end) : ""; | ||
if (limit_expr.empty() || sort_expr.empty()) | ||
throw Exception("top operator need a by clause", ErrorCodes::SYNTAX_ERROR); | ||
|
||
query = std::format("{} sort by {} | take {}", prev_query, sort_expr, limit_expr); | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <Parsers/IParserBase.h> | ||
#include <Parsers/Kusto/ParserKQLQuery.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
class ParserKQLTop : public ParserKQLBase | ||
{ | ||
protected: | ||
const char * getName() const override { return "KQL top"; } | ||
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; | ||
bool updatePipeLine (OperationsPos & operations, String & query) override; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <Parsers/ExpressionListParsers.h> | ||
#include <Parsers/Kusto/ParserKQLQuery.h> | ||
#include <Parsers/Kusto/ParserKQLTopHitter.h> | ||
#include <format> | ||
|
||
namespace DB | ||
{ | ||
namespace ErrorCodes | ||
{ | ||
extern const int SYNTAX_ERROR; | ||
} | ||
|
||
bool ParserKQLTopHitters::parseImpl(Pos & /*pos*/, ASTPtr & /*node*/, Expected & /*expected*/) | ||
{ | ||
return true; | ||
} | ||
|
||
bool ParserKQLTopHitters::updatePipeLine (OperationsPos & operations, String & query) | ||
{ | ||
Pos pos = operations.back().second; | ||
|
||
if (pos->isEnd() || pos->type == TokenType::PipeMark || pos->type == TokenType::Semicolon) | ||
throw Exception("Syntax error near top-hitters operator", ErrorCodes::SYNTAX_ERROR); | ||
|
||
Pos start_pos = operations.front().second; | ||
Pos end_pos = pos; | ||
--end_pos; | ||
--end_pos; | ||
--end_pos; | ||
--end_pos; | ||
|
||
String prev_query(start_pos->begin, end_pos->end); | ||
|
||
String number_of_values, value_expression, summing_expression; | ||
start_pos = pos; | ||
end_pos = pos; | ||
while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon) | ||
{ | ||
if (String(pos->begin, pos->end) == "of") | ||
{ | ||
auto number_end_pos = pos; | ||
--number_end_pos; | ||
number_of_values = String(start_pos->begin, number_end_pos->end); | ||
start_pos = pos; | ||
++start_pos; | ||
} | ||
|
||
if (String(pos->begin, pos->end) == "by") | ||
{ | ||
auto expr_end_pos = pos; | ||
--expr_end_pos; | ||
value_expression = String(start_pos->begin, expr_end_pos->end); | ||
start_pos = pos; | ||
++start_pos; | ||
} | ||
end_pos = pos; | ||
++pos; | ||
} | ||
|
||
if (value_expression.empty()) | ||
value_expression = (start_pos <= end_pos) ? String(start_pos->begin, end_pos->end) : ""; | ||
else | ||
summing_expression = (start_pos <= end_pos) ? String(start_pos->begin, end_pos->end) : ""; | ||
|
||
if (number_of_values.empty() || value_expression.empty()) | ||
throw Exception("top-hitter operator need a ValueExpression", ErrorCodes::SYNTAX_ERROR); | ||
|
||
if (summing_expression.empty()) | ||
query = std::format("{0} summarize approximate_count_{1} = count() by {1} | sort by approximate_count_{1} desc | take {2} ", prev_query, value_expression, number_of_values); | ||
else | ||
query = std::format("{0} summarize approximate_sum_{1} = sum({1}) by {2} | sort by approximate_sum_{1} desc | take {3}", prev_query, summing_expression, value_expression, number_of_values); | ||
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include <Parsers/IParserBase.h> | ||
#include <Parsers/Kusto/ParserKQLQuery.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
class ParserKQLTopHitters : public ParserKQLBase | ||
{ | ||
protected: | ||
const char * getName() const override { return "KQL top-hitters"; } | ||
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override; | ||
bool updatePipeLine (OperationsPos & operations, String & query) override; | ||
}; | ||
|
||
} |
Oops, something went wrong.