IS NULL doesn't type-check its input
Describe the bug
The input expression to IS NULL isn't type-checked, unless for other constructs (IS TRUE, binary operators, etc.)
To Reproduce
CREATE TABLE tt();
SELECT * FROM tt WHERE ((STARTS_WITH()) IS NULL);
Yields:
thread 'main' (41425691) panicked at datafusion/functions/src/string/starts_with.rs:168:54:
index out of bounds: the len is 0 but the index is 1
stack backtrace:
0: __rustc::rust_begin_unwind
at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/std/src/panicking.rs:698:5
1: core::panicking::panic_fmt
at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:80:14
2: core::panicking::panic_bounds_check
at /rustc/ded5c06cf21d2b93bffd5d884aa6e96934ee4234/library/core/src/panicking.rs:276:5
3: <usize as core::slice::index::SliceIndex<[T]>>::index
at /Users/neilconway/.rustup/toolchains/1.92.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/slice/index.rs:267:10
4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
at /Users/neilconway/.rustup/toolchains/1.92.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/slice/index.rs:18:15
5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
at /Users/neilconway/.rustup/toolchains/1.92.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3628:9
6: <datafusion_functions::string::starts_with::StartsWithFunc as datafusion_expr::udf::ScalarUDFImpl>::simplify
at ./datafusion/functions/src/string/starts_with.rs:168:54
Expected behavior
The expected behavior is that STARTS_WITH() should be rejected at planning time because of a type mismatch -- which is what happens for other variants like STARTS_WITH() IS TRUE.
Additional context
No response
I took a brief look at fixing this, but it was a bit out of my depth. Fixing the starts_with() implementation itself would be wrong because this case should be rejected at planning-time, as it is for IS TRUE and other variants. For IS TRUE, the type mismatch is detected during type coercion; I suppose we could add some type coercion for IS NULL and IS NOT NULL (e.g., by adding those cases to TreeNodeRewriter in type_coercion.rs), but I'd appreciate some comments on whether that makes sense or whether a different approach would be preferred.
I took a look at this and agree that fixing it in starts_with() would be wrong - the panic happens at starts_with.rs:168 when it tries to access args[1] on an empty vector, but this should've been caught earlier.
I searched for where to add type coercion for IS NULL similar to IS TRUE (as you mentioned with TreeNodeRewriter in type_coercion.rs), but I couldn't locate the exact spot. If you could point me to where IS TRUE does its type checking, I'm happy to implement the same pattern for IS NULL and IS NOT NULL!
We can probably fix this in type_coercion.rs to at least stop it panicking; then raise a separate issue (if one doesn't already exist) to try get these cases rejected at planning time instead of during type coercion pass, if possible. SELECT starts_with() gets rejected before type coercion is applied, but it seems the way we have things wired causes SELECT starts_with() IS TRUE to only be rejected during the type coercion pass.
Thanks to @Jefffrey , the problem is clear: IS TRUE and other similar operatiors requires its input a specific type, so they invoke coercer like BinaryTypeCoercer in type_coercion.rs. But IS NULL accepts any types so it do nothing to check type.
| Expr::Column(_)
| Expr::ScalarVariable(_, _)
| Expr::Literal(_, _)
| Expr::SimilarTo(_)
| Expr::IsNotNull(_)
| Expr::IsNull(_)
| Expr::Negative(_)
| Expr::Cast(_)
| Expr::TryCast(_)
| Expr::Wildcard { .. }
| Expr::GroupingSet(_)
| Expr::Placeholder(_)
| Expr::OuterReferenceColumn(_, _) => Ok(Transformed::no(expr)),
Similar problems occur with some of the operators above. For example:
> SELECT * FROM tt WHERE ((STARTS_WITH()) SIMILAR TO '123');
thread 'main' (96331) panicked at .../datafusion-functions-52.1.0/src/string/starts_with.rs:169:54:
index out of bounds: the len is 0 but the index is 1
I'd like to stop panicking in type_coercion.rs, and open another issue to discuss about how to reject the cases at planning.
Hi @Jefffrey @neilconway , I opened an issue #20356 to try to get these cases (and potential relevant cases) rejected at planning time instead of during type coercion pass.