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 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 @@ -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
4 changes: 2 additions & 2 deletions test/sql/test_function/R/test_get_query_dump
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ select get_query_dump('invalid-query');
-- result:
E: (6013, "SemanticException: Getting analyzing error. Detail message: Invalid parameter get_query_dump: execute query failed. Getting syntax error at line 1, column 0. Detail message: Unexpected input 'invalid', the most similar input is {'INSTALL', 'UNINSTALL', 'KILL', 'LOAD', 'ADMIN', '(', ';'}..")
-- !result
select get_query_dump(lower('select * from t1'));
select get_query_dump(rtrim('select * from t1'));
-- result:
E: (1064, 'Getting analyzing error. Detail message: Meta function get_query_dump does not support non-constant arguments.')
-- !result
Expand Down Expand Up @@ -103,4 +103,4 @@ select get_json_string(x, '$.statement') from query_dump;
-- result:
SELECT *
FROM db_mock_000.tbl_mock_001
-- !result
-- !result
2 changes: 1 addition & 1 deletion test/sql/test_function/T/test_get_query_dump
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ from __row_util;
-- Invalid cases.
select get_query_dump('');
select get_query_dump('invalid-query');
select get_query_dump(lower('select * from t1'));
select get_query_dump(trim('select * from t1'));

-- Valid cases.
with
Expand Down
Loading