vranes opened a new pull request, #60758:
URL: https://github.com/apache/spark/pull/60758

   ### What changes were proposed in this pull request?
   
   `BinByExec` builds one projection for each bin and one for a NULL range. 
Both projections chose the DISTRIBUTE columns to scale with 
`distributeColumns.map(bindOrdinal)`. `bindOrdinal` returns the position of the 
first matching child column, so only the first copy of a DISTRIBUTE column was 
scaled.
   
   This PR makes both projections check each child column with 
`distributeReplacements.contains(attr)`, which is the same map that 
`BinByExec.output` uses. The whole-stage codegen path and the iterator path use 
the same projections, so the fix applies to both.
   
   ### Why are the changes needed?
   
   When the input of BIN BY outputs a DISTRIBUTE column more than once, for 
example `SELECT *, value`, `BinByExec.output` maps every copy to the scaled 
column. But only the first copy was scaled. The other copies kept the input 
value, and for a NULL range they were not set to NULL. A reader that reads the 
physical rows by position gets these wrong values. For example:
   
   ```sql
   SELECT * FROM (SELECT *, value FROM VALUES
     (TIMESTAMP '2024-01-01 00:00:00', TIMESTAMP '2024-01-01 00:10:00', 100.0D)
     AS t(ts_start, ts_end, value))
   BIN BY (
     RANGE ts_start TO ts_end BIN WIDTH INTERVAL '5' MINUTE
     ALIGN TO TIMESTAMP '2024-01-01 00:00:00' DISTRIBUTE UNIFORM (value))
   ```
   
   Each of the two bins had `50.0` in the first copy of `value` and `100.0` in 
the second. Now both copies are `50.0`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a bug fix. BIN BY is off by default behind the internal config 
`spark.sql.binByRelationOperator.enabled`. With BIN BY enabled, every copy of a 
DISTRIBUTE column is now scaled, or NULL for a NULL range. Before, only the 
first copy was.
   
   ### How was this patch tested?
   
   Added "BIN BY scales every copy of a DISTRIBUTE column" to `BinBySuite`. It 
reads the physical rows by position. It checks both copies of a DISTRIBUTE 
column for a range that spans two bins and for a NULL range, and it checks that 
a new alias of the column (`value AS raw_value`) is not scaled. It also checks 
that no projection sits above BIN BY and which BIN BY path ran. 
`BinByWithoutCodegenSuite` runs the same test on the iterator path. The test 
fails without the fix.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


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