Skip to content
Merged
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
19 changes: 16 additions & 3 deletions src/Parsers/Kusto/KustoFunctions/IParserKQLFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ String IParserKQLFunction::getConvertedArgument(const String & fn_name, IParser:
array_index += getExpression(pos);
++pos;
}
token = std::format("[ {0} >=0 ? {0} + 1 : {0}]", array_index);
if (Int64 index; (boost::conversion::try_lexical_convert(array_index, index)))
{
auto ch_index = index >= 0 ? index + 1 : index;
token = std::format("[{0}]", ch_index);
}
else
token = std::format("[ {0} >=0 ? {0} + 1 : {0}]", array_index);
}
else
token = String(pos->begin, pos->end);
Expand Down Expand Up @@ -354,9 +360,16 @@ String IParserKQLFunction::getExpression(IParser::Pos & pos)
array_index += getExpression(pos);
++pos;
}
arg = std::format("[ {0} >=0 ? {0} + 1 : {0}]", array_index);
if (Int64 index; (boost::conversion::try_lexical_convert(array_index, index)))
{
auto ch_index = index >= 0 ? index + 1 : index;
arg = std::format("[{0}]", ch_index);
}
else
{
arg = std::format("[ {0} >=0 ? {0} + 1 : {0}]", array_index);
}
}

return arg;
}

Expand Down
26 changes: 19 additions & 7 deletions src/Parsers/tests/KQL/gtest_KQL_dynamicFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_Dynamic, ParserTest,
},
{
"print array_sort_asc(dynamic([2, 1, null,3]), dynamic([20, 10, 40, 30]))[0]",
"SELECT tupleElement(kql_array_sort_asc([2, 1, NULL, 3], [20, 10, 40, 30]), if(0 >= 0, 0 + 1, 0)) AS print_0"
"SELECT kql_array_sort_asc([2, 1, NULL, 3], [20, 10, 40, 30]).1 AS print_0"
},
{
"print (t) = array_sort_asc(dynamic([2, 1, null,3]), dynamic([20, 10, 40, 30]))",
Expand All @@ -103,31 +103,43 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_Dynamic, ParserTest,
},
{
"print A[0]",
"SELECT A[if(0 >= 0, 0 + 1, 0)] AS print_0"
"SELECT A[1] AS print_0"
},
{
"print A[0][1]",
"SELECT (A[if(0 >= 0, 0 + 1, 0)])[if(1 >= 0, 1 + 1, 1)] AS print_0"
"SELECT (A[1])[2] AS print_0"
},
{
"print A[-5]",
"SELECT A[-5] AS print_0"
},
{
"print A[-1][1]",
"SELECT (A[-1])[2] AS print_0"
},
{
"print dynamic([[1,2,3,4,5],[20,30]])[0]",
"SELECT [[1, 2, 3, 4, 5], [20, 30]][if(0 >= 0, 0 + 1, 0)] AS print_0"
"SELECT [[1, 2, 3, 4, 5], [20, 30]][1] AS print_0"
},
{
"print dynamic([[1,2,3,4,5],[20,30]])[1][1]",
"SELECT ([[1, 2, 3, 4, 5], [20, 30]][if(1 >= 0, 1 + 1, 1)])[if(1 >= 0, 1 + 1, 1)] AS print_0"
"SELECT ([[1, 2, 3, 4, 5], [20, 30]][2])[2] AS print_0"
},
{
"print dynamic([[1,2,3,4,5],[20,30]])[1][-1]",
"SELECT ([[1, 2, 3, 4, 5], [20, 30]][2])[-1] AS print_0"
},
{
"print A[B[1]]",
"SELECT A[if((B[if(1 >= 0, 1 + 1, 1)]) >= 0, (B[if(1 >= 0, 1 + 1, 1)]) + 1, B[if(1 >= 0, 1 + 1, 1)])] AS print_0"
"SELECT A[if((B[2]) >= 0, (B[2]) + 1, B[2])] AS print_0"
Copy link

@kashwy kashwy Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this recursive case be solved too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to get the value of B[2] to correctly map it, which isn't available during Parsing. Therefore, treating B[2] as an arbitrary expression.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to get the value of B[2] to correctly map it, which isn't available during Parsing. Therefore, treating B[2] as an arbitrary expression.

ok, that's good

},
{
"print A[strlen('a')-1]",
"SELECT A[if((lengthUTF8('a') - 1) >= 0, (lengthUTF8('a') - 1) + 1, lengthUTF8('a') - 1)] AS print_0"
},
{
"print strlen(A[0])",
"SELECT lengthUTF8(A[if(0 >= 0, 0 + 1, 0)]) AS print_0"
"SELECT lengthUTF8(A[1]) AS print_0"
},
{
"print repeat(1, 3)",
Expand Down