Skip to content

Commit

Permalink
[CALCITE-5708] SUBSTRING validation error if any parameter is a NULL …
Browse files Browse the repository at this point in the history
…literal

Close apache#3211
  • Loading branch information
zstan authored and tanclary committed Jul 5, 2023
1 parent f1d0316 commit 1b11d99
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
* Definition of the "SUBSTRING" builtin SQL function.
*/
public class SqlSubstringFunction extends SqlFunction {
/** Type checker for 3 argument calls. Put the STRING_NUMERIC_NUMERIC checker
/** Type checker for 3 argument calls. Put the STRING_INTEGER_INTEGER checker
* first because almost every other type can be coerced to STRING. */
private static final SqlSingleOperandTypeChecker CHECKER3 =
OperandTypes.STRING_NUMERIC_NUMERIC
OperandTypes.STRING_INTEGER_INTEGER
.or(OperandTypes.STRING_STRING_STRING);

//~ Constructors -----------------------------------------------------------
Expand All @@ -74,7 +74,8 @@ public class SqlSubstringFunction extends SqlFunction {
case 3:
return "{0}({1} FROM {2} FOR {3})";
default:
throw new AssertionError();
throw new AssertionError("Incorrect " + getName() + " signature, operands "
+ "count = " + operandsCount);
}
}

Expand Down Expand Up @@ -121,7 +122,7 @@ public class SqlSubstringFunction extends SqlFunction {
return false;
}
}
if (!SqlTypeUtil.inSameFamily(t1, t2)) {
if (!SqlTypeUtil.inSameFamilyOrNull(t1, t2)) {
if (throwOnFailure) {
throw callBinding.newValidationSignatureError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,12 @@ void testDyadicCollateOperator() {
.columnType("VARCHAR(1) NOT NULL");
expr("substring('a', 1, '3')")
.columnType("VARCHAR(1) NOT NULL");

// Correctly processed null string and params.
expr("SUBSTRING(NULL FROM 1 FOR 2)").ok();
expr("SUBSTRING('text' FROM 1 FOR NULL)").ok();
expr("SUBSTRING('text' FROM NULL FOR 2)").ok();
expr("SUBSTRING('text' FROM NULL)").ok();
}

@Test void testSubstringFails() {
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/resources/sql/functions.iq
Original file line number Diff line number Diff line change
Expand Up @@ -938,5 +938,20 @@ table(AUX.TBLFUN_IDENTITY(3)) as t3(v);

!ok

# SUBSTRING
-- returns 'null'
select SUBSTRING(NULL FROM 1 FOR 2);
select SUBSTRING('text' FROM 1 FOR NULL);
select SUBSTRING('text' FROM NULL FOR 2);
select SUBSTRING(s FROM i FOR l) FROM (VALUES ('abc', NULL, 2)) AS t (s, i, l);
select SUBSTRING('text' FROM NULL);
+--------+
| EXPR$0 |
+--------+
| |
+--------+
(1 row)

!ok

# End functions.iq

0 comments on commit 1b11d99

Please sign in to comment.