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 2 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,18 +259,19 @@ 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.
*
* {{{
* // 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")
* }}}
*
* @note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can just make it a bit shorter, and put it into the main doc instead of the separate note. I don't want to scare users about this .. e.g., DataFrameReader.csv about schema inference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I trimmed the note a bit. Is that better?

I also took a look at the CSV reader method:

* This function will go through the input once to determine the input schema if `inferSchema`
* is enabled. To avoid going through the entire data once, disable `inferSchema` option or
* specify the schema explicitly using `schema`.

It's pretty similar to what I'm proposing here.

I believe it's more important to highlight the eager computation here since pivot is a transformation and, unlike with reader methods, users are probably not expecting expensive computations to be triggered. But I agree, we don't want to make it sound like there's something wrong with not specifying pivot values.

* Spark will '''eagerly''' compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. Depending on the size and complexity of your
* data, this may take some time. In other words, though the pivot transformation is lazy like
* most DataFrame transformations, computing the distinct pivot values is not. To avoid any
* eager computations, provide an explicit list of values via `pivot(pivotColumn: String,
* values: Seq[Any])`.
*
* @see
* `org.apache.spark.sql.Dataset.unpivot` for the reverse operation, except for the
* aggregation.
Expand Down Expand Up @@ -392,14 +393,21 @@ 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.
*
* {{{
* // 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");
* }}}
*
* @note
* Spark will '''eagerly''' compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. Depending on the size and complexity of your
* data, this may take some time. In other words, though the pivot transformation is lazy like
* most DataFrame transformations, computing the distinct pivot values is not. To avoid any
* eager computations, provide an explicit list of values via `pivot(pivotColumn: Column,
* values: Seq[Any])`.
*
* @see
* `org.apache.spark.sql.Dataset.unpivot` for the reverse operation, except for the
* aggregation.
Expand Down
14 changes: 9 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,14 @@ 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.

.. note:: If ``values`` is not provided, Spark will **eagerly** compute the distinct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this too. I would just put it up in the doctest (like DataFrameReader.csv)

values in ``pivot_col`` so it can determine the resulting schema of the
transformation. Depending on the size and complexity of your data, this may take
some time.
In other words, though the pivot transformation is lazy like most DataFrame
transformations, computing the distinct pivot values is not. 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,18 +324,18 @@ 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.
*
* {{{
* // 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")
* }}}
*
* @note Spark will '''eagerly''' compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. Depending on the size and complexity of your
* data, this may take some time. In other words, though the pivot transformation is lazy like
* most DataFrame transformations, computing the distinct pivot values is not. To avoid any
* eager computations, provide an explicit list of values via
* `pivot(pivotColumn: String, values: Seq[Any])`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably spent about an hour trying to get this to work as a proper link via [[pivot(...]], per the scaladoc docs on ambiguous links, but I could not get it to work.

*
* @see `org.apache.spark.sql.Dataset.unpivot` for the reverse operation,
* except for the aggregation.
*
Expand Down Expand Up @@ -407,13 +407,19 @@ 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.
*
* {{{
* // 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");
* }}}
*
* @note Spark will '''eagerly''' compute the distinct values in `pivotColumn` so it can determine
* the resulting schema of the transformation. Depending on the size and complexity of your
* data, this may take some time. In other words, though the pivot transformation is lazy like
* most DataFrame transformations, computing the distinct pivot values is not. To avoid any
* eager computations, provide an explicit list of values via
* `pivot(pivotColumn: Column, values: Seq[Any])`.
*
* @see `org.apache.spark.sql.Dataset.unpivot` for the reverse operation,
* except for the aggregation.
*
Expand Down