[SPARK-34805][SQL] Propagate metadata from nested columns in Alias#35270
Closed
kevinwallimann wants to merge 11 commits intoapache:masterfrom
kevinwallimann:SPARK-34805
Closed
[SPARK-34805][SQL] Propagate metadata from nested columns in Alias#35270kevinwallimann wants to merge 11 commits intoapache:masterfrom kevinwallimann:SPARK-34805
kevinwallimann wants to merge 11 commits intoapache:masterfrom
kevinwallimann:SPARK-34805
Conversation
|
Can one of the admins verify this patch? |
Member
|
cc @viirya FYI |
viirya
reviewed
Jan 23, 2022
| assert(newCol.expr.asInstanceOf[NamedExpression].metadata.getString("key") === "value") | ||
| } | ||
|
|
||
| test("as propagates metadata from nested column") { |
Member
There was a problem hiding this comment.
Can you add jira number as test name prefix? I.e. SPARK-34805: ...
viirya
reviewed
Jan 23, 2022
| nonInheritableMetadataKeys.foreach(builder.remove) | ||
| builder.build() | ||
|
|
||
| case structField: GetStructField => structField.metadata |
Member
There was a problem hiding this comment.
Don't we need to remove keys in nonInheritableMetadataKeys?
Contributor
Author
There was a problem hiding this comment.
Yes, you're right. I fixed it and added an additional test.
Contributor
Author
|
Hi @viirya Could you please review my changes again? |
|
Hello, @viirya any news on this review? |
cloud-fan
approved these changes
Mar 21, 2022
Contributor
|
thanks, merging to master/3.3! |
cloud-fan
pushed a commit
that referenced
this pull request
Mar 21, 2022
### What changes were proposed in this pull request?
The metadata of a `GetStructField` expression is propagated in the `Alias` expression.
### Why are the changes needed?
Currently, in a dataframe with nested structs, when selecting an inner struct, the metadata of that inner struct is lost. For example, suppose
`df.schema.head.dataType.head.metadata`
returns a non-empty Metadata object, then
`df.select('Field0.SubField0').schema.head.metadata`
returns an empty Metadata object
The following snippet demonstrates the issue
```
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{LongType, MetadataBuilder, StructField, StructType}
val metadataAbc = new MetadataBuilder().putString("my-metadata", "abc").build()
val metadataXyz = new MetadataBuilder().putString("my-metadata", "xyz").build()
val schema = StructType(Seq(
StructField("abc",
StructType(Seq(
StructField("xyz", LongType, nullable = true, metadataXyz)
)), metadata = metadataAbc)))
import scala.collection.JavaConverters._
val data = Seq(Row(Row(1L))).asJava
val df = spark.createDataFrame(data, schema)
println(df.select("abc").schema.head.metadata) // OK, metadata is {"my-metadata":"abc"}
println(df.select("abc.xyz").schema.head.metadata) // NOT OK, metadata is {}, expected {"my-metadata","xyz"}
```
The issue can be reproduced in versions 3.2.0, 3.1.2 and 2.4.8
### Does this PR introduce _any_ user-facing change?
No
### How was this patch tested?
Added a new test
Closes #35270 from kevinwallimann/SPARK-34805.
Authored-by: Kevin Wallimann <kevin.wallimann@absa.africa>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
(cherry picked from commit f8fd023)
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
The metadata of a
GetStructFieldexpression is propagated in theAliasexpression.Why are the changes needed?
Currently, in a dataframe with nested structs, when selecting an inner struct, the metadata of that inner struct is lost. For example, suppose
df.schema.head.dataType.head.metadatareturns a non-empty Metadata object, then
df.select('Field0.SubField0').schema.head.metadatareturns an empty Metadata object
The following snippet demonstrates the issue
The issue can be reproduced in versions 3.2.0, 3.1.2 and 2.4.8
Does this PR introduce any user-facing change?
No
How was this patch tested?
Added a new test