Pastoray commented on PR #425:
URL: https://github.com/apache/datafusion-spark/pull/425#issuecomment-7307176821

   Thanks for the detailed review, and for pointing at `CreateStruct.apply`. 
You were right about the main issue: I had not seen the "Expected behavior" 
section of #364, so the first version named every field `col{n}`. I pushed a 
follow-up commit that fixes it. Answers to each point below.
    
   ## Field names for column arguments
    
   Fixed. `SparkFunctionPlanner::plan_struct_literal` now has the argument 
expressions, so it names each field the way `CreateStruct.apply` does and plans 
the call to `named_struct`:
    
   - an `Expr::Column` keeps its name (the last part, for `a.b.c`),
   - an alias keeps the alias,
   - `get_field(s, 'x')` (a `struct(s.x)` call) keeps `x`,
   - anything else is `col{i + 1}`, where `i` is the position in the full 
argument list.
   `SELECT struct(a, b + 1) FROM (VALUES (1, 2)) AS t(a, b)` now gives `{a: 1, 
col2: 3}`. `SparkStruct` stays as the registered `struct` function and the 
`expr_fn` entry point. It cannot see expressions, so a call that does not come 
from SQL struct syntax still names every field `col{n}`. The `#[user_doc]` 
describes the column rule, and the docs are regenerated.
    
   ## Mixed named and unnamed arguments
    
   Handled in the same place. DataFusion hands the planner `[name, value, ...]` 
pairs where an unnamed entry got the literal name `c{i}`, so the planner treats 
that name as "unnamed" and applies the rule above. `struct(1 AS a, 2)` now 
gives `{a, col2}`, and `struct(1, 2 AS b, 3)` gives `{col1, b, col3}`.
    
   One limitation: a made-up `c{i}` can't be told apart from an explicit `value 
AS c{i}` at the same position, so that (very unusual) alias is renamed too. It 
is documented in the code and covered by a unit test. The proper fix is on the 
DataFusion side, in apache/datafusion#27723 (preserve explicit versus generated 
struct field names for `ExprPlanner`).
    
   ## Zero arguments
    
   `SparkStruct` now supports it: `struct()` is an empty struct 
(`StructArray::try_new_with_length`, as you suggested). The planner also routes 
an empty argument list to it, because `named_struct` rejects zero arguments. 
This is covered by unit tests.
    
   I could not add an `.slt` case. The SQL parser rejects `SELECT struct()` 
(`Expected: an expression, found: )`) before it reaches any planner, which is 
the known apache/datafusion-sqlparser-rs#3048 ("Spark dialect rejects empty 
struct() expressions"). So the empty struct is only reachable through the 
function or expression API for now. Spark accepts it and returns `struct<>`, 
which I confirmed on 4.2.0.
    
   ## Field nullability
    
   I tried this and **kept `nullable = true`**, with a comment explaining why. 
With the argument's nullability propagated, `array[struct(1, 'a'), struct(2, 
CAST(NULL AS STRING))]` (the existing `map_from_entries.slt` test) panics in 
DataFusion's `make_array`:
    
   ```
   Arrays with inconsistent types passed to MutableArrayData
     left:  Struct([col1 Int64, col2 Utf8View nullable])
     right: Struct([col1 Int64, col2 Utf8View])
   ```
    
   The two structs differ only in the nullability of a field, so DataFusion 
treats them as different types, while Spark widens them. DataFusion's own 
`struct` and `named_struct` make every field nullable for this reason. I added 
an `.slt` regression case for exactly this array. Propagating Spark's 
nullability needs DataFusion to unify such struct types first, and the comment 
says to revisit it then. I can open a tracking issue if you want one.
    
   ## Tests
    
   - **PySpark 4.2.0.** I ran `pyspark_slt_runner.py` on `struct.slt`, 
`map_from_arrays.slt` and `map_from_entries.slt`: 36 pass, 6 are existing 
`xfail`s, 24 are skipped, and there are no differences. The skips are records 
Spark can't parse (the `array[...]` syntax), the `map_key_dedup_policy` 
records, and my `get_field` and `arrow_typeof` checks, which are 
DataFusion-only functions.
   - **Schema check.** The runner compares values only, so I also checked the 
schema on Spark 4.2.0. `struct(1, CAST(NULL AS INT), 'a')` has nullable 
`[false, true, false]` fields, `struct()` is `struct<>`, `struct(1 AS a, 2)` is 
`struct<a:int,col2:int>`, and duplicate names (`struct(a, a)`) are allowed.
   - **New `struct.slt` cases.** They cover columns, `a + 1` and `b + 1`, a 
qualified column, a struct field access, aliased and mixed arguments, duplicate 
names, array and map arguments, and the nullable-array regression.
   - **Unit tests.** `spark_struct.rs` covers field naming, empty struct and 
nullability. `planner.rs` covers the naming rule, the `c{i}` detection and the 
empty-argument branch.
   - `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.
   ## Release comparison
    
   Agreed that `struct(columns...)` should go straight from `c0, c1, ...` to 
the column names, so the PR description now says that, not only "`col1`, 
`col2`, ...".
    
   


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