Uh oh!
There was an error while loading. Please reload this page.
[SPARK-32796][SQL] Make withField API support nested struct in array - #29645
[SPARK-32796][SQL] Make withField API support nested struct in array#29645viirya wants to merge 2 commits into
Conversation
| case q: LogicalPlan => | ||
| q.transformExpressions { | ||
| case expr if !expr.childrenResolved => expr | ||
| case e: UnresolvedWithFields => WithFields(e.col, e.fieldName, e.expr) |
There was a problem hiding this comment.
This can be moved to other proper rule. Just not sure which one is good, so put as an individual rule first.
SparkQA
commented
Sep 4, 2020
Test build #128278 has finished for PR 29645 at commit
|
HyukjinKwon
commented
Sep 4, 2020
retest this please |
| test("withField should add field to struct of array") { | ||
| checkAnswerAndSchema( | ||
| arrayLevel1.withColumn("a", 'a.withField("d", lit(4))), |
There was a problem hiding this comment.
I personally more prefer an explicit call such as |
cloud-fan
commented
Sep 4, 2020
I agree with @HyukjinKwon that it's better to be more explicit. |
SparkQA
commented
Sep 4, 2020
Test build #128288 has finished for PR 29645 at commit
|
viirya
commented
Sep 4, 2020
@cloud-fan@HyukjinKwon Thanks for comment. So let me get more ideas from you. This is like a syntax sugar to more easily express complex nested |
27f37dc to
39edb0aCompareAn example looks like: privatelazyvalarrayType=ArrayType(
StructType(Seq(
StructField("a", IntegerType, nullable =false),
StructField("b", IntegerType, nullable =true),
StructField("c", IntegerType, nullable =false))),
containsNull =true)
privatelazyvalarrayStructArrayLevel1:DataFrame= spark.createDataFrame(
sparkContext.parallelize(Row(Array(Row(Array(Row(1, null, 3)), null, 3))) ::Nil),
StructType(
Seq(StructField("a", ArrayType(
StructType(Seq(
StructField("a", arrayType, nullable =false),
StructField("b", IntegerType, nullable =true),
StructField("c", IntegerType, nullable =false))),
containsNull =false)))))The data looks like: In order to replace deeply nested Currently by using arrayStructArrayLevel1.withColumn("a",
transform($"a", _.withField("a",
flatten(transform($"a.a", transform(_, _.withField("b", lit(2))))))))Using modified arrayStructArrayLevel1.withColumn("a", $"a".withField("a.b", lit(2)))It could significantly simplify how we add/replace deeply nested fields. |
SparkQA
commented
Sep 5, 2020
Test build #128313 has finished for PR 29645 at commit
|
SparkQA
commented
Sep 5, 2020
Test build #128314 has finished for PR 29645 at commit
|
cloud-fan
commented
Sep 7, 2020
We can save more code by supporting array of array of struct. It's a trade-off between "clear and simple semantic" vs "flexiblity of supporting various input types". |
viirya
commented
Sep 7, 2020
@cloud-fan Sorry if I mis-read your comment. Do you mean we should support array of array of struct? |
cloud-fan
commented
Sep 8, 2020
I mean we should prefer "clear and simple semantic", otherwise people can always ask to be more flexible and save more code, like supporting array of array of struct. |
viirya
commented
Sep 8, 2020
Okay, I see. It also makes sense to me. This is a hard trade-off between simplicity and flexibility. I will close this now. If we need this flexibility in the future, we can revisit it. |
encarvlucas
commented
Feb 18, 2022
Could you revisit this? |
cloud-fan
commented
Feb 21, 2022
Does |
encarvlucas
commented
Feb 21, 2022
Unfortunately it doesn't. I'm using spark through the pyspark library |
cloud-fan
commented
Feb 21, 2022
@HyukjinKwon shall we add the |
encarvlucas
commented
Feb 21, 2022
There is a |
cloud-fan
commented
Feb 21, 2022
Do you mean the |
encarvlucas
commented
Feb 21, 2022
Yes. Nevermind then, I retract my request. |
What changes were proposed in this pull request?
This patch adds nested struct support to
Column.withFieldAPI.Why are the changes needed?
Currently
Column.withFieldonly supportsStructType. For nested struct inArrayType, it doesn't support. We can support nested struct in array to make the API more general and useful.Does this PR introduce any user-facing change?
Yes. Adding nested struct support to
Column.withFieldAPI.How was this patch tested?
Unit tests.