Kimahriman commented on a change in pull request #32448:
URL: https://github.com/apache/spark/pull/32448#discussion_r634518604
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala
##########
@@ -743,17 +777,59 @@ class DataFrameSetOperationsSuite extends QueryTest with
SharedSparkSession {
StructField("a", StringType)))
val nestedStructValues2 = Row("b", "a")
- val df1: DataFrame = spark.createDataFrame(
+ val df1 = spark.createDataFrame(
sparkContext.parallelize(Row(nestedStructValues1) :: Nil),
StructType(Seq(StructField("topLevelCol", nestedStructType1))))
- val df2: DataFrame = spark.createDataFrame(
+ val df2 = spark.createDataFrame(
sparkContext.parallelize(Row(nestedStructValues2) :: Nil),
StructType(Seq(StructField("topLevelCol", nestedStructType2))))
val union = df1.unionByName(df2, allowMissingColumns = true)
- checkAnswer(union, Row(Row(null, "b")) :: Row(Row("a", "b")) :: Nil)
- assert(union.schema.toDDL == "`topLevelCol` STRUCT<`a`: STRING, `b`:
STRING>")
+ assert(union.schema.toDDL == "`topLevelCol` STRUCT<`b`: STRING, `a`:
STRING>")
+ checkAnswer(union, Row(Row("b", null)) :: Row(Row("b", "a")) :: Nil)
+ }
+
+ test("SPARK-35290: Make unionByName null-filling behavior work with struct
columns"
+ + " - sorting edge case") {
+ val nestedStructType1 = StructType(Seq(
+ StructField("b", StructType(Seq(
+ StructField("ba", StringType)
+ )))
+ ))
+ val nestedStructValues1 = Row(Row("ba"))
+
+ val nestedStructType2 = StructType(Seq(
+ StructField("a", StructType(Seq(
+ StructField("aa", StringType)
+ ))),
+ StructField("b", StructType(Seq(
+ StructField("bb", StringType)
+ )))
+ ))
+ val nestedStructValues2 = Row(Row("aa"), Row("bb"))
+
+ val df1 = spark.createDataFrame(
+ sparkContext.parallelize(Row(nestedStructValues1) :: Nil),
+ StructType(Seq(StructField("topLevelCol", nestedStructType1))))
+
+ val df2 = spark.createDataFrame(
+ sparkContext.parallelize(Row(nestedStructValues2) :: Nil),
+ StructType(Seq(StructField("topLevelCol", nestedStructType2))))
+
+ var unionDf = df1.unionByName(df2, true)
+ assert(unionDf.schema.toDDL == "`topLevelCol` " +
+ "STRUCT<`b`: STRUCT<`ba`: STRING, `bb`: STRING>, `a`: STRUCT<`aa`:
STRING>>")
+ checkAnswer(unionDf,
+ Row(Row(Row("ba", null), null)) ::
+ Row(Row(Row(null, "bb"), Row("aa"))) :: Nil)
+
+ unionDf = df2.unionByName(df1, true)
+ assert(unionDf.schema.toDDL == "`topLevelCol` STRUCT<`a`: STRUCT<`aa`:
STRING>, " +
+ "`b`: STRUCT<`bb`: STRING, `ba`: STRING>>")
+ checkAnswer(unionDf,
+ Row(Row(null, Row(null, "ba"))) ::
+ Row(Row(Row("aa"), Row("bb", null))) :: Nil)
Review comment:
It's just weird because it's different behavior than every other part of
spark. `withColumn` and `withField` both append to the end. And currently
before this PR and if the I applied the tweak `df1.unionByname(df2, True)`
would not always have the same schema as `df2.unionByName(df1, True)`. It only
sorts nested struct fields, not the top level attributes.
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]