Skip to content

Commit

Permalink
#145: Allow /count to work with distinct filters
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Sep 29, 2020
1 parent 5fd2622 commit 86c6f27
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 9 additions & 1 deletion common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ def __init__(self, fields):
def apply_filter(self, query):
try:
log.info("Adding ICAT distinct filter to ICAT query")
query.setAggregate("DISTINCT")
if (
query.aggregate == "COUNT"
or query.aggregate == "AVG"
or query.aggregate == "SUM"
):
# Distinct can be combined with other aggregate functions
query.setAggregate(f"{query.aggregate}:DISTINCT")
else:
query.setAggregate("DISTINCT")

# Using where filters to identify which fields to apply distinct too
for field in self.fields:
Expand Down
9 changes: 6 additions & 3 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ def execute_query(self, client, return_json_formattable=False):

flat_query_includes = self.flatten_query_included_fields(self.query.includes)
mapped_distinct_fields = None

# If the query has a COUNT function applied to it, some of these steps can be
# skipped
count_query = True if "COUNT" in self.query.aggregate else False

if self.query.aggregate == "DISTINCT":
if self.query.aggregate == "DISTINCT" and not count_query:
log.info("Extracting the distinct fields from query's conditions")
# Check query's conditions for the ones created by the distinct filter
distinct_attributes = self.iterate_query_conditions_for_distinctiveness()
Expand All @@ -182,8 +186,7 @@ def execute_query(self, client, return_json_formattable=False):
for result in query_result:
log.debug(f"Aggregate: {self.query.aggregate}")
# TODO - How to deal with distinct and count as aggregate
if self.query.aggregate != "COUNT":

if not count_query:
dict_result = self.entity_to_dict(
result, flat_query_includes, mapped_distinct_fields
)
Expand Down

0 comments on commit 86c6f27

Please sign in to comment.