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 : group by on functional expressions in mongo #200

Merged
merged 3 commits into from
Jul 2, 2024

Conversation

RishabhB99
Copy link
Contributor

@RishabhB99 RishabhB99 commented Jun 28, 2024

Description

Bug fix for grouping on functional expressions

Testing

Added a test for the same scenario

Checklist:

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

Documentation

Make sure that you have documented corresponding changes in this repository or hypertrace docs repo if required.

"function": 3.0,
"functionCount": 2
}
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Empty line at the end.

@@ -145,6 +150,20 @@ public static ReturnDocument getReturnDocument(final ReturnDocumentType returnDo
String.format("Unhandled return document type: %s", returnDocumentType)));
}

public static boolean isFunctionExpressionSelectionWithGroupBy(
Copy link
Contributor

Choose a reason for hiding this comment

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

Utils is only for very specific and common things. Can we move this method out of the utils?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is used at 2 places at MongoGroupTypeExpressionParser and MongoSelectTypeExpressionParser
so we'll need to define it at some common place, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense to put them in a common place, but doesn't feel like "Utils" is the right place. Either we could have a shared class (non-static) or have the definition in one place and access it in another.

final SelectionSpec selectionSpec, final List<String> groupByAliases) {
return selectionSpec.getAlias() != null
&& groupByAliases.contains(selectionSpec.getAlias())
&& selectionSpec.getExpression().getClass().equals(FunctionExpression.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please avoid checking class instances like this. Refer classes like IdentifierChecker, etc.

public static List<String> getGroupByAliases(final List<GroupTypeExpression> expressions) {
return expressions.stream()
.filter(expression -> expression.getClass().equals(IdentifierExpression.class))
.map(expression -> ((IdentifierExpression) expression).getName())
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you avoid explicit instance of/class equality checks and casting by leveraging the visitor pattern?

functionExpressionSelectionWithGroupBys.stream()
.map(spec -> MongoGroupTypeExpressionParser.parse(parser, spec))
.reduce(
new LinkedHashMap<>(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason for a linked hashmap? Is the order important here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no reason, i just copy pasted the reduce used below
don't think the order is important

@@ -99,4 +128,15 @@ private Map<String, Object> parse(final GroupTypeExpression expression) {
MongoGroupTypeExpressionParser parser = new MongoGroupTypeExpressionParser();
return expression.accept(parser);
}

private static List<SelectionSpec> getFunctionExpressionSelectionWithGroupBys(
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is this getting used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RishabhB99 RishabhB99 requested a review from suresh-prakash July 2, 2024 06:04

@Override
public String visit(FunctionExpression expression) {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to throw exception instead of null.
Or, alternatively return optional of string to avoid NullPointerExceptions.

@RishabhB99 RishabhB99 requested a review from suresh-prakash July 2, 2024 10:25
Comment on lines +158 to +159
.filter(Optional::isPresent)
.map(Optional::get)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can also be simplified to.

Suggested change
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Optional::stream)

Copy link

codecov bot commented Jul 2, 2024

Codecov Report

Attention: Patch coverage is 97.56098% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.65%. Comparing base (43b12e8) to head (948d214).

Files Patch % Lines
.../core/documentstore/parser/GroupByAliasGetter.java 66.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #200      +/-   ##
============================================
+ Coverage     79.45%   79.65%   +0.20%     
- Complexity     1003     1013      +10     
============================================
  Files           193      194       +1     
  Lines          4769     4807      +38     
  Branches        399      402       +3     
============================================
+ Hits           3789     3829      +40     
+ Misses          698      696       -2     
  Partials        282      282              
Flag Coverage Δ
integration 79.65% <97.56%> (+0.20%) ⬆️
unit 57.18% <58.53%> (+<0.01%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@suresh-prakash suresh-prakash merged commit c7ea09e into hypertrace:main Jul 2, 2024
6 checks passed
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