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: handle group_by identifier more generically #127

Merged
merged 4 commits into from
Sep 5, 2022
Merged

Conversation

kotharironak
Copy link
Contributor

The previous fix - #124 for the issue #123, was handling a very specific case.

As part of this PR,

  • it makes it more generic, and make sure that all the groupby clause identifier match with selection clause identifiers.

Testing:

  • all existing tests are passing
  • added new unit + integration test

Example of handling same in existing mongo impl:
Before transformation:

SELECT DISTINCT_COUNT(`quantity`) AS qty_count, `item`, `price` FROM <implicit_collection> WHERE `price` <= 10 GROUP BY `item`

After transformation:

SELECT DISTINCT(`quantity`) AS qty_count, `_id.item` AS item, `price`, LENGTH(`qty_count`) AS qty_count FROM <implicit_collection> WHERE `price` <= 10 GROUP BY `item`

Mongo Query:

[{"$match": {"price": {"$lte": 10}}}, 
{"$group": {"qty_count": {"$addToSet": "$quantity"}, "_id": {"item": "$item"}}}, 
{"$project": {"item": "$_id.item", "qty_count": {"$size": "$qty_count"}, "price": 1}}]

Response:

[{"item":"Soap","qty_count":2},{"item":"Comb","qty_count":2},{"item":"Shampoo","qty_count":2}]

@github-actions

This comment has been minimized.

@codecov
Copy link

codecov bot commented Sep 5, 2022

Codecov Report

Merging #127 (d5e3360) into main (30c69aa) will increase coverage by 0.10%.
The diff coverage is 90.62%.

@@             Coverage Diff              @@
##               main     #127      +/-   ##
============================================
+ Coverage     78.88%   78.99%   +0.10%     
- Complexity      564      566       +2     
============================================
  Files            92       92              
  Lines          2770     2794      +24     
  Branches        268      271       +3     
============================================
+ Hits           2185     2207      +22     
- Misses          424      426       +2     
  Partials        161      161              
Flag Coverage Δ
integration 78.99% <90.62%> (+0.10%) ⬆️
unit 49.42% <90.62%> (+0.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...transformer/PostgresSelectionQueryTransformer.java 91.89% <90.62%> (-0.42%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

.setFilter(
RelationalExpression.of(
IdentifierExpression.of("price"), LTE, ConstantExpression.of(10)))
.addAggregation(IdentifierExpression.of("item"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Add group by on price as well, because it's a part of the selection?

List<SelectionSpec> finalSelectionSpecs =
query.getSelections().stream()
.filter(selectionSpec -> selectionSpec.getExpression().accept(this))
.filter(
selectionSpec -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we keep the lambda definitions small by introducing private method to improve readability?

public Boolean visit(AggregateExpression expression) {
return true;
}
private static class LocalSelectTypeAggregationExpressionSelector
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is just returning the instance type, do need a separate visitor hierarchy? Could we just do using instance of?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think, this is a more readable pattern.

@Override
public Boolean visit(FunctionExpression expression) {
return false;
private static class LocalSelectTypeIdentifierExpressionSelector
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

@Override
public Boolean visit(IdentifierExpression expression) {
return false;
private static class LocalGroupByIdentifierExpressionSelector
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

RelationalExpression.of(
IdentifierExpression.of("price"), LTE, ConstantExpression.of(10)))
.addAggregation(IdentifierExpression.of("item"))
.build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Add price aggregation?

String sql = postgresQueryParser.parse();

assertEquals(
"SELECT COUNT(DISTINCT document->>'quantity' ) AS \"qty_count\", "
Copy link
Contributor

Choose a reason for hiding this comment

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

It confuses to see where the price selection is lost!
Then, I remembered the transformation logic. Anyway, that'll come up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed offline.

suresh-prakash
suresh-prakash previously approved these changes Sep 5, 2022
@github-actions

This comment has been minimized.

@kotharironak kotharironak merged commit 1306d65 into main Sep 5, 2022
@kotharironak kotharironak deleted the groupby-fix branch September 5, 2022 05:43
@github-actions
Copy link

github-actions bot commented Sep 5, 2022

Unit Test Results

  16 files  ±0    16 suites  ±0   6s ⏱️ -1s
121 tests +1  121 ✔️ +1  0 💤 ±0  0 ❌ ±0 
275 runs  +3  275 ✔️ +3  0 💤 ±0  0 ❌ ±0 

Results for commit 1306d65. ± Comparison against base commit 30c69aa.

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