EnricoMi commented on code in PR #37407:
URL: https://github.com/apache/spark/pull/37407#discussion_r981080039
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -869,26 +869,50 @@ class Analyzer(override val catalogManager:
CatalogManager)
def apply(plan: LogicalPlan): LogicalPlan =
plan.resolveOperatorsWithPruning(
_.containsPattern(UNPIVOT), ruleId) {
- // once children and ids are resolved, we can determine values, if non
were given
- case up: Unpivot if up.childrenResolved && up.ids.forall(_.resolved) &&
up.values.isEmpty =>
- up.copy(values = up.child.output.diff(up.ids))
-
- case up: Unpivot if !up.childrenResolved || !up.ids.forall(_.resolved) ||
- up.values.isEmpty || !up.values.forall(_.resolved) ||
!up.valuesTypeCoercioned => up
+ // once children are resolved, we can determine values from ids and vice
versa
+ // if only either is given
+ case up: Unpivot if up.childrenResolved &&
+ up.ids.exists(_.forall(_.resolved)) && up.values.isEmpty =>
+ val values = (AttributeSet(up.child.output) --
AttributeSet(up.ids.get)).toSeq
+ // up.child.output.intersect preserves order of columns
+ up.copy(values = Some(up.child.output.intersect(values).map(Seq(_))))
+ case up: Unpivot if up.childrenResolved &&
+ up.values.exists(_.forall(_.forall(_.resolved))) && up.ids.isEmpty =>
+ val ids = (AttributeSet(up.child.output) --
AttributeSet(up.values.get.flatten)).toSeq
+ // up.child.output.intersect preserves order of columns
+ up.copy(ids = Some(up.child.output.intersect(ids)))
+
+ case up: Unpivot if !up.childrenResolved ||
!up.ids.exists(_.forall(_.resolved)) ||
+ !up.values.exists(_.nonEmpty) ||
!up.values.exists(_.forall(_.forall(_.resolved))) ||
+ !up.values.get.forall(_.length == up.valueColumnNames.length) ||
+ !up.valuesTypeCoercioned => up
// TypeCoercionBase.UnpivotCoercion determines valueType
// and casts values once values are set and resolved
- case Unpivot(ids, values, variableColumnName, valueColumnName, child) =>
+ case Unpivot(Some(ids), Some(values), aliases, variableColumnName,
valueColumnNames, child) =>
+
+ def toString(values: Seq[NamedExpression]): String =
+ "(" + values.map(v => quoteIdentifier(v.name)).mkString(",") + ")"
Review Comment:
[BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#unpivot_operator)
does `col1_col2`:
row_value_alias: An optional alias for a column set that is displayed
for the column set in name_column.
If not specified, a string value for the column set is used and each
column in the string is separated with
an underscore (_). For example, `(col1, col2)` outputs `col1_col2`.
[Oracle](https://www.oracletutorial.com/oracle-basics/oracle-unpivot/) and
[T-SQL](https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot?view=sql-server-ver15#unpivot-example)
are not specific about the string representation of those column name tuples.
[Oracle seems to do the same
thing](https://blogs.oracle.com/sql/post/how-to-convert-rows-to-columns-and-back-again-with-sql-aka-pivot-and-unpivot)
as BigQuery _(given `(gold_medals, gold_sports)`)_:
Without these medal column contains the concatenation of the source
column names.
For example GOLD_MEDALS_GOLD_SPORTS.
I am going to change that.
--
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]