cloud-fan commented on code in PR #36150: URL: https://github.com/apache/spark/pull/36150#discussion_r922274515
########## sql/core/src/test/scala/org/apache/spark/sql/DatasetUnpivotSuite.scala: ########## @@ -0,0 +1,491 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import org.apache.spark.sql.errors.QueryErrorsSuiteBase +import org.apache.spark.sql.functions.{length, struct, sum} +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types._ + +/** + * Comprehensive tests for Dataset.unpivot. + */ +class DatasetUnpivotSuite extends QueryTest + with QueryErrorsSuiteBase + with SharedSparkSession { + import testImplicits._ + + lazy val wideDataDs: Dataset[WideData] = Seq( + WideData(1, "one", "One", Some(1), Some(1L)), + WideData(2, "two", null, None, Some(2L)), + WideData(3, null, "three", Some(3), None), + WideData(4, null, null, None, None) + ).toDS() + + val longDataRows = Seq( + Row(1, "str1", "one"), + Row(1, "str2", "One"), + Row(2, "str1", "two"), + Row(2, "str2", null), + Row(3, "str1", null), + Row(3, "str2", "three"), + Row(4, "str1", null), + Row(4, "str2", null) + ) + + val longDataWithoutIdRows: Seq[Row] = + longDataRows.map(row => Row(row.getString(1), row.getString(2))) + + val longSchema: StructType = StructType(Seq( + StructField("id", IntegerType, nullable = false), + StructField("var", StringType, nullable = false), + StructField("val", StringType, nullable = true) + )) + + lazy val wideStructDataDs: DataFrame = wideDataDs.select( + struct($"id").as("an"), + struct( + $"str1".as("one"), + $"str2".as("two") + ).as("str") + ) + val longStructDataRows: Seq[Row] = longDataRows.map(row => + Row( + row.getInt(0), + row.getString(1) match { + case "str1" => "one" + case "str2" => "two" + }, + row.getString(2)) + ) + + test("overloaded unpivot without values") { + val ds = wideDataDs.select($"id", $"str1", $"str2") + checkAnswer( + ds.unpivot(Array($"id"), "var", "val"), + ds.unpivot(Array($"id"), Array.empty, "var", "val")) + } + + test("unpivot with single id") { + val unpivoted = wideDataDs + .unpivot( + Array($"id"), + Array($"str1", $"str2"), + variableColumnName = "var", + valueColumnName = "val") + unpivoted.explain(true) + assert(unpivoted.schema === longSchema) + checkAnswer(unpivoted, longDataRows) + } + + test("unpivot with two ids") { + val unpivotedRows = Seq( + Row(1, 1, "str1", "one"), + Row(1, 1, "str2", "One"), + Row(2, null, "str1", "two"), + Row(2, null, "str2", null), + Row(3, 3, "str1", null), + Row(3, 3, "str2", "three"), + Row(4, null, "str1", null), + Row(4, null, "str2", null)) + + val unpivoted = wideDataDs + .unpivot( + Array($"id", $"int1"), + Array($"str1", $"str2"), + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted.schema === StructType(Seq( + StructField("id", IntegerType, nullable = false), + StructField("int1", IntegerType, nullable = true), + StructField("var", StringType, nullable = false), + StructField("val", StringType, nullable = true)))) + checkAnswer(unpivoted, unpivotedRows) + } + + test("unpivot without ids") { + val unpivoted = wideDataDs + .unpivot( + Array.empty, + Array($"str1", $"str2"), + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted.schema === StructType(Seq( + StructField("var", StringType, nullable = false), + StructField("val", StringType, nullable = true)))) + checkAnswer(unpivoted, longDataWithoutIdRows) + } + + test("unpivot without values") { + val unpivoted = wideDataDs.select($"id", $"str1", $"str2") + .unpivot( + Array($"id"), + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted.schema === longSchema) + checkAnswer(unpivoted, longDataRows) + + val unpivoted2 = wideDataDs.select($"id", $"str1", $"str2") + .unpivot( + Array($"id"), + Array.empty, + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted2.schema === longSchema) + checkAnswer(unpivoted2, longDataRows) + } + + test("unpivot without ids or values") { + val unpivoted = wideDataDs.select($"str1", $"str2") + .unpivot( + Array.empty, + Array.empty, + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted.schema === StructType(Seq( + StructField("var", StringType, nullable = false), + StructField("val", StringType, nullable = true)))) + checkAnswer(unpivoted, longDataWithoutIdRows) + } + + test("unpivot with star values") { + val unpivoted = wideDataDs.select($"str1", $"str2") + .unpivot( + Array.empty, + Array($"*"), + variableColumnName = "var", + valueColumnName = "val") + assert(unpivoted.schema === StructType(Seq( + StructField("var", StringType, nullable = false), + StructField("val", StringType, nullable = true)))) + checkAnswer(unpivoted, longDataWithoutIdRows) + } + + test("unpivot with id and star values") { + val unpivoted = wideDataDs.select($"id", $"int1", $"long1") + .unpivot( + Array($"id"), + Array($"*"), Review Comment: does this make `id` both id and value column? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
