Skip to content

Commit ad3d9cd

Browse files
authored
Fixed ClickHouse#2515 by modifying convertImpl for indexof_regex (#381)
* Fixed ClickHouse#2515 by modifying convertImpl for indexof_regex * Removed a extra comment
1 parent 24a912c commit ad3d9cd

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

src/Parsers/Kusto/KustoFunctions/KQLStringFunctions.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,25 @@ bool IndexOf::convertImpl(String & out, IParser::Pos & pos)
315315

316316
bool IndexOfRegex::convertImpl(String & out, IParser::Pos & pos)
317317
{
318-
return directMapping(out, pos, "kql_indexof_regex");
318+
const auto fn_name = getKQLFunctionName(pos);
319+
if (fn_name.empty())
320+
return false;
321+
322+
const auto source = getArgument(fn_name, pos);
323+
const auto lookup = getArgument(fn_name, pos);
324+
const auto start_index = getOptionalArgument(fn_name, pos);
325+
const auto length = getOptionalArgument(fn_name, pos);
326+
const auto occurrence = getOptionalArgument(fn_name, pos);
327+
328+
out = std::format(
329+
"If(isNULL({0}), -1, kql_indexof_regex(kql_tostring({0}),kql_tostring({1}),{2},{3},{4}))",
330+
source,
331+
lookup,
332+
start_index.value_or("0"),
333+
length.value_or("-1"),
334+
occurrence.value_or("1"));
335+
336+
return true;
319337
}
320338

321339
bool IsAscii::convertImpl(String & out, IParser::Pos & pos)

src/Parsers/tests/KQL/gtest_KQL_StringFunctions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ INSTANTIATE_TEST_SUITE_P(ParserKQLQuery_String, ParserTest,
262262
},
263263
{
264264
"table | project indexof_regex(A, B, C, D, E)",
265-
"SELECT kql_indexof_regex(A, B, C, D, E) AS Column1\nFROM table"
265+
"SELECT If(A IS NULL, -1, kql_indexof_regex(kql_tostring(A), kql_tostring(B), C, D, E)) AS Column1\nFROM table"
266266
},
267267
{
268268
"Customers | project t = isascii(FirstName)",

tests/queries/0_stateless/02366_kql_func_string.reference

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ Latoya Shen Professional Graduate Degree 25
428428
-1
429429
-1
430430
\N
431+
-1
431432
2
432433
\N
433434
-1

tests/queries/0_stateless/02366_kql_func_string.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ print idx2 = indexof_regex("abcabcdefg", "a.c", 0, 9, 2);
422422
print idx3 = indexof_regex("abcabc", "a.c", 1, -1, 2);
423423
print idx4 = indexof_regex("ababaa", "a.a", 0, -1, 2);
424424
print idx5 = indexof_regex("abcabc", "a|ab", -1);
425+
print idx6 = indexof_regex(int(null), '.');
425426
print indexof_regex('adsasdasasd', 'sas');
426427
print indexof_regex('adsasdasasd', 'sas', -1);
427428
print indexof_regex('adsasdasasd', 'sas', 99);

0 commit comments

Comments
 (0)