EnricoMi commented on code in PR #36150:
URL: https://github.com/apache/spark/pull/36150#discussion_r881695872


##########
sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala:
##########
@@ -2012,6 +2012,152 @@ class Dataset[T] private[sql](
   @scala.annotation.varargs
   def agg(expr: Column, exprs: Column*): DataFrame = groupBy().agg(expr, exprs 
: _*)
 
+  /**
+   * (Scala-specific)
+   * Unpivot a DataFrame from wide format to long format, optionally
+   * leaving identifier variables set.
+   *
+   * This function is useful to massage a DataFrame into a format where some
+   * columns are identifier variables (`ids`), while all other columns,
+   * considered measured variables (`values`), are "unpivoted" to the rows,
+   * leaving just two non-identifier columns, 'variable' and 'value'.
+   *
+   * {{{
+   *   val df = Seq((1, 11, 12L), (2, 21, 22L)).toDF("id", "int", "long")
+   *   df.show()
+   *   // output:
+   *   // +---+---+----+
+   *   // | id|int|long|
+   *   // +---+---+----+
+   *   // |  1| 11|  12|
+   *   // |  2| 21|  22|
+   *   // +---+---+----+
+   *
+   *   df.melt(Seq("id")).show()
+   *   // output:
+   *   // +---+--------+-----+
+   *   // | id|variable|value|

Review Comment:
   The `variable` and `value` column names come from the [Pandas 
API](https://pandas.pydata.org/docs/reference/api/pandas.melt.html). So this is 
identical to existing [PySpark Pandas 
API](https://github.com/apache/spark/blob/v3.2.1/python/pyspark/pandas/frame.py#L9019-L9023).
   
   SQL based DBMSes require those column names to be provided, so there is no 
default. See 
[BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator),
 
[T-SQL](https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15#unpivot-example),
 [Oracle](https://www.oracletutorial.com/oracle-basics/oracle-unpivot/).



-- 
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]

Reply via email to