Skip to content

Commit

Permalink
fix: honor offset when occurence is 0 (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
shobsi authored Oct 1, 2024
1 parent 1b05b09 commit ba69b58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions udfs/community/cw_regexp_replace_generic.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ LANGUAGE js AS """
if (haystack == null || regexp == null || replacement == null || offset == null || occurrence == null || mode == null) return null;
replacement = replacement.replace('\\\\', '$');
let regExp = new RegExp(regexp, mode);
if (occurrence == 0)
return haystack.replace(regExp, replacement);
const start = offset - 1;
if (occurrence == 0)
return haystack.substr(0, start) + haystack.substr(start).replace(regExp, replacement);
const index = occurrence - 1;
let relevantString = haystack.substring(start);
let a, count = 0;
Expand Down
18 changes: 18 additions & 0 deletions udfs/community/test_cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,24 @@ generate_udf_test("cw_regexp_replace_4", [
expected_output: `"TestCad$123456"`,
},
]);
generate_udf_test("cw_regexp_replace_4", [
{
inputs: [`"TestStr123456Str"`, `"Str"`, `"Cad$"`, `CAST(1 AS INT64)`],
expected_output: `"TestCad$123456Cad$"`,
},
]);
generate_udf_test("cw_regexp_replace_4", [
{
inputs: [`"abaa"`, `"a"`, `"A"`, `CAST(1 AS INT64)`],
expected_output: `"AbAA"`,
},
]);
generate_udf_test("cw_regexp_replace_4", [
{
inputs: [`"abaa"`, `"a"`, `"A"`, `CAST(2 AS INT64)`],
expected_output: `"abAA"`,
},
]);
generate_udf_test("cw_regexp_replace_5", [
{
inputs: [
Expand Down

0 comments on commit ba69b58

Please sign in to comment.