Kimahriman commented on a change in pull request #32448:
URL: https://github.com/apache/spark/pull/32448#discussion_r633698385
##########
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:
The union throws an analysis exception because the right side gets
corrupted. It's the same example I have in the issue:
https://issues.apache.org/jira/browse/SPARK-35290
##########
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:
I found one tweak that seemed to fix the issue but made the deeply
nested case a lot slower to resolve. I went down this route because it sounded
like this would have been the ideal behavior originally but sorting seemed like
the only option. And it simplifies things a little bit and uses existing struct
merging functionality.
--
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]