Skip to content

Commit

Permalink
Simplify KQL trim functions to avoid aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrk2 authored and kashwy committed Aug 26, 2023
1 parent e102a76 commit 61877a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
31 changes: 12 additions & 19 deletions src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,48 +547,41 @@ bool Translate::convertImpl(String & out,IParser::Pos & pos)
return true;
}

bool Trim::convertImpl(String & out,IParser::Pos & pos)
bool Trim::convertImpl(String & out, IParser::Pos & pos)
{
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;

++pos;
String regex = getConvertedArgument(fn_name, pos);
++pos;
String source = getConvertedArgument(fn_name, pos);
String ltrim = std::format("if ((replaceRegexpOne(concat('start_random_str_', {0}) as srcl, concat('start_random_str_', {1}),'') as dstl) = srcl, {0}, dstl)", source, regex);
out = std::format("if ((replaceRegexpOne(concat({0}, '_end_random_str') as srcr, concat({1}, '_end_random_str'),'') as dstr) = srcr, {0}, dstr)", ltrim, regex);
const auto regex = getArgument(fn_name, pos);
const auto source = getArgument(fn_name, pos);
out = kqlCallToExpression("trim_start", {regex, std::format("trim_end({0}, {1})", regex, source)}, pos.max_depth);

return true;
}

bool TrimEnd::convertImpl(String & out,IParser::Pos & pos)
bool TrimEnd::convertImpl(String & out, IParser::Pos & pos)
{
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;

++pos;
String regex = getConvertedArgument(fn_name, pos);
++pos;
String source = getConvertedArgument(fn_name, pos);
out = std::format("if ((replaceRegexpOne(concat({0}, '_end_random_str') as src, concat({1},'_end_random_str'),'') as dst) = src, {0}, dst)", source, regex);
const auto regex = getArgument(fn_name, pos);
const auto source = getArgument(fn_name, pos);
out = std::format("replaceRegexpOne({0}, concat({1}, '$'), '')", source, regex);

return true;
}

bool TrimStart::convertImpl(String & out,IParser::Pos & pos)
bool TrimStart::convertImpl(String & out, IParser::Pos & pos)
{
const String fn_name = getKQLFunctionName(pos);
if (fn_name.empty())
return false;

++pos;
String regex = getConvertedArgument(fn_name, pos);
++pos;
String source = getConvertedArgument(fn_name, pos);
out = std::format("if ((replaceRegexpOne(concat('start_random_str_', {0}) as src, concat('start_random_str_', {1}),'') as dst) = src, {0}, dst)", source, regex);
const auto regex = getArgument(fn_name, pos);
const auto source = getArgument(fn_name, pos);
out = std::format("replaceRegexpOne({0}, concat('^', {1}), '')", source, regex);

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Parsers/Kusto/ParserKQLQuery.h>

INSTANTIATE_TEST_SUITE_P(ParserKQLQuery, ParserTest,
INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_String, ParserTest,
::testing::Combine(
::testing::Values(std::make_shared<DB::ParserKQLQuery>()),
::testing::ValuesIn(std::initializer_list<ParserTestCase>{
Expand Down Expand Up @@ -36,15 +36,15 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery, ParserTest,
},
{
"print trim_start('[^\\w]+', strcat('- ','Te st1','// $'))",
"SELECT if((replaceRegexpOne(concat('start_random_str_', concat('- ', 'Te st1', '// $')) AS src, concat('start_random_str_', '[^\\\\w]+'), '') AS dst) = src, concat('- ', 'Te st1', '// $'), dst)"
"SELECT replaceRegexpOne(concat('- ', 'Te st1', '// $'), concat('^', '[^\\\\w]+'), '')"
},
{
"print trim_end('.com', 'bing.com')",
"SELECT if((replaceRegexpOne(concat('bing.com', '_end_random_str') AS src, concat('.com', '_end_random_str'), '') AS dst) = src, 'bing.com', dst)"
"SELECT replaceRegexpOne('bing.com', concat('.com', '$'), '')"
},
{
"print trim('--', '--https://bing.com--')",
"SELECT if((replaceRegexpOne(concat(if((replaceRegexpOne(concat('start_random_str_', '--https://bing.com--') AS srcl, concat('start_random_str_', '--'), '') AS dstl) = srcl, '--https://bing.com--', dstl), '_end_random_str') AS srcr, concat('--', '_end_random_str'), '') AS dstr) = srcr, if(dstl = srcl, '--https://bing.com--', dstl), dstr)"
"SELECT replaceRegexpOne(replaceRegexpOne('--https://bing.com--', concat('--', '$'), ''), concat('^', '--'), '')"
},
{
"print bool(1)",
Expand Down

0 comments on commit 61877a4

Please sign in to comment.