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


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

Review Comment:
   @cloud-fan looks like `AttributeSet` treats `$"id"`, `$"id" * 2`, and 
`$"id".as("uid")` as equal.
   
   This is why `id` rows disappear from
   
   ```scala
   df.unpivot(Array($"id" * 2), "var", "val")
   ```
   
https://github.com/apache/spark/pull/37407/files#diff-2a4366c38acddfb5e0f468e88411679ccb3c081369a457606b3460e0e78d29d6R165
   
   and
   
   ```scala
   df.unpivot(Array($"id".as("uid")), "var", "val")
   ```
   
https://github.com/apache/spark/pull/37407/files#diff-2a4366c38acddfb5e0f468e88411679ccb3c081369a457606b3460e0e78d29d6R181
   
   I am using `AttributeSet -- AttributeSet` here because 
`up.child.output.diff(up.ids))` does not work as `up.child.output` contains 
different `AttributeReference` _instances_ than `up.ids`, though they got 
resolved to the same attributes. The `diff` does not remove any `ids` from 
`up.child.output`.



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