diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java index ad6c9ee318915..6edc0405e6b15 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctions.java @@ -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) { diff --git a/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoFunctionTransformTest.java b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoFunctionTransformTest.java index 27992d14ee9fe..712f00a224641 100644 --- a/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoFunctionTransformTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/connector/parser/trino/TrinoFunctionTransformTest.java @@ -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')"); diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java index c686e46052d87..d6736f120946a 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/optimizer/rewrite/ScalarOperatorFunctionsTest.java @@ -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