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

Permuted grouped aggregate #1085

Closed
MMcM opened this issue Dec 8, 2020 · 0 comments
Closed

Permuted grouped aggregate #1085

MMcM opened this issue Dec 8, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@MMcM
Copy link
Contributor

MMcM commented Dec 8, 2020

Consider a query along the lines of

SELECT zip, MAX(value) FROM properties WHERE state = 'MA' GROUP BY zip ORDER BY 2 DESC

that aims to return some grouping keys ordered by an aggregate.

An ordinary plan requires unbounded storage of the intermediate aggregates and then a sort of that temporary table.

Some dialects support an interesting twist using the (relationally suspect)

SELECT DISTINCT zip FROM properties WHERE state = 'MA' ORDER BY value DESC

which can use an index on state, value. It produces results right away and needs space for tracking zips already seen.

To do this with precomputed aggregates, two B-trees are needed:

  • An ordinary index on all the group keys and the value (so, state, zip, value).

  • A permuted MAX index with state, MAX(value), zip.

Then for maintenance:

  • An index insert needs to ascertain whether its value is the new maximum and update the permuted entry if so.

    • Assume the ordinary index has not yet been updated (we have no entry there).
    • Read ordinary (state, zip) reverse, limit 1. This is the maximum from another record for the same group and so the current maximum.
    • If this is absent or less than the value to be inserted: if not absent, remove permuted (state, that_value, zip) and insert permuted (state, value, zip).
  • An index remove needs to determine whether its value is the current maximum and update the permuted entry if so.

    • Assume that the ordinary has been updated already (our entry is not there).
    • Read permuted (state, value, zip).
    • If not present, we are not it, stop.
    • Read ordinary (state, zip) reverse, limit 1. This is the maximum from another record for the same group and so the replacement maximum.
    • If this is absent or less than value (it might be equal): remove permuted (state, value, zip) and, if not absent, insert permuted (state, that_value, zip).
@MMcM MMcM added the enhancement New feature or request label Dec 8, 2020
MMcM added a commit to MMcM/fdb-record-layer that referenced this issue Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant