adith-os opened a new pull request, #60405:
URL: https://github.com/apache/spark/pull/60405

   ### What changes were proposed in this pull request?
   In Spark Classic, `Dataset.rollup`, `Dataset.cube` and 
`Dataset.groupingSets` now turn top-level integer literals in their grouping 
expressions into GROUP BY ordinals when `spark.sql.groupByOrdinal` is enabled, 
as `Dataset.groupBy` already does. The conversion is moved into one private 
helper shared by the four methods.
   
   ### Why are the changes needed?
   SPARK-51820 (#50606) moved ordinal construction out of the analyzer, but in 
Classic only `Dataset.groupBy` got the conversion. The SQL parser and 
`SparkConnectPlanner` convert literals for every grouping type. So since 4.1.0, 
Classic treats an integer literal in `rollup`/`cube`/`groupingSets` as a 
constant, which changes results and accepts out-of-range positions.
   
   ```scala
   val df = spark.range(5).toDF("id").withColumn("g", col("id") % 2)
   df.cube(col("g"), lit(1)).count()  // before: 6 rows (constant); after: 7 
rows, same as 4.0, Connect and SQL `CUBE(g, 1)`
   df.cube(lit(-28)).count()          // before: returns rows; after: 
GROUP_BY_POS_OUT_OF_RANGE
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   Yes. With `spark.sql.groupByOrdinal=true` (the default), integer literals 
passed to `rollup`, `cube` and `groupingSets` in Spark Classic are GROUP BY 
positions again, as in Spark 4.0, Spark Connect, SQL and `Dataset.groupBy`. 
Since 4.1.0 they were constants. Setting `spark.sql.groupByOrdinal=false` keeps 
a literal constant, and also turns off ordinals in `groupBy` and SQL.
   
   ### How was this patch tested?
   Added `SPARK-60750: Rollup, cube and grouping sets by ordinal - Dataframe` 
to `ReplaceIntegerLiteralsWithOrdinalsDataframeSuite`. 
   For each method, with `spark.sql.groupByOrdinal` on and off, it checks that 
the DataFrame result equals the equivalent SQL `GROUP BY` query. It also checks 
`GROUP_BY_POS_OUT_OF_RANGE` for `cube(lit(-1))`, and `GROUP_BY_POS_AGGREGATE` 
for `cube(lit(1))` with `spark.sql.retainGroupColumns=false`. Without the fix, 
the test fails.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Claude Code (Claude Sonnet 5.5)


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