Skip to content

Conversation

@soupi
Copy link
Contributor

@soupi soupi commented Jun 6, 2024

What

fixes https://hasurahq.atlassian.net/browse/PG-112

Previously, order by and limit clauses were not applied for the aggregates part of the query as the spec said it was not needed, however, that is a mistake which we now fix.

How

To fix this, we unify the way we handle rows and aggregates.
Callers will no longer need to call translate_rows_query and translate_aggregates_query separately and then try to stitch them together, but rather call one part, translate_query, which will call both.

Each translate part will call the same function, translate_query_part which translates the common elements (where, order by, limit, etc.), but will different in the way it handles fields or aggregates.

This way, we make sure that an ndc-spec query is translated consistently between the two parts - rows and aggregates.

// fields
let returning_select = match &query.fields {
Some(_) => {
let (_, returning_select) = crate::translation::query::root::translate_rows_query(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of calling translate_rows_query and translate_aggregate_query, and stitch everything together, we call translate_query which does all of this for us.

/// affected, but it must return at least one. A `SelectSet` describes this as a type, so we can
/// convert an optional returning `Select` and an optional aggregate `Select` to a `SelectSet`,
/// failing if neither exists.
fn rows_and_aggregates_to_select_set(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in translate_query.

// fields
let returning_select = match &query.fields {
Some(_) => {
let (_, returning_select) = crate::translation::query::root::translate_rows_query(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of calling the function and then set the from clause to the returned select outside of it, we just pass the from clause into the function and have it set it.


/// Translate a query to sql ast.
/// We return a SELECT for the 'rows' field and a SELECT for the 'aggregates' field.
pub fn translate_query(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rewritten a bit and moved to ::root.

&join_field.query,
)?;

// add join expressions to row / aggregate selects
Copy link
Contributor Author

Choose a reason for hiding this comment

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

injecting the join predicate means we don't need to unpack afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Test that limit and order by has been applied to the aggregates query.

Comment on lines +39 to +42
ORDER BY
"%1_Invoice"."InvoiceId" ASC
LIMIT
10 OFFSET 5
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, has been applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now we apply the limit. We also changed the test a bit so it is easy to verify.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the test request did not contain order for the album, so it was ambigious. fixed.

@soupi soupi changed the title Gil/unify agg rows query Unify query part handling for rows and aggregates Jun 6, 2024
Gil Mizrahi added 2 commits June 6, 2024 14:59
@soupi soupi marked this pull request as ready for review June 6, 2024 12:06
@soupi soupi enabled auto-merge June 6, 2024 15:22
@soupi soupi added this pull request to the merge queue Jun 6, 2024
Merged via the queue into main with commit 7ee9fbb Jun 6, 2024
@soupi soupi deleted the gil/unify-agg-rows-query branch June 6, 2024 15:31
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