← Back to Pull requests

feat(spark): add Spark-compatible struct() field #425

Open
unknown opened this pull request with 542 lines diff across 6 files
View on GitHub
unknown unknown commented on Nov 26, 2026 22:15 UTC
Author

Which issue does this PR close?

  • Closes #364.
  • Rationale for this change

    DataFusion's struct(...) names its fields c0, c1, ..., while Spark names them after the argument when it is a column, an alias or a struct field, and col1, col2, ... otherwise. With the Spark functions registered:

  • SELECT struct(1, 2, 3) returned {c0: 1, c1: 2, c2: 3} where Spark returns {col1: 1, col2: 2, col3: 3}.

  • SELECT struct(a, b + 1) FROM t returned {c0, c1} where Spark returns {a, col2}.

  • SELECT struct(1 AS a, 2) returned {a, c1} where Spark returns {a, col2}.
  • This shows up in the output of any query that builds a struct, such as map_from_arrays and map_from_entries with struct values.

    What changes are included in this PR?

  • SparkFunctionPlanner::plan_struct_literal now plans SQL struct(...). DataFusion plans struct(...) through the ExprPlanner and never looks up a registered struct function, so the planner is the place to do it. It names each field as CreateStruct.apply does in Spark and plans the call to named_struct:

  • - a column keeps its name (the last part, for a.b.c),
    - an alias keeps the alias,
    - get_field(s, 'x') (from struct(s.x)) keeps x,
    - anything else is col{i + 1}, where i is its position in the full argument list.
  • Mixed named and unnamed arguments, such as struct(1 AS a, 2), are handled in the same place. DataFusion names an unnamed entry c{i}, and the planner replaces that with the Spark name. A made-up c{i} can't be told apart from an explicit value AS c{i} at the same position, so that rare alias is renamed too (see apache/datafusion#27723).

  • Add a Spark-compatible struct function (SparkStruct), registered with the other Spark functions and exported as expr_fn::r#struct. It names fields col1, col2, ..., and struct() returns an empty struct, as in Spark. Calls that do not come from SQL struct syntax use it.

  • Fields are always nullable, with a comment saying why. Spark makes a field nullable only if its argument is, but propagating that makes array[struct(1, 'a'), struct(2, NULL)] panic in DataFusion's make_array, because the two structs become different types. DataFusion's own struct and named_struct make every field nullable for this reason.

  • Add the #[user_doc] and regenerate docs/functions (new scalar/struct.md page and README entries).
  • Are these changes tested?

    Yes.

  • struct/struct.slt: cases for int and mixed-type fields, NULL, nested structs, columns, a + 1, a qualified column, a struct field access, aliased and mixed arguments, duplicate names, array and map arguments, and the nullable-array regression.

  • map/map_from_arrays.slt: removes the xfail spark marker that referenced this issue and updates the expected value.

  • map/map_from_entries.slt: the expected value of the nested struct(1, 2, 3) now uses col1..col3.

  • Unit tests in function/struct/spark_struct.rs (field naming, empty struct, nullable fields, array and scalar arguments) and planner.rs (the naming rule, the c{i} detection, the empty-argument branch).
  • The expected results were checked against Spark 4.2.0 with the runner in crates/sqllogictest/scripts over struct.slt, map_from_arrays.slt and map_from_entries.slt: no differences. The runner compares values only, so I also checked the schema on 4.2.0 directly: struct() is struct<>, struct(1 AS a, 2) is struct<a:int,col2:int>, and duplicate names are allowed.

    SELECT struct() cannot be tested in SQL because the parser rejects it (apache/datafusion-sqlparser-rs#3048), so the empty struct is only covered by unit tests.

    cargo fmt --all -- --check, cargo clippy --all-targets --all-features -- -D warnings, cargo test -p datafusion-spark and cargo test -p datafusion-spark-sqllogictest all pass.

    Are there any user-facing changes?

    Yes. struct(...) now returns Spark's field names instead of DataFusion's c0, c1, ... when the Spark functions are registered: the column name for a column, alias or struct field argument, and col<N> otherwise. Queries that referenced the old names, for example get_field(s, 'c0'), need updating. It also adds struct to the function reference docs and a new public expr_fn::r#struct. There is no breaking change to the existing public Rust API.

    Diff: 542 lines (+406 / -136) across 6 files Reviewer: andygrove Review duration: 7014m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 542 lines diff across 6 files
    0.48
    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.377
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #425"
    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: 48.0% (MEDIUM) · Higher = larger blast radius
    48.0%
    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: 25.6% · Safe to merge
    25.6%