Skip to content

Commit

Permalink
[Enhancement] support lower/upper constant fold (backport #50576) (#5…
Browse files Browse the repository at this point in the history
…0654)

Co-authored-by: Murphy <[email protected]>
  • Loading branch information
mergify[bot] and murphyatwork authored Sep 4, 2024
1 parent b00dce5 commit 05ed62e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,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 @@ -1034,7 +1034,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 @@ -1438,6 +1438,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 @@ -454,4 +454,5 @@ public void testNumericLiteralComparison() throws Exception {
connectContext.getSessionVariable().setSqlMode(prevSqlMode);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1541,7 +1541,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

0 comments on commit 05ed62e

Please sign in to comment.