← Back to Pull requests

fix: reject null-typed columns in stack #402

Open
unknown opened this pull request with 467 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 22, 2026 19:49 UTC
Author

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.

    Diff: 467 lines (+350 / -117) across 5 files Reviewer: andygrove Review duration: 11309m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 467 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #402"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.6% (MEDIUM) · Higher = larger blast radius
    45.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.3% · Safe to merge
    24.3%