Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

row_number() calculation does not agree with sort order when there are ties and there is a nested view #1814

Open
christopherswenson opened this issue Aug 8, 2024 · 0 comments
Assignees
Labels
L2 Modest change, bug fix, or refactor language An issue in the Malloy language

Comments

@christopherswenson
Copy link
Contributor

In a query like:

run: something -> {
  group_by: thing
  aggregate: stuff
  order_by: stuff
  nest: cool_chart
  calculate: n is row_number()
}

We generate SQL like:

WITH __stage0 AS (
  SELECT
    group_set,
    ... as "stuff__0"
    ... row_number() ... as "n__0"
  FROM (...)
  GROUP BY ...
)
SELECT
  ... as "stuff",
  ... as "n"
FROM __stage0
GROUP BY 1
ORDER BY "stuff" desc

If there is a tie between rows A and B in the stuff field, the row_number() can be calculated with A before B in __stage0, then ordered with B before A in the outer select. This results in something like:

  • thing: "C" stuff: 100, cool_chart: ..., n: 1
  • thing: "D" stuff: 90, cool_chart: ..., n: 2
  • thing: "B" stuff: 80, cool_chart: ..., n: 4
  • thing: "A" stuff: 70, cool_chart: ..., n: 3

This does not happen without a nest in the query because it only generates a one-stage query.

@christopherswenson christopherswenson added the language An issue in the Malloy language label Aug 8, 2024
@christopherswenson christopherswenson added the L2 Modest change, bug fix, or refactor label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L2 Modest change, bug fix, or refactor language An issue in the Malloy language
Projects
None yet
Development

No branches or pull requests

2 participants