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

Fixes when using aggregates inside Spread Embedded Resources #3693

Merged
merged 4 commits into from
Aug 21, 2024

Conversation

laurenceisla
Copy link
Member

@laurenceisla laurenceisla commented Aug 20, 2024

Missing:

  • Tests
  • Refactor
  • Changelog

These fixes are also needed for #3640

src/PostgREST/Plan.hs Outdated Show resolved Hide resolved
Copy link
Member Author

@laurenceisla laurenceisla left a comment

Choose a reason for hiding this comment

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

For Spread Resources I decided to use COUNT(<row>) instead of COUNT(*). When there are nested spread resources, doing a COUNT(*) may not return the expected values, since it would count all the columns * from the intermediate resources.

For example, for this query:

curl 'localhost:3000/process_supervisor?select=...processes(factory_id,...process_costs(count()))'

The resulting SQL would be:

SELECT "process_supervisor_processes_1"."factory_id",
       COUNT("process_supervisor_processes_1"."count") AS "count"
FROM "test"."process_supervisor"
LEFT JOIN LATERAL
  (SELECT "processes_1"."factory_id",
          "processes_process_costs_2"."count"
   FROM "test"."processes" AS "processes_1"
   LEFT JOIN LATERAL
     (SELECT "process_costs_2" AS "count"
      FROM "test"."process_costs" AS "process_costs_2"
      WHERE "process_costs_2"."process_id" = "processes_1"."id") AS "processes_process_costs_2" ON TRUE
   WHERE "processes_1"."id" = "test"."process_supervisor"."process_id") AS "process_supervisor_processes_1" ON TRUE
GROUP BY "process_supervisor_processes_1"."factory_id"

Using COUNT("process_supervisor_processes_1".*) would not count correctly since there are processes that do not have process_count assigned.

@laurenceisla laurenceisla marked this pull request as ready for review August 21, 2024 00:53
Copy link
Member

@steve-chavez steve-chavez left a comment

Choose a reason for hiding this comment

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

LGTM!

… a field - @laurenceisla

- Fixed "column reference <col> is ambiguous" error when selecting "?select=...table(col,count())"
- Fixed "column <json_aggregate>.<alias> does not exist" error when selecting "?select=...table(aias:count())"
@laurenceisla laurenceisla merged commit ded2e99 into PostgREST:main Aug 21, 2024
22 checks passed
@laurenceisla laurenceisla deleted the fix-spread-agg branch August 21, 2024 21:56
@laurenceisla
Copy link
Member Author

Did not backport the fixes to v12 because the last one depends on a feature/performance commit, so it has a conflict with the code there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants