TechWizard9999 opened a new issue, #431:
URL: https://github.com/apache/datafusion-spark/issues/431

   ### Describe the bug
   
   `format_string` panics with `internal error: entered unreachable code` when 
the format argument is a column of type NULL, for example a `VALUES` column 
that only holds NULL. Spark casts such a column to STRING and returns NULL for 
every row.
   
   `return_field_from_args` accepts `DataType::Null` 
([format_string.rs#L108](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/spark/src/function/string/format_string.rs#L108))
 and the scalar path returns NULL for `ScalarValue::Null` 
([#L134](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/spark/src/function/string/format_string.rs#L134)).
 The array path has no case for it: `try_as_str()` returns `None` and reaches 
`unreachable!()` 
([#L202](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/spark/src/function/string/format_string.rs#L202)).
 The match on the result type after the loop has no NULL arm either 
([#L209](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/spark/src/function/string/format_string.rs#L209)).
   
   ### To Reproduce
   
   ```sql
   SELECT format_string(f, a) FROM (VALUES (NULL, CAST(1 AS INT)), (NULL, 
CAST(2 AS INT))) AS t(f, a);
   ```
   
   datafusion-spark panics:
   
   ```
   panicked at crates\spark\src\function\string\format_string.rs:202:30:
   internal error: entered unreachable code
   ```
   
   A NULL literal format (`format_string(NULL, CAST(1 AS INT))`) and a NULL 
STRING column both return NULL, so only a NULL-typed column fails. The `printf` 
alias panics the same way.
   
   <details>
   <summary>sqllogictest file with Spark's results (fails today)</summary>
   
   Save it as 
`crates/sqllogictest/test_files/string/format_string_null_format.slt` and run 
`cargo test -p datafusion-spark-sqllogictest -- format_string_null_format`. 
Today both modes fail with the panic.
   
   ```
   query T
   SELECT format_string(f, a) FROM (VALUES (NULL, CAST(1 AS INT)), (NULL, 
CAST(2 AS INT))) AS t(f, a);
   ----
   NULL
   NULL
   ```
   
   </details>
   
   ### Expected behavior
   
   As in Spark:
   
   ```
   NULL
   NULL
   ```
   
   ### Spark versions
   
   Spark 4.2.0, from the source; not run on a live Spark. `FormatString` is 
`ImplicitCastInputTypes` and declares STRING for the format 
([stringExpressions.scala#L2066-L2068](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala#L2066-L2068)).
 A NULL-typed argument is cast to the expected type 
([TypeCoercion.scala#L202](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/TypeCoercion.scala#L202);
 with ANSI mode on, 
[Cast.scala#L389](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L389)),
 and a NULL format gives NULL 
([stringExpressions.scala#L2080-L2083](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala#L2080-L2083)).
 Spark accepts a NULL-only `VALUES` column, as in its own test `s
 ql-tests/inputs/group-by.sql`. 3.5.8, 4.0.4 and 4.1.3 have the same logic.
   
   ### datafusion-spark version
   
   main at ab9d308
   
   ### Additional context
   
   Reproduced on main ab9d308 with the sqllogictest runner in simplify and 
evaluate mode, and with `datafusion-spark-cli` with ANSI mode on and off. 
`unreachable!()` panics in release builds too.
   
   cc @andygrove
   


-- 
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