Skip to content

fix: omit cross-shard aggregate helpers on single-shard queries - #1474

Open
murex971 wants to merge 2 commits into
pgdogdev:mainfrom
murex971:fix-direct-aggregate-helpers
Open

fix: omit cross-shard aggregate helpers on single-shard queries#1474
murex971 wants to merge 2 commits into
pgdogdev:mainfrom
murex971:fix-direct-aggregate-helpers

Conversation

@murex971

@murex971 murex971 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Cross-shard AVG/STDDEV/VARIANCE rewrites add __pgdog_* helper columns before routing. When the query later hits a single shard, those helpers were still sent to Postgres and shown into the client result.
  • Keep the original SELECT as direct_stmt and restore it after routing when the route is not cross-shard (simple Query and Parse/prepared statements). Cross-shard queries still use the helper rewrite.
  • closes [Cross shard] temporary columns for stddev() visible in client response #1425

@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.62162% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...rc/frontend/router/parser/rewrite/statement/mod.rs 78.57% 3 Missing ⚠️
...tend/client/query_engine/test/rewrite_aggregate.rs 98.48% 1 Missing ⚠️
...outer/parser/rewrite/statement/aggregate/engine.rs 97.14% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@murex971
murex971 requested a review from levkk September 2, 2026 07:09
unreachable!("statement was checked to be SELECT");
};
self.rewrite_aggregates(&mut select, mem, &mut plan, self.db_schema)?;
if !plan.aggregates.is_noop() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it would cool to have a rollback_aggregates function (should be pretty easy to remove the helper columns we added) and only then call deparse. That way, queries without aggregates bypass the deparse step and remain fast!

offset,
direct_stmt,
} => {
if request.is_executable()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I wouldn't mind moving this to apply_after_parser (maybe its own function), to keep this code clean. The apply_after_parser also has better context on how to rewrite requests correctly (since apply is adjacent too), so there is more opportunity for code re-use.

@levkk

levkk commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

I think just a couple of small changes needed. Longer term we would want to not run the aggregate rewriter at all if the query is direct to shard, but that requires a bigger refactor (parse before rewrite, not rewrite before parse like we do now).

@murex971

murex971 commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

yeah, ideal architecture would be first decide query route then rewrite, but it would have been a bigger refactor

@murex971
murex971 requested a review from levkk September 2, 2026 17:19
@levkk

levkk commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

👀

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.

[Cross shard] temporary columns for stddev() visible in client response

2 participants