Skip to content

Commit

Permalink
[CALCITE-6780] AbstractSqlTester fails to build query for expression …
Browse files Browse the repository at this point in the history
…TRIM(string)
  • Loading branch information
ILuffZhe authored and NobiGo committed Jan 20, 2025
1 parent ab8843d commit b852db2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,8 @@ ExInst<CalciteException> illegalArgumentForTableFunctionCall(String a0,
@BaseMessage("Illegal arguments: The length of the keys array {0,number,#} is not equal to the length of the values array {1,number,#} in MAP_FROM_ARRAYS function")
ExInst<CalciteException> illegalArgumentsInMapFromArraysFunc(int arg0, int arg1);

@BaseMessage("Trim error: trim character must be exactly 1 character")
ExInst<CalciteException> trimError();
@BaseMessage("Invalid argument ''{0}'': the length of the string describing the trimmed character must be 1")
ExInst<CalciteException> trimError(String seek);

@BaseMessage("Invalid types for arithmetic: {0} {1} {2}")
ExInst<CalciteException> invalidTypesForArithmetic(String clazzName0, String op,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ public static String trim(boolean left, boolean right, String seek,
public static String trim(boolean left, boolean right, String seek,
String s, boolean strict) {
if (strict && seek.length() != 1) {
throw RESOURCE.trimError().ex();
throw RESOURCE.trimError(seek).ex();
}
int j = s.length();
if (right) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ IllegalMapValuesWithNullKey=Illegal arguments for 'MAP_VALUES' function: using a
InvalidPrecisionForDecimalType=DECIMAL precision {0,number,#} must be between 1 and {1,number,#}
InvalidScaleForDecimalType=DECIMAL scale {0,number,#} must be between {1,number,#} and {2,number,#}
IllegalArgumentsInMapFromArraysFunc=Illegal arguments: The length of the keys array {0,number,#} is not equal to the length of the values array {1,number,#} in MAP_FROM_ARRAYS function
TrimError=Trim error: trim character must be exactly 1 character
TrimError=Invalid argument ''{0}'': the length of the string describing the trimmed character must be 1
InvalidTypesForArithmetic=Invalid types for arithmetic: {0} {1} {2}
InvalidTypesForComparison=Invalid types for comparison: {0} {1} {2}
CannotConvert=Cannot convert {0} to {1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ protected String buildQuery2(SqlTestFactory factory, String expression) {
&& isNull(call.operand(0))) {
literalSet.add(call);
return call;
} else if (operator == SqlStdOperatorTable.TRIM) {
// see https://issues.apache.org/jira/projects/CALCITE/issues/CALCITE-6780
// don't extract trimmed literal for TRIM function
call.operand(2).accept(this);
return call;
} else if (ops.contains(operator)) {
// "Argument to function 'LOCALTIME' must be a
// literal"
Expand Down
34 changes: 19 additions & 15 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11706,31 +11706,35 @@ void assertSubFunReturns(boolean binary, String s, int start,
// SQL:2003 6.29.11 Trimming a CHAR yields a VARCHAR
f.checkString("trim('a' from 'aAa')", "A", "VARCHAR(3) NOT NULL");
f.checkString("trim(both 'a' from 'aAa')", "A", "VARCHAR(3) NOT NULL");
f.checkString("trim(' aAa ')", "aAa", "VARCHAR(5) NOT NULL");
f.checkString("trim(both ' ' from ' aAa ')", "aAa", "VARCHAR(5) NOT NULL");
f.checkString("trim(leading 'a' from 'aAa')", "Aa", "VARCHAR(3) NOT NULL");
f.checkString("trim(trailing 'a' from 'aAa')", "aA", "VARCHAR(3) NOT NULL");
f.checkNull("trim(null)");
f.checkNull("trim(cast(null as varchar(1)) from 'a')");
f.checkNull("trim('a' from cast(null as varchar(1)))");

// SQL:2003 6.29.9: trim string must have length=1. Failure occurs
// at runtime.
//
// TODO: Change message to "Invalid argument\(s\) for
// 'TRIM' function".
// The message should come from a resource file, and should still
// have the SQL error code 22027.
// SQL:2003 6.29.9 and SQL:2016 6.30.11: trim string must have length=1.
// Failure occurs at runtime.
f.checkFails("trim('xy' from 'abcde')",
"Trim error: trim character must be exactly 1 character",
"Invalid argument 'xy': the length of the string describing "
+ "the trimmed character must be 1",
true);
f.checkFails("trim('' from 'abcde')",
"Trim error: trim character must be exactly 1 character",
"Invalid argument '': the length of the string describing "
+ "the trimmed character must be 1",
true);

final SqlOperatorFixture f1 = f.withConformance(SqlConformanceEnum.MYSQL_5);
f1.checkString("trim(leading 'eh' from 'hehe__hehe')", "__hehe",
"VARCHAR(10) NOT NULL");
f1.checkString("trim(trailing 'eh' from 'hehe__hehe')", "hehe__",
"VARCHAR(10) NOT NULL");
f1.checkString("trim('eh' from 'hehe__hehe')", "__", "VARCHAR(10) NOT NULL");
final Consumer<SqlOperatorFixture> consumer = f1 -> {
f1.checkString("trim(leading 'eh' from 'hehe__hehe')", "__hehe",
"VARCHAR(10) NOT NULL");
f1.checkString("trim(trailing 'eh' from 'hehe__hehe')", "hehe__",
"VARCHAR(10) NOT NULL");
f1.checkString("trim('eh' from 'hehe__hehe')", "__", "VARCHAR(10) NOT NULL");
};
final List<SqlConformanceEnum> conformanceEnums =
list(SqlConformanceEnum.MYSQL_5, SqlConformanceEnum.SQL_SERVER_2008);
f.forEachConformance(conformanceEnums, consumer);
}

@Test void testRtrimFunc() {
Expand Down

0 comments on commit b852db2

Please sign in to comment.