-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: add support for query that have aggregation but missing group by #124
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fedaeff
fix: adding support for default group by
kotharironak c972de7
Merge remote-tracking branch 'origin/main' into handle-groupby
kotharironak 4414858
trying out with different aggregation w/o group by
kotharironak 46d4491
adding distinct count query w/o group by
kotharironak 98ba135
feat: add query tranformer for handling selection
kotharironak e25529a
add tests for corrosponding scenarios
kotharironak 72e74e0
Merge branch 'main' into handle-groupby
kotharironak 066f8ee
applied spotless
kotharironak 91710ec
Merge remote-tracking branch 'origin/handle-groupby' into handle-groupby
kotharironak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
document-store/src/integrationTest/resources/mongo/test_aggr_only_with_fliter_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
{ | ||
"qty_count":3 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...hypertrace/core/documentstore/postgres/query/v1/transformer/PostgresQueryTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.hypertrace.core.documentstore.postgres.query.v1.transformer; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.hypertrace.core.documentstore.query.Query; | ||
import org.hypertrace.core.documentstore.query.transform.QueryTransformer; | ||
|
||
public class PostgresQueryTransformer { | ||
|
||
// Transform the query in the listed below order | ||
private static final List<QueryTransformer> TRANSFORMERS = | ||
new ImmutableList.Builder<QueryTransformer>() | ||
.add(new PostgresSelectionQueryTransformer()) | ||
.build(); | ||
|
||
public static Query transform(final Query query) { | ||
Query transformedQuery = query; | ||
|
||
for (QueryTransformer transformer : TRANSFORMERS) { | ||
transformedQuery = transformer.transform(transformedQuery); | ||
} | ||
|
||
return transformedQuery; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...e/core/documentstore/postgres/query/v1/transformer/PostgresSelectionQueryTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.hypertrace.core.documentstore.postgres.query.v1.transformer; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.hypertrace.core.documentstore.expression.impl.AggregateExpression; | ||
import org.hypertrace.core.documentstore.expression.impl.ConstantExpression; | ||
import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; | ||
import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; | ||
import org.hypertrace.core.documentstore.parser.SelectTypeExpressionVisitor; | ||
import org.hypertrace.core.documentstore.query.Query; | ||
import org.hypertrace.core.documentstore.query.SelectionSpec; | ||
import org.hypertrace.core.documentstore.query.transform.QueryTransformer; | ||
import org.hypertrace.core.documentstore.query.transform.TransformedQueryBuilder; | ||
|
||
/* | ||
* Postgres doesn't support the selection of attributes and aggregation w/o group by expression. | ||
* e.g | ||
* SELECT COUNT(DISTINCT document->>'quantity' ) AS QTY, document->'price' AS price | ||
* FROM testCollection | ||
* WHERE (CAST (document->>'price' AS NUMERIC) <= 10) | ||
* | ||
* So, if group by clause is missing, and selection contains any aggregation expression, | ||
* this transformer removes all the non-aggregated expressions. So, the above query will be transformed | ||
* to: | ||
* | ||
* SELECT COUNT(DISTINCT document->>'quantity' ) AS QTY | ||
* FROM testCollection | ||
* WHERE (CAST (document->>'price' AS NUMERIC) <= 10) | ||
* | ||
* This is the similar behavior supported in our other document store implementation (e.g Mongo) | ||
* */ | ||
public class PostgresSelectionQueryTransformer | ||
implements QueryTransformer, SelectTypeExpressionVisitor { | ||
|
||
@Override | ||
public Query transform(Query query) { | ||
// no-op if group by clause exits | ||
if (!query.getAggregations().isEmpty()) return query; | ||
|
||
// check for all selections, remove non-aggregated selections. | ||
List<SelectionSpec> finalSelectionSpecs = | ||
query.getSelections().stream() | ||
.filter(selectionSpec -> selectionSpec.getExpression().accept(this)) | ||
.collect(Collectors.toUnmodifiableList()); | ||
|
||
return finalSelectionSpecs.size() > 0 | ||
? new TransformedQueryBuilder(query).setSelections(finalSelectionSpecs).build() | ||
: query; | ||
} | ||
|
||
@Override | ||
public Boolean visit(AggregateExpression expression) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Boolean visit(ConstantExpression expression) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Boolean visit(FunctionExpression expression) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Boolean visit(IdentifierExpression expression) { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we should go this route. We have never transformed the user-given query to a different non-equivalent query just because some database does not support it. In fact, this transformation alters the input selections and removes some of them. This might result in unexpected/undesired effects in the clients because they are asking for 2 selections, but, we only return 1 silently ignoring the other.
The query transformer in Mongo only builds equivalent queries by modifying the given expressions to other equivalent forms or adds some expressions to support the modification. But, the overall query is transformed to another equivalent query supported by the database. We neither remove anything nor transform to a non-equivalent query. Ideally, for such scenarios, we should fail (even in Mongo if that's not the case today). I suspect, the DB itself is not returning the rows in case of Mongo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, in Mongo we convert "DISTINCT_COUNT" into "$addToSet" in the "$group" stage and add "$size" in the "$project" stage. But, the query is still equivalent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mongo internally does that and discards them. And, thought of the above path, but have to do this for backward compatibility as currently in the Query API layer, we are not discarding those queries.
e.g Mongo Query for the above sample query
Response to the above query from mongo (selections are discarded):