Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2452,25 +2452,13 @@ class SparkConnectPlanner(
}

val pivotExpr = transformExpression(rel.getPivot.getCol)

var valueExprs = rel.getPivot.getValuesList.asScala.toSeq.map(transformLiteral)
if (valueExprs.isEmpty) {
// This is to prevent unintended OOM errors when the number of distinct values is large
val maxValues = session.sessionState.conf.dataFramePivotMaxValues
// Get the distinct values of the column and sort them so its consistent
val pivotCol = Column(pivotExpr)
valueExprs = Dataset
.ofRows(session, input)
.select(pivotCol)
.distinct()
.limit(maxValues + 1)
.sort(pivotCol) // ensure that the output columns are in a consistent logical order
.collect()
.map(_.get(0))
.toImmutableArraySeq
val valueExprs = if (rel.getPivot.getValuesCount > 0) {
rel.getPivot.getValuesList.asScala.toSeq.map(transformLiteral)
} else {
RelationalGroupedDataset
.collectPivotValues(Dataset.ofRows(session, input), Column(pivotExpr))
.map(expressions.Literal.apply)
}

logical.Pivot(
groupByExprsOpt = Some(groupingExprs.map(toNamedExpression)),
pivotColumn = pivotExpr,
Expand All @@ -2489,6 +2477,7 @@ class SparkConnectPlanner(
userGivenGroupByExprs = groupingExprs)),
aggregateExpressions = aliasedAgg,
child = input)

case other => throw InvalidPlanInput(s"Unknown Group Type $other")
}
}
Expand Down
5 changes: 5 additions & 0 deletions python/pyspark/sql/tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from pyspark.sql import Row
from pyspark.sql import functions as sf
from pyspark.errors import AnalysisException
from pyspark.testing.sqlutils import (
ReusedSQLTestCase,
have_pandas,
Expand Down Expand Up @@ -185,6 +186,10 @@ def test_order_by_ordinal(self):
with self.assertRaises(IndexError):
df.orderBy(-3)

def test_pivot_exceed_max_values(self):
with self.assertRaises(AnalysisException):
spark.range(100001).groupBy(sf.lit(1)).pivot("id").count().show()


class GroupTests(GroupTestsMixin, ReusedSQLTestCase):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,28 +423,7 @@ class RelationalGroupedDataset protected[sql](
* @since 2.4.0
*/
def pivot(pivotColumn: Column): RelationalGroupedDataset = {
if (df.isStreaming) {
throw new AnalysisException(
errorClass = "_LEGACY_ERROR_TEMP_3063",
messageParameters = Map.empty)
}
// This is to prevent unintended OOM errors when the number of distinct values is large
val maxValues = df.sparkSession.sessionState.conf.dataFramePivotMaxValues
// Get the distinct values of the column and sort them so its consistent
val values = df.select(pivotColumn)
.distinct()
.limit(maxValues + 1)
.sort(pivotColumn) // ensure that the output columns are in a consistent logical order
.collect()
.map(_.get(0))
.toImmutableArraySeq

if (values.length > maxValues) {
throw QueryCompilationErrors.aggregationFunctionAppliedOnNonNumericColumnError(
pivotColumn.toString, maxValues)
}

pivot(pivotColumn, values)
pivot(pivotColumn, collectPivotValues(df, pivotColumn))
}

/**
Expand Down Expand Up @@ -798,6 +777,30 @@ private[sql] object RelationalGroupedDataset {
case expr: Expression => Alias(expr, toPrettySQL(expr))()
}

private[sql] def collectPivotValues(df: DataFrame, pivotColumn: Column): Seq[Any] = {
if (df.isStreaming) {
throw new AnalysisException(
errorClass = "_LEGACY_ERROR_TEMP_3063",
messageParameters = Map.empty)
}
// This is to prevent unintended OOM errors when the number of distinct values is large
val maxValues = df.sparkSession.sessionState.conf.dataFramePivotMaxValues
// Get the distinct values of the column and sort them so its consistent
val values = df.select(pivotColumn)
.distinct()
.limit(maxValues + 1)
.sort(pivotColumn) // ensure that the output columns are in a consistent logical order
.collect()
.map(_.get(0))
.toImmutableArraySeq

if (values.length > maxValues) {
throw QueryCompilationErrors.aggregationFunctionAppliedOnNonNumericColumnError(
pivotColumn.toString, maxValues)
}
values
}

/**
* The Grouping Type
*/
Expand Down