WangGuangxin opened a new issue, #13739: URL: https://github.com/apache/gluten/issues/13739
### Backend VL (Velox) ### Bug description ### Expected behavior For `rank()` / `dense_rank()`, rows with equal ORDER BY values tie and get the same rank. When the ORDER BY keys are the same as the PARTITION BY keys, every row in a partition has rank 1, so a filter `r <= N` (N >= 1) must keep every row in the partition. ### Actual behavior With the Velox backend, Gluten keeps only N rows per partition for `rank()` and `dense_rank()`, as if the function were `row_number()`. Tied rows are silently dropped and the query returns fewer rows than vanilla Spark. ### Reproducer ```sql WITH src AS (SELECT CAST(id % 2 AS INT) AS g FROM range(0, 24, 1, 4)), -- 12 rows each for g = 0 and 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 ``` | | Result | |---|---| | Vanilla Spark | `(0, 12)`, `(1, 12)` | | Gluten + Velox | `(0, 2)`, `(1, 2)` | `dense_rank()` gives the same wrong result. `row_number()` is correct, returning `(0, 2)`, `(1, 2)`. ### Root cause Spark infers `WindowGroupLimit(partition = [g], order = [g ASC], rank, limit = 2)`. In `SubstraitToVeloxPlanConverter::toVeloxPlan(const WindowGroupLimitRel&)`, sort keys that also appear in the partition keys are removed, because Velox's `TopNRowNumberNode` requires non-empty sorting keys that do not overlap the partition keys. That leaves no sort keys here. Since #7089, the converter then falls back to `RowNumberNode(partitionKeys, limit)` whatever the ranking function is. This is correct for `row_number`, but for `rank` / `dense_rank` it truncates each partition to `limit` rows instead of keeping all the tied rows. The Bolt backend (`cpp/bolt/substrait/SubstraitToBoltPlan.cc`) has the same `RowNumberNode` fallback. ### Affected versions Any Spark version with `WindowGroupLimit` (3.5+) on the Velox backend. ### Gluten version main branch ### Spark version Spark-3.5.x ### Spark configurations _No response_ ### System information _No response_ ### Relevant logs ```bash ``` -- 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]
