From 61877a40e5db127bf73ccd4799dabc2b15d6a82c Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Mon, 12 Sep 2022 10:13:18 -0700 Subject: [PATCH] Simplify KQL trim functions to avoid aliases --- .../KustoFunctions/KQLStringFunctions.cpp | 31 +++++++------------ .../tests/KQL/gtest_KQL_StringFunctions.cpp | 8 ++--- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp index 2085918ada0c..f37563ecccd3 100644 --- a/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp +++ b/src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp @@ -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; } diff --git a/src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp b/src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp index ea010210ea14..12ad5c6d563c 100644 --- a/src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp +++ b/src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp @@ -2,7 +2,7 @@ #include -INSTANTIATE_TEST_SUITE_P(ParserKQLQuery, ParserTest, +INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_String, ParserTest, ::testing::Combine( ::testing::Values(std::make_shared()), ::testing::ValuesIn(std::initializer_list{ @@ -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)",