JasonShin / sqlx-ts

node.js compile-time SQL validations & type generations
https://jasonshin.github.io/sqlx-ts/
MIT License
181 stars 6 forks source link

RANK() OVER (PARTITION BY.. results in thread 'main' panicked at ... called `Option::unwrap()` on a `None` value #104

Open simplenotezy opened 6 months ago

simplenotezy commented 6 months ago

Using Rank() OVER (PARTITION...) results in this error:

thread 'main' panicked at src/ts_generator/sql_parser/expressions/translate_expr.rs:163:61:
called `Option::unwrap()` on a `None` value

Full query:

WITH cte AS (
  SELECT
    s.title,
    sg.genre,
    RANK() OVER (PARTITION BY sg.genre ORDER BY s.title) AS rank
  FROM
    show s
  JOIN
    show_genre sg ON s.id = sg.show_id
  WHERE
    sg.genre IN ('Action', 'Adventure')
)
SELECT
  title,
  genre
FROM
  cte
WHERE
  rank <= 10;
simplenotezy commented 6 months ago

A workaround is to create a second cte and do the WHERE rank... filtering there, and then select from this new CTE:

WITH cte AS (
  SELECT
    s.title,
    sg.genre,
    RANK() OVER (PARTITION BY sg.genre ORDER BY s.title) AS rank
  FROM
    show s
  JOIN
    show_genre sg ON s.id = sg.show_id
  WHERE
    sg.genre IN ('Action', 'Adventure')
),
filtered_cte AS (
  SELECT
    title,
    genre
  FROM
    cte
  WHERE
    rank <= 10
)
SELECT * FROM filtered_cte
JasonShin commented 6 months ago

This is interesting, thanks. I actually haven't considered CTE yet but that's quite common in MySQL 8 and PG. Will take a look shortly with the other issues