fqaiser94 commented on a change in pull request #29587:
URL: https://github.com/apache/spark/pull/29587#discussion_r488100532
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/DataFrameSetOperationsSuite.scala
##########
@@ -507,33 +507,156 @@ class DataFrameSetOperationsSuite extends QueryTest with
SharedSparkSession {
}
test("SPARK-29358: Make unionByName optionally fill missing columns with
nulls") {
- var df1 = Seq(1, 2, 3).toDF("a")
- var df2 = Seq(3, 1, 2).toDF("b")
- val df3 = Seq(2, 3, 1).toDF("c")
- val unionDf = df1.unionByName(df2.unionByName(df3, true), true)
- checkAnswer(unionDf,
- Row(1, null, null) :: Row(2, null, null) :: Row(3, null, null) :: // df1
- Row(null, 3, null) :: Row(null, 1, null) :: Row(null, 2, null) :: //
df2
- Row(null, null, 2) :: Row(null, null, 3) :: Row(null, null, 1) :: Nil
// df3
- )
+ Seq("true", "false").foreach { config =>
+ withSQLConf(SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> config) {
+ var df1 = Seq(1, 2, 3).toDF("a")
+ var df2 = Seq(3, 1, 2).toDF("b")
+ val df3 = Seq(2, 3, 1).toDF("c")
+ val unionDf = df1.unionByName(df2.unionByName(df3, true), true)
+ checkAnswer(unionDf,
+ Row(1, null, null) :: Row(2, null, null) :: Row(3, null, null) :: //
df1
+ Row(null, 3, null) :: Row(null, 1, null) :: Row(null, 2, null) ::
// df2
+ Row(null, null, 2) :: Row(null, null, 3) :: Row(null, null, 1) ::
Nil // df3
+ )
+
+ df1 = Seq((1, 2)).toDF("a", "c")
+ df2 = Seq((3, 4, 5)).toDF("a", "b", "c")
+ checkAnswer(df1.unionByName(df2, true),
+ Row(1, 2, null) :: Row(3, 5, 4) :: Nil)
+ checkAnswer(df2.unionByName(df1, true),
+ Row(3, 4, 5) :: Row(1, null, 2) :: Nil)
+
+ withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
+ df2 = Seq((3, 4, 5)).toDF("a", "B", "C")
+ val union1 = df1.unionByName(df2, true)
+ val union2 = df2.unionByName(df1, true)
+
+ checkAnswer(union1, Row(1, 2, null, null) :: Row(3, null, 4, 5) ::
Nil)
+ checkAnswer(union2, Row(3, 4, 5, null) :: Row(1, null, null, 2) ::
Nil)
+
+ assert(union1.schema.fieldNames === Array("a", "c", "B", "C"))
+ assert(union2.schema.fieldNames === Array("a", "B", "C", "c"))
+ }
+ }
+ }
+ }
- df1 = Seq((1, 2)).toDF("a", "c")
- df2 = Seq((3, 4, 5)).toDF("a", "b", "c")
- checkAnswer(df1.unionByName(df2, true),
- Row(1, 2, null) :: Row(3, 5, 4) :: Nil)
- checkAnswer(df2.unionByName(df1, true),
- Row(3, 4, 5) :: Row(1, null, 2) :: Nil)
+ test("SPARK-32376: Make unionByName null-filling behavior work with struct
columns - simple") {
+ withSQLConf(SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> "true") {
+ val df1 = Seq(((1, 2, 3), 0), ((2, 3, 4), 1), ((3, 4, 5), 2)).toDF("a",
"idx")
+ val df2 = Seq(((3, 4), 0), ((1, 2), 1), ((2, 3), 2)).toDF("a", "idx")
+ val df3 = Seq(((100, 101, 102, 103), 0), ((110, 111, 112, 113), 1),
((120, 121, 122, 123), 2))
+ .toDF("a", "idx")
+
+ var unionDf = df1.unionByName(df2, true)
+
+ checkAnswer(unionDf,
+ Row(Row(1, 2, 3), 0) :: Row(Row(2, 3, 4), 1) :: Row(Row(3, 4, 5), 2) ::
+ Row(Row(3, 4, null), 0) :: Row(Row(1, 2, null), 1) :: Row(Row(2, 3,
null), 2) :: Nil
+ )
+
+ assert(unionDf.schema.toDDL == "`a` STRUCT<`_1`: INT, `_2`: INT, `_3`:
INT>,`idx` INT")
+
+ unionDf = df1.unionByName(df2, true).unionByName(df3, true)
+
+ checkAnswer(unionDf,
+ Row(Row(1, 2, 3, null), 0) ::
+ Row(Row(2, 3, 4, null), 1) ::
+ Row(Row(3, 4, 5, null), 2) :: // df1
+ Row(Row(3, 4, null, null), 0) ::
+ Row(Row(1, 2, null, null), 1) ::
+ Row(Row(2, 3, null, null), 2) :: // df2
+ Row(Row(100, 101, 102, 103), 0) ::
+ Row(Row(110, 111, 112, 113), 1) ::
+ Row(Row(120, 121, 122, 123), 2) :: Nil // df3
+ )
+ assert(unionDf.schema.toDDL ==
+ "`a` STRUCT<`_1`: INT, `_2`: INT, `_3`: INT, `_4`: INT>,`idx` INT")
+ }
+ }
- withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true") {
- df2 = Seq((3, 4, 5)).toDF("a", "B", "C")
- val union1 = df1.unionByName(df2, true)
- val union2 = df2.unionByName(df1, true)
+ test("SPARK-32376: Make unionByName null-filling behavior work with struct
columns - nested") {
+ withSQLConf(SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> "true") {
+ val df1 = Seq((0, UnionClass1a(0, 1L, UnionClass2(1, "2")))).toDF("id",
"a")
+ val df2 = Seq((1, UnionClass1b(1, 2L, UnionClass3(2, 3L)))).toDF("id",
"a")
+
+ val expectedSchema = "`id` INT,`a` STRUCT<`a`: INT, `b`: BIGINT, " +
+ "`nested`: STRUCT<`a`: INT, `b`: BIGINT, `c`: STRING>>"
+
+ var unionDf = df1.unionByName(df2, true)
+ checkAnswer(unionDf,
+ Row(0, Row(0, 1, Row(1, null, "2"))) ::
+ Row(1, Row(1, 2, Row(2, 3L, null))) :: Nil)
+ assert(unionDf.schema.toDDL == expectedSchema)
+
+ unionDf = df2.unionByName(df1, true)
+ checkAnswer(unionDf,
+ Row(1, Row(1, 2, Row(2, 3L, null))) ::
+ Row(0, Row(0, 1, Row(1, null, "2"))) :: Nil)
+ assert(unionDf.schema.toDDL == expectedSchema)
+
+ val df3 = Seq((2, UnionClass1b(2, 3L, null))).toDF("id", "a")
+ unionDf = df1.unionByName(df3, true)
+ checkAnswer(unionDf,
+ Row(0, Row(0, 1, Row(1, null, "2"))) ::
+ Row(2, Row(2, 3, null)) :: Nil)
+ assert(unionDf.schema.toDDL == expectedSchema)
+ }
+ }
- checkAnswer(union1, Row(1, 2, null, null) :: Row(3, null, 4, 5) :: Nil)
- checkAnswer(union2, Row(3, 4, 5, null) :: Row(1, null, null, 2) :: Nil)
+ test("SPARK-32376: Make unionByName null-filling behavior work with struct
columns" +
+ " - case-sensitive cases") {
+ withSQLConf(SQLConf.CASE_SENSITIVE.key -> "true",
+ SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> "true") {
+ val df1 = Seq((0, UnionClass1a(0, 1L, UnionClass2(1, "2")))).toDF("id",
"a")
+ val df2 = Seq((1, UnionClass1c(1, 2L, UnionClass4(2, 3L)))).toDF("id",
"a")
+
+ var unionDf = df1.unionByName(df2, true)
+ checkAnswer(unionDf,
+ Row(0, Row(0, 1, Row(null, 1, null, "2"))) ::
+ Row(1, Row(1, 2, Row(2, null, 3L, null))) :: Nil)
+ assert(unionDf.schema.toDDL ==
+ "`id` INT,`a` STRUCT<`a`: INT, `b`: BIGINT, " +
+ "`nested`: STRUCT<`A`: INT, `a`: INT, `b`: BIGINT, `c`: STRING>>")
+
+ unionDf = df2.unionByName(df1, true)
+ checkAnswer(unionDf,
+ Row(1, Row(1, 2, Row(2, null, 3L, null))) ::
+ Row(0, Row(0, 1, Row(null, 1, null, "2"))) :: Nil)
+ assert(unionDf.schema.toDDL ==
+ "`id` INT,`a` STRUCT<`a`: INT, `b`: BIGINT, " +
+ "`nested`: STRUCT<`A`: INT, `a`: INT, `b`: BIGINT, `c`: STRING>>")
+
+ val df3 = Seq((2, UnionClass1b(2, 3L, UnionClass3(4, 5L)))).toDF("id",
"a")
+ unionDf = df2.unionByName(df3, true)
+ checkAnswer(unionDf,
+ Row(1, Row(1, 2, Row(2, null, 3L))) ::
+ Row(2, Row(2, 3, Row(null, 4, 5L))) :: Nil)
+ assert(unionDf.schema.toDDL ==
+ "`id` INT,`a` STRUCT<`a`: INT, `b`: BIGINT, " +
+ "`nested`: STRUCT<`A`: INT, `a`: INT, `b`: BIGINT>>")
+ }
+ }
- assert(union1.schema.fieldNames === Array("a", "c", "B", "C"))
- assert(union2.schema.fieldNames === Array("a", "B", "C", "c"))
+ test("SPARK-32376: Make unionByName null-filling behavior work with struct
columns - disable") {
+ withSQLConf(SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> "false") {
+ val df1 = Seq(((1, 2, 3), 0), ((2, 3, 4), 1), ((3, 4, 5), 2)).toDF("a",
"idx")
+ val df2 = Seq(((3, 4), 0), ((1, 2), 1), ((2, 3), 2)).toDF("a", "idx")
+
+ val err = intercept[AnalysisException] {
+ df1.unionByName(df2, true).collect()
+ }
+ assert(err.getMessage.contains("Union can only be performed on tables
with the compatible " +
+ "column types. struct<_1:int,_2:int> <> struct<_1:int,_2:int,_3:int>
at the first column " +
+ "of the second table"))
}
}
}
+
+case class UnionClass1a(a: Int, b: Long, nested: UnionClass2)
+case class UnionClass1b(a: Int, b: Long, nested: UnionClass3)
+case class UnionClass1c(a: Int, b: Long, nested: UnionClass4)
+
+case class UnionClass2(a: Int, c: String)
+case class UnionClass3(a: Int, b: Long)
+case class UnionClass4(A: Int, b: Long)
Review comment:
What if there are missing nested columns only on one side and fields are
arranged in a "not-natural" order on the other side?
For example:
```
test("failing test?") {
withSQLConf(SQLConf.UNION_BYNAME_STRUCT_SUPPORT_ENABLED.key -> "true") {
val nestedStructType1 = StructType(Seq(
StructField("b", StringType)))
val nestedStructValues1 = Row("b")
val nestedStructType2 = StructType(Seq(
StructField("b", StringType),
StructField("a", StringType)))
val nestedStructValues2 = Row("b", "a")
val df1: DataFrame = spark.createDataFrame(
sparkContext.parallelize(Row(nestedStructValues1) :: Nil),
StructType(Seq(StructField("topLevelCol", nestedStructType1))))
val df2: DataFrame = spark.createDataFrame(
sparkContext.parallelize(Row(nestedStructValues2) :: Nil),
StructType(Seq(StructField("topLevelCol", nestedStructType2))))
df1.unionByName(df2, allowMissingColumns = true).show(false)
}
}
```
Gives me the following error:
```
[info] - failing test? *** FAILED *** (3 seconds, 719 milliseconds)
[info] org.apache.spark.sql.AnalysisException: Union can only be performed
on tables with the compatible column types. struct<b:string,a:string> <>
struct<a:string,b:string> at the first column of the second table;;
[info] 'Union false, false
[info] :- Project [with_fields(topLevelCol#1, a, null, true) AS
topLevelCol#6]
[info] : +- LogicalRDD [topLevelCol#1], false
[info] +- Project [topLevelCol#4]
[info] +- LogicalRDD [topLevelCol#4], false
```
Is this expected behaviour?
----------------------------------------------------------------
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]