mbutrovich opened a new issue, #27276:
URL: https://github.com/apache/datafusion/issues/27276

   **Describe the bug**
   
   `split_part(s, delimiter, 0)` on a row where `s` is NULL returns NULL when 
the position comes from a column, and errors with "field position must not be 
zero" when the position is a literal. The [fast 
path](https://github.com/apache/datafusion/blob/14b3edd9a350b983fae6d2f194df1a3ae343ba15/datafusion/functions/src/string/split_part.rs#L97-L105)
 for an array string with a scalar delimiter and position [rejects position 
0](https://github.com/apache/datafusion/blob/14b3edd9a350b983fae6d2f194df1a3ae343ba15/datafusion/functions/src/string/split_part.rs#L269-L271)
 before it looks at any row. The array path [skips rows where any argument is 
NULL](https://github.com/apache/datafusion/blob/14b3edd9a350b983fae6d2f194df1a3ae343ba15/datafusion/functions/src/string/split_part.rs#L530-L541),
 so it errors only on a row whose three arguments are all non-NULL. The two 
paths disagree whenever no row has a non-NULL string, such as a batch whose 
strings are all NULL.
   
   **To Reproduce**
   
   ```sql
   -- Literal delimiter and position: the fast path
   SELECT split_part(s, '.', 0) AS r FROM (VALUES (NULL::text)) AS t(s);
   -- Execution error: field position must not be zero
   
   -- The same row with the position from a column: the array path
   SELECT split_part(s, '.', p) AS r FROM (VALUES (NULL::text, 0)) AS t(s, p);
   -- NULL
   
   -- Only literals
   SELECT split_part(NULL::text, '.', 0) AS r;
   -- NULL
   ```
   
   **Expected behavior**
   
   All three return NULL. PostgreSQL's `split_part` is strict: its [`pg_proc` 
entry](https://github.com/postgres/postgres/blob/884aca9b96989b9bc1b65d42e731e90f098ee2ee/src/include/catalog/pg_proc.dat#L3887-L3889)
 leaves 
[`proisstrict`](https://github.com/postgres/postgres/blob/884aca9b96989b9bc1b65d42e731e90f098ee2ee/src/include/catalog/pg_proc.h#L70)
 at its default of true, so a NULL argument returns NULL without calling the 
function.
   
   
[`expr.slt`](https://github.com/apache/datafusion/blob/14b3edd9a350b983fae6d2f194df1a3ae343ba15/datafusion/sqllogictest/test_files/expr.slt#L714-L716)
 expects the first query's error ("Position 0 with column input errors even for 
empty/null inputs"), while the review of #21238 asked for the fast path to 
match the array path 
([discussion_r3030106101](https://github.com/apache/datafusion/pull/21238#discussion_r3030106101)).
 Which behavior is intended? If it is NULL, the fast path could return all 
NULLs when the string array has no valid row before checking the position, and 
the `expr.slt` case would expect NULL.
   
   **Additional context**
   
   Property-based tests found this on `main` at 14b3edd9a3: calling 
`split_part` with an array string and scalar delimiter and position gave an 
error, and the same call with every argument as an array gave NULL. The 
reproduction runs on `main` at 14b3edd9a3. The fast path came with #21238.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to