Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Support const folding for replace() #49436

Merged
merged 2 commits into from
Aug 7, 2024
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
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
Loading