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

[BugFix] Fix REPLACE constant fold (backport #50828) #50846

Closed
wants to merge 1 commit into from
Closed
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 @@ -1303,7 +1303,8 @@ public static ConstantOperator upper(ConstantOperator str) {
@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()));
return ConstantOperator.createVarchar(
StringUtils.replace(value.getVarchar(), target.getVarchar(), replacement.getVarchar()));
}

private static ConstantOperator createDecimalConstant(BigDecimal result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1431,11 +1431,21 @@ public void testSubString() {

@Test
public void testReplace() {
assertEquals("20240806", ScalarOperatorFunctions.replace(
new ConstantOperator("2024-08-06", Type.VARCHAR),
new ConstantOperator("-", Type.VARCHAR),
new ConstantOperator("", Type.VARCHAR)
).getVarchar());
// arg0, arg1, arg2, expected_result
String[][] testCases = {
{"2024-08-06", "-", "", "20240806"},
{"abc def ghi", "", "1234", "abc def ghi"},
{"abc def ghi abc", "abc", "1234", "1234 def ghi 1234"},
{"", "abc", "1234", ""}
};

for (String[] tc : testCases) {
assertEquals("Test case: " + Arrays.toString(tc), tc[3], ScalarOperatorFunctions.replace(
new ConstantOperator(tc[0], Type.VARCHAR),
new ConstantOperator(tc[1], Type.VARCHAR),
new ConstantOperator(tc[2], Type.VARCHAR)
).getVarchar());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,104 @@ public void testNumericLiteralComparison() throws Exception {
}
}

<<<<<<< HEAD
=======
@Test
public void testGetQueryDump() throws Exception {
DumpInfo prevDumpInfo = connectContext.getDumpInfo();

try {
connectContext.setDumpInfo(null);

// Non-constant arguments.
{
String sql = "SELECT get_query_dump(rtrim('select count(v1) from t0')) from t0";
Assert.assertThrows("Meta function get_query_dump does not support non-constant arguments",
SemanticException.class, () -> getFragmentPlan(sql));
}

// Success cases.
{
String sql = "SELECT get_query_dump('select count(v1) from t0', false) from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "{\"statement\":\"select count(v1) from t0\"");
}

{
String sql = "SELECT get_query_dump('select count(v1) from t0', true) from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "{\"statement\":\"SELECT count(tbl_mock_001.mock_002)...");
}

{
String sql = "SELECT get_query_dump('select count(v1) from t0') from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "{\"statement\":\"select count(v1) from t0\"");
}

{
String sql = "SELECT get_query_dump(concat('select count(v1)', ' from t0')) from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "{\"statement\":\"select count(v1) from t0\"");
}

// Failed cases.
{
String sql = "SELECT get_query_dump('') from t0";
Assert.assertThrows("Invalid parameter get_query_dump: query is empty",
StarRocksPlannerException.class, () -> getFragmentPlan(sql));
}
{
String sql = "SELECT get_query_dump('not-a-query') from t0";
Assert.assertThrows("Invalid parameter get_query_dump: execute query failed.",
StarRocksPlannerException.class, () -> getFragmentPlan(sql));
}

// Success cases after failed cases.
{
String sql = "SELECT get_query_dump(concat('select count(v1)', ' from t0')) from t0";
String plan = getFragmentPlan(sql);
assertContains(plan, "{\"statement\":\"select count(v1) from t0\"");
}

} finally {
connectContext.setDumpInfo(prevDumpInfo);
}

}

@Test
public void testReplace() throws Exception {
{
String plan = getFragmentPlan("SELECT REPLACE('abc def ghi abc', '', '1234')");
assertContains(plan, "<slot 2> : 'abc def ghi abc'");
}

{
String plan = getFragmentPlan("SELECT REPLACE('abc def ghi abc', 'abc', '1234')");
assertContains(plan, "<slot 2> : '1234 def ghi 1234'");
}

{
String plan = getFragmentPlan("SELECT REPLACE('', 'abc', '1234')");
assertContains(plan, "<slot 2> : ''");
}

{
String plan = getFragmentPlan("SELECT REPLACE(NULL, 'abc', '1234')");
assertContains(plan, "<slot 2> : NULL");
}

{
String plan = getFragmentPlan("SELECT REPLACE('abc def ghi abc', NULL, '1234')");
assertContains(plan, "<slot 2> : NULL");
}

{
String plan = getFragmentPlan("SELECT REPLACE('abc def ghi abc', 'abc', NULL)");
assertContains(plan, "<slot 2> : NULL");
}

}
>>>>>>> fe04681389 ([BugFix] Fix REPLACE constant fold (#50828))
}
Loading