Skip to content

Commit

Permalink
[Enhancement] Support const folding for replace() (#49436)
Browse files Browse the repository at this point in the history
Signed-off-by: kaijian.ding <[email protected]>
(cherry picked from commit b2436c7)
  • Loading branch information
kaijianding authored and mergify[bot] committed Aug 7, 2024
1 parent 857dd73 commit fb29681
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,12 @@ public static ConstantOperator substring(ConstantOperator value, ConstantOperato
return ConstantOperator.createVarchar(string.substring(beginIndex, endIndex));
}

@ConstantFunction(name = "replace", argTypes = {VARCHAR, VARCHAR, VARCHAR}, returnType = VARCHAR)
public static ConstantOperator replace(ConstantOperator value, ConstantOperator target,
ConstantOperator replacement) {
return ConstantOperator.createVarchar(value.getVarchar().replace(target.getVarchar(), replacement.getVarchar()));
}

private static ConstantOperator createDecimalConstant(BigDecimal result) {
Type type;
if (!Config.enable_decimal_v3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ public void testStringFnTransform() throws Exception {
assertPlanContains(sql, "str_to_map(split('a:1,b:2,c:null', ','), ':')");

sql = "SELECT replace('hello-world', '-');";
assertPlanContains(sql, "replace('hello-world', '-', '')");
assertPlanContains(sql, "'helloworld'");

sql = "SELECT replace('hello-world', '-', '$');";
assertPlanContains(sql, "replace('hello-world', '-', '$')");
assertPlanContains(sql, "'hello$world'");

sql = "select index('hello', 'l')";
assertPlanContains(sql, "instr('hello', 'l')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,15 @@ public void testUrlExtractParameter() {
ConstantOperator.createNull(Type.VARCHAR));
}

@Test
public void testReplace() {
assertEquals("20240806", ScalarOperatorFunctions.replace(
new ConstantOperator("2024-08-06", Type.VARCHAR),
new ConstantOperator("-", Type.VARCHAR),
new ConstantOperator("", Type.VARCHAR)
).getVarchar());
}

/*
test cases are generated by the following SQL by capturing:
1. leap year
Expand Down

0 comments on commit fb29681

Please sign in to comment.