Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 53 additions & 36 deletions src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
#include "IParserKQLFunction.h"

#include "KQLFunctionFactory.h"

#include <Parsers/Kusto/ParserKQLOperators.h>

namespace DB
{
#include <format>
#include <numeric>

namespace ErrorCodes
namespace DB::ErrorCodes
{
extern const int SYNTAX_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}


namespace DB
{
bool IParserKQLFunction::convert(String & out,IParser::Pos & pos)
{
return wrapConvertImpl(pos, IncreaseDepthTag{}, [&]
Expand All @@ -38,11 +26,11 @@ bool IParserKQLFunction::convert(String & out,IParser::Pos & pos)
});
}

bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const String & ch_fn)
bool IParserKQLFunction::directMapping(String & out, IParser::Pos & pos, const String & ch_fn)
{
std::vector<String> arguments;

String fn_name = getKQLFunctionName(pos);
String fn_name = getKQLFunctionName(pos);

if (fn_name.empty())
return false;
Expand All @@ -52,17 +40,17 @@ bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const Str
++pos;
while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
{
String argument = getConvertedArgument(fn_name,pos);
String argument = getConvertedArgument(fn_name, pos);
arguments.push_back(argument);

if (pos->type == TokenType::ClosingRoundBracket)
{
for (auto arg : arguments)
for (auto arg : arguments)
{
if (res.empty())
res = ch_fn + "(" + arg;
else
res = res + ", "+ arg;
res = res + ", " + arg;
}
res += ")";

Expand All @@ -76,6 +64,11 @@ bool IParserKQLFunction::directMapping(String & out,IParser::Pos & pos,const Str
return false;
}

String IParserKQLFunction::getArgument(const String & function_name, IParser::Pos & pos)
{
return getOptionalArgument(function_name, pos).value();
}

String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser::Pos & pos)
{
String converted_arg;
Expand All @@ -90,13 +83,13 @@ String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser:

while (!pos->isEnd() && pos->type != TokenType::PipeMark && pos->type != TokenType::Semicolon)
{
String token = String(pos->begin,pos->end);
String token = String(pos->begin, pos->end);
String new_token;
if (!KQLOperators().convert(tokens,pos))
if (!KQLOperators().convert(tokens, pos))
{
if (pos->type == TokenType::BareWord )
if (pos->type == TokenType::BareWord)
{
tokens.push_back(IParserKQLFunction::getExpression(pos));
tokens.push_back(getExpression(pos));
}
else if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket)
{
Expand All @@ -109,8 +102,8 @@ String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser:
if (pos->type == TokenType::Comma || pos->type == TokenType::ClosingRoundBracket)
break;
}
for (auto token : tokens)
converted_arg = converted_arg + token +" ";
for (auto token : tokens)
converted_arg = converted_arg + token + " ";

return converted_arg;
}
Expand All @@ -124,19 +117,44 @@ String IParserKQLFunction::getKQLFunctionName(IParser::Pos & pos)
--pos;
return "";
}
return fn_name;
return fn_name;
}

std::optional<String> IParserKQLFunction::getOptionalArgument(const String & function_name, IParser::Pos & pos)
{
std::optional<String> argument;
if (const auto & type = pos->type; type != TokenType::Comma && type != TokenType::OpeningRoundBracket)
return {};

++pos;
return getConvertedArgument(function_name, pos);
}

String IParserKQLFunction::kqlCallToExpression(
const String & function_name, std::initializer_list<std::reference_wrapper<const String>> params, const uint32_t max_depth)
{
const auto params_str = std::accumulate(
std::cbegin(params),
std::cend(params),
String(),
[](auto acc, const auto & param) { return (acc.empty() ? "" : ", ") + std::move(acc) + param.get(); });

const auto kql_call = std::format("{}({})", function_name, params_str);
Tokens call_tokens(kql_call.c_str(), kql_call.c_str() + kql_call.length());
IParser::Pos tokens_pos(call_tokens, max_depth);
return getExpression(tokens_pos);
}

void IParserKQLFunction::validateEndOfFunction(const String & fn_name, IParser::Pos & pos)
{
if (pos->type != TokenType:: ClosingRoundBracket)
if (pos->type != TokenType::ClosingRoundBracket)
throw Exception("Too many arguments in function: " + fn_name, ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
}

String IParserKQLFunction::getExpression(IParser::Pos & pos)
{
String arg = String(pos->begin, pos->end);
if (pos->type == TokenType::BareWord )
if (pos->type == TokenType::BareWord)
{
String new_arg;
auto fun = KQLFunctionFactory::get(arg);
Expand All @@ -148,5 +166,4 @@ String IParserKQLFunction::getExpression(IParser::Pos & pos)
}
return arg;
}

}
26 changes: 18 additions & 8 deletions src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/IParser.h>

namespace DB
{
class IParserKQLFunction
Expand All @@ -16,7 +16,11 @@ class IParserKQLFunction
pos = begin;
return res;
}
struct IncreaseDepthTag {};

struct IncreaseDepthTag
{
};

template <typename F>
ALWAYS_INLINE static bool wrapConvertImpl(IParser::Pos & pos, IncreaseDepthTag, const F & func)
{
Expand All @@ -28,17 +32,23 @@ class IParserKQLFunction
pos = begin;
return res;
}
bool convert(String & out,IParser::Pos & pos);

bool convert(String & out, IParser::Pos & pos);
virtual const char * getName() const = 0;
virtual ~IParserKQLFunction() = default;

static String getExpression(IParser::Pos & pos);

protected:
virtual bool convertImpl(String & out,IParser::Pos & pos) = 0;
static bool directMapping(String &out,IParser::Pos & pos,const String & ch_fn);
virtual bool convertImpl(String & out, IParser::Pos & pos) = 0;

static bool directMapping(String & out, IParser::Pos & pos, const String & ch_fn);
static String getArgument(const String & function_name, IParser::Pos & pos);
static String getConvertedArgument(const String & fn_name, IParser::Pos & pos);
static std::optional<String> getOptionalArgument(const String & function_name, IParser::Pos & pos);
static String kqlCallToExpression(
const String & function_name, std::initializer_list<std::reference_wrapper<const String>> params, uint32_t max_depth);
static void validateEndOfFunction(const String & fn_name, IParser::Pos & pos);
static String getKQLFunctionName(IParser::Pos & pos);
};

}

