Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-47252][DOCS] Clarify that pivot may trigger an eager computation #45363

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,12 @@ class RelationalGroupedDataset private[sql] (
/**
* Pivots a column of the current `DataFrame` and performs the specified aggregation.
*
* There are two versions of `pivot` function: one that requires the caller to specify the list
* of distinct values to pivot on, and one that does not. The latter is more concise but less
* efficient, because Spark needs to first compute the list of distinct values internally.
* Spark will eagerly compute the distinct values in `pivotColumn` so it can determine the
* resulting schema of the transformation. To avoid any eager computations, provide an explicit
* list of values via `pivot(pivotColumn: String, values: Seq[Any])`.
*
* {{{
* // Compute the sum of earnings for each year by course with each course as a separate column
* df.groupBy("year").pivot("course", Seq("dotNET", "Java")).sum("earnings")
*
* // Or without specifying column values (less efficient)
* df.groupBy("year").pivot("course").sum("earnings")
* }}}
*
Expand Down Expand Up @@ -392,11 +389,14 @@ class RelationalGroupedDataset private[sql] (
}

/**
* Pivots a column of the current `DataFrame` and performs the specified aggregation. This is an
* overloaded version of the `pivot` method with `pivotColumn` of the `String` type.
* Pivots a column of the current `DataFrame` and performs the specified aggregation.
*
* Spark will eagerly compute the distinct values in `pivotColumn` so it can determine the
* resulting schema of the transformation. To avoid any eager computations, provide an explicit
* list of values via `pivot(pivotColumn: Column, values: Seq[Any])`.
*
* {{{
* // Or without specifying column values (less efficient)
* // Compute the sum of earnings for each year by course with each course as a separate column
* df.groupBy($"year").pivot($"course").sum($"earnings");
* }}}
*
Expand Down
10 changes: 5 additions & 5 deletions python/pyspark/sql/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,7 @@ def sum(self, *cols: str) -> DataFrame: # type: ignore[empty-body]

def pivot(self, pivot_col: str, values: Optional[List["LiteralType"]] = None) -> "GroupedData":
"""
Pivots a column of the current :class:`DataFrame` and perform the specified aggregation.
There are two versions of the pivot function: one that requires the caller
to specify the list of distinct values to pivot on, and one that does not.
The latter is more concise but less efficient,
because Spark needs to first compute the list of distinct values internally.
Pivots a column of the current :class:`DataFrame` and performs the specified aggregation.

.. versionadded:: 1.6.0

Expand All @@ -450,6 +446,10 @@ def pivot(self, pivot_col: str, values: Optional[List["LiteralType"]] = None) ->
values : list, optional
List of values that will be translated to columns in the output DataFrame.

If ``values`` is not provided, Spark will eagerly compute the distinct values in
``pivot_col`` so it can determine the resulting schema of the transformation. To avoid
any eager computations, provide an explicit list of values.

Examples
--------
>>> from pyspark.sql import Row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,12 @@ class RelationalGroupedDataset protected[sql](
/**
* Pivots a column of the current `DataFrame` and performs the specified aggregation.
*
* There are two versions of `pivot` function: one that requires the caller to specify the list
* of distinct values to pivot on, and one that does not. The latter is more concise but less
* efficient, because Spark needs to first compute the list of distinct values internally.
* Spark will eagerly compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. To avoid any eager computations, provide an
* explicit list of values via `pivot(pivotColumn: String, values: Seq[Any])`.
*
* {{{
* // Compute the sum of earnings for each year by course with each course as a separate column
* df.groupBy("year").pivot("course", Seq("dotNET", "Java")).sum("earnings")
*
* // Or without specifying column values (less efficient)
* df.groupBy("year").pivot("course").sum("earnings")
* }}}
*
Expand Down Expand Up @@ -407,10 +404,13 @@ class RelationalGroupedDataset protected[sql](

/**
* Pivots a column of the current `DataFrame` and performs the specified aggregation.
* This is an overloaded version of the `pivot` method with `pivotColumn` of the `String` type.
*
* Spark will eagerly compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. To avoid any eager computations, provide an
* explicit list of values via `pivot(pivotColumn: Column, values: Seq[Any])`.
*
* {{{
* // Or without specifying column values (less efficient)
* // Compute the sum of earnings for each year by course with each course as a separate column
* df.groupBy($"year").pivot($"course").sum($"earnings");
* }}}
*
Expand Down