Subodh-17 opened a new pull request, #402:
URL: https://github.com/apache/datafusion-spark/pull/402

   ## Which issue does this PR close?
   
   Closes #361.
   
   ## Rationale for this change
   
   In Apache Spark, `stack(n, expr1, ..., exprk)` permits literal `NULL` values 
in a column by coercing them to the data type of that column (via 
`StackCoercion`). However, when a table column itself has `NullType` (Spark 
type `"VOID"`), Spark does not coerce the column and instead fails validation 
during query analysis with:
   ```
   [DATATYPE_MISMATCH.STACK_COLUMN_DIFF_TYPES] The data type of the column 
(<col>) do not have the same type: "<expected>" (<idx>) <> "<actual>" (<idx>). 
SQLSTATE: 42K09
   ```
   
   Prior to this PR, `datafusion-spark` checked `!actual.is_null()` when 
comparing argument types in `column_types()`. This inadvertently bypassed all 
`DataType::Null` arguments regardless of whether they were literal NULL 
expressions or NULL-typed table columns, causing queries like `stack(2, a, b)` 
with `b` of type `Null` to incorrectly succeed instead of raising 
`DATATYPE_MISMATCH.STACK_COLUMN_DIFF_TYPES`.
   
   ## What changes are included in this PR?
   
   1. **Distinguish literal NULL from NULL-typed columns**: In 
`crates/spark/src/function/generator/stack.rs:column_types`, differentiate 
between literal NULLs (which have 
`scalar_arguments.get(i).copied().flatten().is_some()`) and table columns of 
`DataType::Null`.
   2. **Order-independent column type resolution**:
      - For each column, if the initial entry is a literal NULL, resolve the 
target column type as the first non-null-literal expression in that column (or 
`DataType::Null` if all entries are literal NULLs). If the initial entry is a 
column (typed or NULL-typed), use its data type directly.
      - Validate all non-null-literal arguments in the column against this 
expected type.
   3. **Spark-compatible error diagnostics**:
      - Use error class `[DATATYPE_MISMATCH.STACK_COLUMN_DIFF_TYPES]`.
      - Format expected and actual types using `spark_sql_type` (e.g. `"VOID"`, 
`"INT"`, `"BIGINT"`, `"SMALLINT"`, `"STRING"`).
      - Use 1-based indices for the parameter positions matching Spark Catalyst 
error messages.
   4. **SLT & Unit Test Coverage**:
      - In `crates/spark/src/function/generator/stack.rs`:
        - Added `test_column_types_rejects_null_typed_column` verifying 
rejection of NULL-typed columns in both forward and reverse orders.
        - Added `test_column_types_literal_null_coercion` testing literal NULL 
before/after typed columns, all-literal NULL columns, and all-NULL-typed table 
columns.
        - Updated `test_column_types_widen_only_bigint_literals` to verify new 
error class and SQL type format.
      - In `crates/sqllogictest/test_files/generator/stack.slt`:
        - Removed `# xfail spark` on line 54.
        - Added positive test cases for literal NULL before and after columns 
(`stack(2, NULL, a)`, `stack(2, a, NULL)`).
        - Added negative test cases verifying 
`[DATATYPE_MISMATCH.STACK_COLUMN_DIFF_TYPES]` for column paired with NULL-typed 
column in forward, reverse, and multi-column positions (`stack(2, a, b)`, 
`stack(2, b, a)`, `stack(2, 1, a, 2, b)`).
        - Updated existing mismatch error expectations to match Spark error 
class and types.
   
   ## Are these changes tested?
   
   Yes:
   - Unit tests: `cargo test -p datafusion-spark --lib generator::stack::tests` 
(8 passed, 0 failed).
   - Sqllogictests: `cargo test -p datafusion-spark-sqllogictest --test 
sqllogictests -- stack` (PASS [simplify], PASS [evaluate]).
   - Linter and style: `cargo fmt --all -- --check` and `cargo clippy -p 
datafusion-spark --all-targets -- -D warnings`.
   
   ## Are there any user-facing changes?
   
   No public API changes. Queries using `stack()` with table columns of `Null` 
type now correctly fail query analysis with Spark-compatible 
`[DATATYPE_MISMATCH.STACK_COLUMN_DIFF_TYPES]`, matching Apache Spark behavior.
   


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