Skip to content

Commit

Permalink
[Enhancement] Support const folding for replace() (backport #49436) (#…
Browse files Browse the repository at this point in the history
…50572)

Co-authored-by: kaijianding <[email protected]>
  • Loading branch information
mergify[bot] and kaijianding authored Sep 3, 2024
1 parent 550f4b8 commit db0387b
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 @@ -1290,6 +1290,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 @@ -254,10 +254,10 @@ public void testStringFnTransform() throws Exception {
assertPlanContains(sql, "char_length('aaa')");

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 @@ -1429,6 +1429,15 @@ public void testSubString() {
new ConstantOperator(10, Type.INT)).getVarchar());
}

@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 db0387b

Please sign in to comment.