feat(spark): add Spark-compatible struct() field #425
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
andygrove
Which issue does this PR close?
Rationale for this change
DataFusion's
struct(...)names its fieldsc0,c1, ..., while Spark names them after the argument when it is a column, an alias or a struct field, andcol1,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 treturned{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_arraysandmap_from_entrieswith struct values.What changes are included in this PR?
SparkFunctionPlanner::plan_struct_literalnow plans SQLstruct(...). DataFusion plansstruct(...)through theExprPlannerand never looks up a registeredstructfunction, so the planner is the place to do it. It names each field asCreateStruct.applydoes in Spark and plans the call tonamed_struct:- a column keeps its name (the last part, for
a.b.c),- an alias keeps the alias,
-
get_field(s, 'x')(fromstruct(s.x)) keepsx,- anything else is
col{i + 1}, whereiis its position in the full argument list.struct(1 AS a, 2), are handled in the same place. DataFusion names an unnamed entryc{i}, and the planner replaces that with the Spark name. A made-upc{i}can't be told apart from an explicitvalue AS c{i}at the same position, so that rare alias is renamed too (see apache/datafusion#27723).structfunction (SparkStruct), registered with the other Spark functions and exported asexpr_fn::r#struct. It names fieldscol1,col2, ..., andstruct()returns an empty struct, as in Spark. Calls that do not come from SQL struct syntax use it.array[struct(1, 'a'), struct(2, NULL)]panic in DataFusion'smake_array, because the two structs become different types. DataFusion's ownstructandnamed_structmake every field nullable for this reason.#[user_doc]and regeneratedocs/functions(newscalar/struct.mdpage 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 thexfail sparkmarker that referenced this issue and updates the expected value.map/map_from_entries.slt: the expected value of the nestedstruct(1, 2, 3)now usescol1..col3.function/struct/spark_struct.rs(field naming, empty struct, nullable fields, array and scalar arguments) andplanner.rs(the naming rule, thec{i}detection, the empty-argument branch).The expected results were checked against Spark 4.2.0 with the runner in
crates/sqllogictest/scriptsoverstruct.slt,map_from_arrays.sltandmap_from_entries.slt: no differences. The runner compares values only, so I also checked the schema on 4.2.0 directly:struct()isstruct<>,struct(1 AS a, 2)isstruct<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-sparkandcargo test -p datafusion-spark-sqllogictestall pass.Are there any user-facing changes?
Yes.
struct(...)now returns Spark's field names instead of DataFusion'sc0,c1, ... when the Spark functions are registered: the column name for a column, alias or struct field argument, andcol<N>otherwise. Queries that referenced the old names, for exampleget_field(s, 'c0'), need updating. It also addsstructto the function reference docs and a new publicexpr_fn::r#struct. There is no breaking change to the existing public Rust API.