-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for query that have aggregation but missing group by (
#124) * fix: adding support for default group by * trying out with different aggregation w/o group by * adding distinct count query w/o group by * feat: add query tranformer for handling selection * add tests for corrosponding scenarios * applied spotless
- Loading branch information
1 parent
a42561f
commit 8b831d6
Showing
7 changed files
with
238 additions
and
24 deletions.
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.