Skip to content

Remove unnecessary DISTINCT ON expressions from introspection SQL. #260

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

Merged
merged 1 commit into from
Jan 18, 2024

Conversation

SamirTalwar
Copy link
Contributor

What

There are two:

  1. selecting only one relation per name
  2. selecting only one aggregate function per input type

These are both unnecessary.

This work was inspired by CockroachDB failing to respect ordering within CTEs.

How

The first, selecting only one relation per name, was there to avoid having to deal with multiple relations of the same name in distinct schemas, but we now support schemas (and prefix all relations with their schema name), so this is unnecessary and possibly dangerous.

The second, selecting only one aggregate function per input type, is not necessary because we only support aggregate functions which take a single argument, and therefore there can only be one per input type anyway, i.e. you cannot have two aggregate functions of the same name with the same input type(s) and different return types (return type polymorphism) in PostgreSQL.

We therefore simplify these queries to avoid having to use DISTINCT ON at all.

**What:**

There are two:

1. selecting only one relation per name
2. selecting only one aggregate function per input type

These are both unnecessary.

This work was inspired by CockroachDB failing to respect ordering within
CTEs.

**How:**

The first, selecting only one relation per name, was there to avoid
having to deal with multiple relations of the same name in distinct
schemas, but we now support schemas (and prefix all relations with their
schema name), so this is unnecessary and possibly dangerous.

The second, selecting only one aggregate function per input type, is not
necessary because we only support aggregate functions which take a
single argument, and therefore there can only be one per input type
_anyway_, i.e. you cannot have two aggregate functions of the same name
with the same input type(s) and different return types (return type
polymorphism) in PostgreSQL.

We therefore simplify these queries to avoid having to use `DISTINCT ON`
at all.

Co-Authored-By: Philip Lykke Carlsen <[email protected]>
@plcplc plcplc added this pull request to the merge queue Jan 18, 2024
Merged via the queue into main with commit c702546 Jan 18, 2024
@plcplc plcplc deleted the samirtalwar/remove-distinct-on branch January 18, 2024 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants