WangGuangxin opened a new pull request, #13738:
URL: https://github.com/apache/gluten/pull/13738

   ## What changes are proposed in this pull request?
   
   When converting `WindowGroupLimitRel` to a Velox plan, Gluten drops sort 
keys that also appear in the partition keys, because Velox's 
`TopNRowNumberNode` requires sorting keys to be non-empty and disjoint from 
partition keys. If every sort key is a partition key (e.g. `rank() OVER 
(PARTITION BY g ORDER BY g)`), the sort key list becomes empty, and since 
apache/gluten#7089 the converter falls back to a `RowNumberNode` with the limit 
regardless of the ranking function.
   
   That is correct for `ROW_NUMBER`, but wrong for `RANK` and `DENSE_RANK`: 
with no effective sort key, every row in a partition ties at rank 1, so a 
filter such as `r <= 2` must keep the whole partition. The `RowNumberNode` 
truncated each partition to `limit` rows and dropped tied rows, returning fewer 
rows than vanilla Spark.
   
   ### Example
   
   ```sql
   WITH src AS (SELECT CAST(id % 2 AS INT) AS g FROM range(0, 24, 1, 4)),  -- 
12 rows with g = 0, 12 rows with g = 1
   ranked AS (SELECT g, rank() OVER (PARTITION BY g ORDER BY g) AS r FROM src)
   SELECT g, COUNT(*) FROM ranked WHERE r <= 2 GROUP BY g
   ```
   
   Spark infers `WindowGroupLimit(partition = [g], order = [g ASC], rank, limit 
= 2)` and pushes it below the window. Inside each partition all rows have the 
same `g`, so all 12 rows have `rank = 1` and all of them satisfy `r <= 2`.
   
   Gluten removes the sort key `g` because it is also a partition key, leaving 
no sort keys, and then converts the operator as follows:
   
   | | Velox node for `WindowGroupLimit` | Result |
   |---|---|---|
   | Vanilla Spark | — | `(0, 12)`, `(1, 12)` |
   | Before this PR | `RowNumberNode(partition = [g], limit = 2)`: keeps the 
first 2 rows of each `g` | `(0, 2)`, `(1, 2)` ❌ |
   | After this PR | identity `ProjectNode`: keeps every row | `(0, 12)`, `(1, 
12)` ✅ |
   
   `dense_rank` behaves the same as `rank`. With `row_number`, the expected 
result is `(0, 2)`, `(1, 2)`, which `RowNumberNode` already returns, so that 
path is unchanged.


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