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 lower/upper constant fold #50576

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1248,6 +1248,16 @@ public static ConstantOperator substring(ConstantOperator value, ConstantOperato
return ConstantOperator.createVarchar(string.substring(beginIndex, endIndex));
}

@ConstantFunction(name = "lower", argTypes = {VARCHAR}, returnType = VARCHAR)
public static ConstantOperator lower(ConstantOperator str) {
return ConstantOperator.createVarchar(StringUtils.lowerCase(str.getVarchar()));
}

@ConstantFunction(name = "upper", argTypes = {VARCHAR}, returnType = VARCHAR)
public static ConstantOperator upper(ConstantOperator str) {
return ConstantOperator.createVarchar(StringUtils.upperCase(str.getVarchar()));
}

@ConstantFunction(name = "replace", argTypes = {VARCHAR, VARCHAR, VARCHAR}, returnType = VARCHAR)
public static ConstantOperator replace(ConstantOperator value, ConstantOperator target,
ConstantOperator replacement) {
murphyatwork marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ public void testTrim() throws Exception {
assertPlanContains(sql, "<slot 2> : trim(' abcd')");

sql = "select trim(trailing 'ER' from upper('worker'));";
assertPlanContains(sql, "<slot 2> : rtrim(upper('worker'), 'ER')");
assertPlanContains(sql, "<slot 2> : rtrim('WORKER', 'ER')");

sql = "select trim(trailing from ' abcd');";
assertPlanContains(sql, "<slot 2> : rtrim(' abcd')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,16 @@ public void testReplace() {
).getVarchar());
}

@Test
public void testLowerUpper() {
assertEquals("aaa", ScalarOperatorFunctions.lower(
new ConstantOperator("AAA", Type.VARCHAR)
).getVarchar());
assertEquals("AAA", ScalarOperatorFunctions.upper(
new ConstantOperator("aaa", Type.VARCHAR)
).getVarchar());
}

/*
test cases are generated by the following SQL by capturing:
1. leap year
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public void testGetQueryDump() throws Exception {

// Non-constant arguments.
{
String sql = "SELECT get_query_dump(lower('select count(v1) from t0')) from t0";
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ public void testConstantFoldInLikeFunction() throws Exception {

String sql2 = "select ilike('AA', concat('a', 'A'))";
String plan2 = getFragmentPlan(sql2);
assertContains(plan2, "<slot 2> : like(lower('AA'), lower('aA'))");
assertContains(plan2, "<slot 2> : like('aa', 'aa')");
}

@Test
Expand Down
Loading