EnricoMi commented on code in PR #37407:
URL: https://github.com/apache/spark/pull/37407#discussion_r972844049
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -869,26 +873,55 @@ 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))
+ // 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 =>
+ up.copy(values =
+ Some(
+ up.child.output.diff(up.ids.get.flatMap(_.references))
Review Comment:
I thought, the following would be annoying:
```
scala> val df = spark.range(5).select($"id", ($"id"*10).as("val"))
scala> df.show()
+---+---+
| id|val|
+---+---+
| 0| 0|
| 1| 10|
| 2| 20|
| 3| 30|
| 4| 40|
+---+---+
df.unpivot(Array($"id" * 2), "col", "val").show()
+--------+---+---+
|(id * 2)|col|val|
+--------+---+---+
| 0|id| 0|
| 0|val| 0|
| 2|id| 1|
| 2|val| 10|
| 4|id 2|
| 4|val| 20|
| 6|id 3|
| 6|val| 30|
| 8|id 4|
| 8|val| 40|
+--------+---+---+
```
Of course, that id manipulation can be done before `unpivot` to
"materialize" it as a reference:
```
df.withColumn("id", $"id" * 2).unpivot(Array($"id"), "col", "val").show()
+---+---+---+
| id|col|val|
+---+---+---+
| 0|val| 0|
| 2|val| 10|
| 4|val| 20|
| 6|val| 30|
| 8|val| 40|
+---+---+---+
```
Happy to remove that complexity.
--
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]