Skip to content

Commit

Permalink
[Enhancement] support lower/upper constant fold (#50576)
Browse files Browse the repository at this point in the history
Signed-off-by: Murphy <[email protected]>
(cherry picked from commit e09b33a)
  • Loading branch information
murphyatwork authored and mergify[bot] committed Sep 4, 2024
1 parent fe43dbe commit 3c7e8f8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,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) {
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 @@ -1561,7 +1561,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

0 comments on commit 3c7e8f8

Please sign in to comment.