Skip to content

Commit

Permalink
[CALCITE-5921] SqlOperatorFixture.checkFails and checkAggFails don't …
Browse files Browse the repository at this point in the history
…check runtime failure

Signed-off-by: Mihai Budiu <mbudiu@gmail.com>
  • Loading branch information
mihaibudiu authored and rubenada committed Oct 25, 2023
1 parent 425a94d commit 0bec957
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ ExInst<CalciteException> invalidCompare(String a0, String a1, String a2,
@BaseMessage("Date literal ''{0}'' out of range")
ExInst<SqlValidatorException> dateLiteralOutOfRange(String a0);

@BaseMessage("Input arguments of {0} out of range: {1,number,#}; should be in the range of {2}")
@BaseMessage("Input arguments of {0} out of range: {1,number,#.#}; should be in the range of {2}")
ExInst<CalciteException> inputArgumentsOfFunctionOutOfRange(String a0, Number a1, String a2);

@BaseMessage("String literal continued on same line")
Expand Down
25 changes: 15 additions & 10 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -860,17 +860,22 @@ public static boolean containsSubstr(String jsonString, String substr,
assert map != null;
Set<String> keys = map.keySet();
Collection<String> values = map.values();
switch (JsonScope.valueOf(jsonScope)) {
case JSON_KEYS:
return keys.contains(substr);
case JSON_KEYS_AND_VALUES:
return keys.contains(substr) || values.contains(substr);
case JSON_VALUES:
return values.contains(substr);
default:
throw new IllegalArgumentException("json_scope argument must be one of: \"JSON_KEYS\", "
+ "\"JSON_VALUES\", \"JSON_KEYS_AND_VALUES\".");
try {
switch (JsonScope.valueOf(jsonScope)) {
case JSON_KEYS:
return keys.contains(substr);
case JSON_KEYS_AND_VALUES:
return keys.contains(substr) || values.contains(substr);
case JSON_VALUES:
return values.contains(substr);
default:
break;
}
} catch (IllegalArgumentException ignored) {
// Happens when jsonScope is not one of the legal enum values
}
throw new IllegalArgumentException("json_scope argument must be one of: \"JSON_KEYS\", "
+ "\"JSON_VALUES\", \"JSON_KEYS_AND_VALUES\".");
}

/** SQL <code>CONTAINS_SUBSTR(expr, substr)</code> operator. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ OperandNotComparable=Operands {0} not comparable to each other
TypeNotComparableEachOther=Types {0} not comparable to each other
NumberLiteralOutOfRange=Numeric literal ''{0}'' out of range
DateLiteralOutOfRange=Date literal ''{0}'' out of range
InputArgumentsOfFunctionOutOfRange=Input arguments of {0} out of range: {1,number,#}; should be in the range of {2}
InputArgumentsOfFunctionOutOfRange=Input arguments of {0} out of range: {1,number,#.#}; should be in the range of {2}
StringFragsOnSameLine=String literal continued on same line
AliasMustBeSimpleIdentifier=Table or column alias must be a simple identifier
CharLiteralAliasNotValid=Expecting alias, found character literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface SqlOperatorFixture extends AutoCloseable {
// Error messages when an invalid time unit is given as
// input to extract for a particular input type.
String INVALID_EXTRACT_UNIT_CONVERTLET_ERROR =
"Extract.*from.*type data is not supported";
"Was not expecting value '.*' for enumeration.*";

String INVALID_EXTRACT_UNIT_VALIDATION_ERROR =
"Cannot apply 'EXTRACT' to arguments of type .*'\n.*";
Expand Down
12 changes: 8 additions & 4 deletions testkit/src/main/java/org/apache/calcite/sql/test/SqlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ public static void checkEx(@Nullable Throwable ex,
int actualEndLine = 100;
int actualEndColumn = 99;

if (ex instanceof ExceptionInInitializerError) {
ex = ((ExceptionInInitializerError) ex).getException();
}

// Search for an CalciteContextException somewhere in the stack.
CalciteContextException ece = null;
for (Throwable x = ex; x != null; x = x.getCause()) {
Expand Down Expand Up @@ -294,18 +298,18 @@ public static void checkEx(@Nullable Throwable ex,
actualMessage = actualException.getMessage();
}
} else {
final String message = ex.getMessage();
if (message != null) {
actualMessage = ex.getMessage();
if (actualMessage != null) {
java.util.regex.Matcher matcher =
LINE_COL_TWICE_PATTERN.matcher(message);
LINE_COL_TWICE_PATTERN.matcher(actualMessage);
if (matcher.matches()) {
actualLine = Integer.parseInt(matcher.group(1));
actualColumn = Integer.parseInt(matcher.group(2));
actualEndLine = Integer.parseInt(matcher.group(3));
actualEndColumn = Integer.parseInt(matcher.group(4));
actualMessage = matcher.group(5);
} else {
matcher = LINE_COL_PATTERN.matcher(message);
matcher = LINE_COL_PATTERN.matcher(actualMessage);
if (matcher.matches()) {
actualLine = Integer.parseInt(matcher.group(1));
actualColumn = Integer.parseInt(matcher.group(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ void forEachQueryValidateAndThen(StringAndPos expression,
SqlValidator validator = factory.createValidator();
SqlNode n = parseAndValidate(validator, sql);
assertNotNull(n);
tester.checkFails(factory, sap, expectedError, runtime);
} else {
checkQueryFails(StringAndPos.of(sql),
expectedError);
Expand All @@ -185,6 +186,7 @@ void forEachQueryValidateAndThen(StringAndPos expression,
SqlValidator validator = factory.createValidator();
SqlNode n = parseAndValidate(validator, sql);
assertNotNull(n);
tester.checkAggFails(factory, expr, inputValues, expectedError, runtime);
} else {
checkQueryFails(StringAndPos.of(sql), expectedError);
}
Expand Down
34 changes: 17 additions & 17 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1438,10 +1438,10 @@ void testCastToBoolean(CastType castType, SqlOperatorFixture f) {
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4861">[CALCITE-4861]
* Optimization of chained CAST calls leads to unexpected behavior</a>. */
@Test void testChainedCast() {
@Test @Disabled("CALCITE-5990") void testChainedCast() {
final SqlOperatorFixture f = fixture();
f.checkFails("CAST(CAST(CAST(123456 AS TINYINT) AS INT) AS BIGINT)",
"Value out of range. Value:\"123456\"", true);
".*Value 123456 out of range", true);
}

@Test void testCase() {
Expand Down Expand Up @@ -1866,10 +1866,10 @@ void testCastToBoolean(CastType castType, SqlOperatorFixture f) {

f.checkFails("code_points_to_bytes(array[-1])",
"Input arguments of CODE_POINTS_TO_BYTES out of range: -1;"
+ " should be in the range of [0, 255]", true);
+ " should be in the range of \\[0, 255\\]", true);
f.checkFails("code_points_to_bytes(array[2147483648, 1])",
"Input arguments of CODE_POINTS_TO_BYTES out of range: 2147483648;"
+ " should be in the range of [0, 255]", true);
+ " should be in the range of \\[0, 255\\]", true);

f.checkString("code_points_to_bytes(array[65, 66, 67, 68])", "41424344", "VARBINARY NOT NULL");
f.checkString("code_points_to_bytes(array[255, 254, 65, 64])", "fffe4140",
Expand Down Expand Up @@ -1899,10 +1899,10 @@ void testCastToBoolean(CastType castType, SqlOperatorFixture f) {

f.checkFails("code_points_to_string(array[-1])",
"Input arguments of CODE_POINTS_TO_STRING out of range: -1;"
+ " should be in the range of [0, 0xD7FF] and [0xE000, 0x10FFFF]", true);
+ " should be in the range of \\[0, 0xD7FF\\] and \\[0xE000, 0x10FFFF\\]", true);
f.checkFails("code_points_to_string(array[2147483648, 1])",
"Input arguments of CODE_POINTS_TO_STRING out of range: 2147483648;"
+ " should be in the range of [0, 0xD7FF] and [0xE000, 0x10FFFF]", true);
+ " should be in the range of \\[0, 0xD7FF\\] and \\[0xE000, 0x10FFFF\\]", true);

f.checkString("code_points_to_string(array[65, 66, 67, 68])", "ABCD",
"VARCHAR NOT NULL");
Expand Down Expand Up @@ -3808,18 +3808,18 @@ static void checkRlikeFails(SqlOperatorFixture f) {

// some negative tests
f.checkFails("'yd' similar to '[x-ze-a]d'",
"Illegal character range near index 6\n"
".*Illegal character range near index 6\n"
+ "\\[x-ze-a\\]d\n"
+ " \\^",
true); // illegal range

// Slightly different error message from JDK 13 onwards
final String expectedError =
TestUtil.getJavaMajorVersion() >= 13
? "Illegal repetition near index 22\n"
? ".*Illegal repetition near index 22\n"
+ "\\[\\:LOWER\\:\\]\\{2\\}\\[\\:DIGIT\\:\\]\\{,5\\}\n"
+ " \\^"
: "Illegal repetition near index 20\n"
: ".*Illegal repetition near index 20\n"
+ "\\[\\:LOWER\\:\\]\\{2\\}\\[\\:DIGIT\\:\\]\\{,5\\}\n"
+ " \\^";
f.checkFails("'yd3223' similar to '[:LOWER:]{2}[:DIGIT:]{,5}'",
Expand Down Expand Up @@ -7229,13 +7229,13 @@ private static void checkIf(SqlOperatorFixture f) {
f.checkNull("atanh(cast(null as integer))");
f.checkNull("atanh(cast(null as double))");
f.checkFails("atanh(1)",
"Input arguments of ATANH out of range: 1; should be in the range of (-1, 1)",
"Input arguments of ATANH out of range: 1; should be in the range of \\(-1, 1\\)",
true);
f.checkFails("atanh(-1)",
"Input arguments of ATANH out of range: -1; should be in the range of (-1, 1)",
"Input arguments of ATANH out of range: -1; should be in the range of \\(-1, 1\\)",
true);
f.checkFails("atanh(-1.5)",
"Input arguments of ATANH out of range: -1.5; should be in the range of (-1, 1)",
"Input arguments of ATANH out of range: -1.5; should be in the range of \\(-1, 1\\)",
true);
};
f0.forEachLibrary(list(SqlLibrary.ALL), consumer);
Expand Down Expand Up @@ -8497,7 +8497,7 @@ private void testCurrentDateFunc(Pair<String, Hook.Closeable> pair) {
f.checkFails("lpad('12345', -3)",
"Second argument for LPAD/RPAD must not be negative", true);
f.checkFails("lpad('12345', 3, '')",
"Third argument (pad pattern) for LPAD/RPAD must not be empty", true);
"Third argument \\(pad pattern\\) for LPAD/RPAD must not be empty", true);
f.checkString("lpad(x'aa', 4, x'bb')", "bbbbbbaa", "VARBINARY NOT NULL");
f.checkString("lpad(x'aa', 4)", "202020aa", "VARBINARY NOT NULL");
f.checkString("lpad(x'aaaaaa', 2)", "aaaa", "VARBINARY NOT NULL");
Expand All @@ -8507,7 +8507,7 @@ private void testCurrentDateFunc(Pair<String, Hook.Closeable> pair) {
f.checkFails("lpad(x'aa', -3)",
"Second argument for LPAD/RPAD must not be negative", true);
f.checkFails("lpad(x'aa', 3, x'')",
"Third argument (pad pattern) for LPAD/RPAD must not be empty", true);
"Third argument \\(pad pattern\\) for LPAD/RPAD must not be empty", true);
}

@Test void testRpadFunction() {
Expand All @@ -8522,7 +8522,7 @@ private void testCurrentDateFunc(Pair<String, Hook.Closeable> pair) {
f.checkFails("rpad('12345', -3)",
"Second argument for LPAD/RPAD must not be negative", true);
f.checkFails("rpad('12345', 3, '')",
"Third argument (pad pattern) for LPAD/RPAD must not be empty", true);
"Third argument \\(pad pattern\\) for LPAD/RPAD must not be empty", true);

f.checkString("rpad(x'aa', 4, x'bb')", "aabbbbbb", "VARBINARY NOT NULL");
f.checkString("rpad(x'aa', 4)", "aa202020", "VARBINARY NOT NULL");
Expand All @@ -8533,7 +8533,7 @@ private void testCurrentDateFunc(Pair<String, Hook.Closeable> pair) {
f.checkFails("rpad(x'aa', -3)",
"Second argument for LPAD/RPAD must not be negative", true);
f.checkFails("rpad(x'aa', 3, x'')",
"Third argument (pad pattern) for LPAD/RPAD must not be empty", true);
"Third argument \\(pad pattern\\) for LPAD/RPAD must not be empty", true);
}

@Test void testContainsSubstrFunc() {
Expand Down Expand Up @@ -9110,7 +9110,7 @@ void assertSubFunReturns(boolean binary, String s, int start,

// test with illegal argument
f.checkFails("format_number(12332.123456, -1)",
"Illegal arguments for 'FORMAT_NUMBER' function:"
"Illegal arguments for FORMAT_NUMBER function:"
+ " negative decimal value not allowed",
true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class SqlRuntimeTester extends AbstractSqlTester {

@Override public void checkFails(SqlTestFactory factory, StringAndPos sap,
String expectedError, boolean runtime) {
final StringAndPos sap2 =
StringAndPos.of(runtime ? buildQuery2(factory, sap.addCarets())
: buildQuery(sap.addCarets()));
final StringAndPos sap2 = StringAndPos.of(buildQuery(sap.addCarets()));
assertExceptionIsThrown(factory, sap2, expectedError, runtime);
}

Expand Down

0 comments on commit 0bec957

Please sign in to comment.