Skip to content

Conversation

@NickCrews
Copy link
Contributor

Before, we only dropped constant sort values if

  • it was an EXACT Literal. if it was derived, eg ibis.literal(5) + 1, then we wouldn't.
  • if the merge_select_select decided to return the original select instead of merging them (eg because of a window function, unnest, etc), then we wouldn't see this optimization.

This is a followup to a TODO that I found at 7a6f0a1#diff-2719b402427dca65945905bfc259790600013287258e555367581729c40d837eR323-R328

@github-actions github-actions bot added tests Issues or PRs related to tests sql Backends that generate SQL labels Sep 13, 2025
*
FROM "star1" AS "t0"
ORDER BY
RANDOM() ASC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there now a subquery? Is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see how the actual expression changed here? Before the expression was just t.order_by(ibis.random()). Now it is the more complicated t.order_by(ibis.random()).select("f", "c").order_by(ibis.random()).

This new version is equivalent to t.select("f", "c").order_by(ibis.random()) which is SELECT "t0"."f", "t0"."c" FROM "star1" AS "t0" ORDER BY RANDOM() ASC. For this simple case we could drop the internal order by, and then merge the two selects. But my initial gut reaction is that there are cases where the internal select result depends on the internal order by, and so in general we can't just drop the inner select? So in general I would say this subquery is unnecessary, but correct.

Before, we only dropped constant sort values if
- it was an EXACT Literal. if it was derived, eg `ibis.literal(5) + 1`, then we wouldn't.
- if the merge_select_select decided to return the original select instead of merging them (eg because of a window function, unnest, etc), then we wouldn't see this optimization.
@NickCrews NickCrews force-pushed the improve-drop-constant-sort-keys branch from dcb629e to 25fab30 Compare October 16, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql Backends that generate SQL tests Issues or PRs related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants