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

fix(pat-5034): include order-by expressions when computing the group-by clause #208

Merged
merged 3 commits into from
Dec 19, 2023

Conversation

ajmasci
Copy link
Contributor

@ajmasci ajmasci commented Dec 18, 2023

  • Include all order-by expressions that are not present in the
    selection set when computing the group-by clause.

Test plan

  • Added unit tests.

Copy link

graphite-app bot commented Dec 18, 2023

Your org has enabled the Graphite merge queue for merging into main

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

You can enable merging using labels in your Graphite merge queue settings.

Copy link
Contributor

@spencerwilson spencerwilson left a comment

Choose a reason for hiding this comment

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

🚢 once my q is resolved

...(selection ?? []),
...(orderBy ?? [])
.map((x: Ordering) => x[0])
.filter((x) => !selectionMap.has(x)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can selectionMap.has(x) ever return true? Given how value equality works in Sets, which is pretty much like ===, it's not clear to me that the FieldExprs in selection could ever === the FieldExprs in the array that the filter is called on here.

Could you add a test that has equivalent, non-aggregateFieldExprs in the select and orderby, and see if they get deduped properly in the GroupbyList?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, the test case with avgPrice as the first order-by expression (see my comment below), results in a true value returned by selectionMap.has, and the group-by is correctly de-duped. Is there any reason that you think a non-aggregate field in the order-by that matches a selection expression will behave differently?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ajith's idea: Two derived fields would be unlikely to match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good callout here @spencerwilson ! The derived field test did reveal the bug. Fixed it by using the field name in the selectionSet.

const query = table
.select('id', 'name', price.avg().as('avgPrice'))
.filter(name.like('%bah%').and(createdOn.before(new Date('2023-01-01'))))
.orderBy(['avgPrice', 'DESC'], [createdOn, 'ASC']) // Note that createdOn is not in the seletion.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@spencerwilson , note the first orderBy expression. It will be mapped to the selection expression aliased to avgPrice. The group-by has no dupe for this expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In any case, I'll include a non-agg that matches a selection in the orderBy.

const grouping = expandedSelection.filter(
(fieldExpr) => !(fieldExpr instanceof AggregateFieldExpr)
);
if (grouping) {
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI grouping will always be truthy (though it may have grouping.length === 0)

Copy link

graphite-app bot commented Dec 19, 2023

Merge activity

@graphite-app graphite-app bot merged commit 796aeb8 into main Dec 19, 2023
10 checks passed
Copy link

graphite-app bot commented Dec 19, 2023

Merge activity

@graphite-app graphite-app bot deleted the ajith/pat-5034-order-by-non-aggs-in-group-by branch December 19, 2023 22:34
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.

None yet

2 participants