17 changes: 1 addition & 16 deletions src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include "KQLAggregationFunctions.h"

namespace DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include "IParserKQLFunction.h"

namespace DB
{
class ArgMax : public IParserKQLFunction
Expand Down
17 changes: 1 addition & 16 deletions src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include "KQLBinaryFunctions.h"

namespace DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include "IParserKQLFunction.h"

namespace DB
{
class BinaryAnd : public IParserKQLFunction
Expand Down
5 changes: 1 addition & 4 deletions src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include "KQLCastingFunctions.h"

namespace DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include "IParserKQLFunction.h"

namespace DB
{
class ToBool : public IParserKQLFunction
Expand Down
17 changes: 1 addition & 16 deletions src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include "KQLDateTimeFunctions.h"

namespace DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include "IParserKQLFunction.h"

namespace DB
{

Expand Down
17 changes: 1 addition & 16 deletions src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include "KQLDynamicFunctions.h"

namespace DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <Parsers/IParserBase.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include "IParserKQLFunction.h"

namespace DB
{
class ArrayConcat : public IParserKQLFunction
Expand Down
29 changes: 12 additions & 17 deletions src/Parsers/Kusto/KustoFunctions/KQLFunctionFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#include <Parsers/IParserBase.h>
#include <Parsers/ParserSetQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/Kusto/ParserKQLQuery.h>
#include <Parsers/Kusto/ParserKQLStatement.h>
#include <Parsers/Kusto/KustoFunctions/IParserKQLFunction.h>
#include <Parsers/Kusto/KustoFunctions/KQLDateTimeFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLStringFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLDynamicFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLCastingFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLAggregationFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLTimeSeriesFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLIPFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLBinaryFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLGeneralFunctions.h>
#include <Parsers/Kusto/KustoFunctions/KQLFunctionFactory.h>
#include "KQLFunctionFactory.h"

#include "IParserKQLFunction.h"
#include "KQLDateTimeFunctions.h"
#include "KQLStringFunctions.h"
#include "KQLDynamicFunctions.h"
#include "KQLCastingFunctions.h"
#include "KQLAggregationFunctions.h"
#include "KQLTimeSeriesFunctions.h"
#include "KQLIPFunctions.h"
#include "KQLBinaryFunctions.h"
#include "KQLGeneralFunctions.h"

namespace DB
{
Expand Down
Loading