Skip to main content

eliminate_distinct_on

Function eliminate_distinct_on 

Source
pub fn eliminate_distinct_on(expr: Expression) -> Result<Expression>
Expand description

Eliminate DISTINCT ON clause by converting to a subquery with ROW_NUMBER

DISTINCT ON is PostgreSQL-specific. For dialects that don’t support it, this converts it to a subquery with a ROW_NUMBER() window function.

Converts:

SELECT DISTINCT ON (a) a, b FROM t ORDER BY a, b

To:

SELECT a, b FROM (
    SELECT a, b, ROW_NUMBER() OVER (PARTITION BY a ORDER BY a, b) AS _row_number
    FROM t
) _t WHERE _row_number = 1

Reference: transforms.py:138-191