Skip to content

Commit

Permalink
#141: Add manual_count flag
Browse files Browse the repository at this point in the history
- Flag used for a count request that has a distinct filter
  • Loading branch information
MRichards99 committed Apr 12, 2021
1 parent 5f11db2 commit abb31bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 14 additions & 5 deletions datagateway_api/common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,22 @@ def apply_filter(self, query):
log.info("Adding ICAT distinct filter to ICAT query")
log.debug("Fields for distinct filter: %s", self.fields)

if (
query.aggregate == "COUNT"
or query.aggregate == "AVG"
or query.aggregate == "SUM"
):
# These aggregate keywords not currently used in the API, but conditional
# present in case they're used in the future
if query.aggregate == "AVG" or query.aggregate == "SUM":
# Distinct can be combined with other aggregate functions
query.setAggregate(f"{query.aggregate}:DISTINCT")
elif query.aggregate == "COUNT":
# When count and distinct keywords are used together when selecting
# multiple attributes, Python ICAT will always throw an error on query
# execution (more info:
# https://github.com/icatproject/python-icat/issues/76). This appears to
# be a JPQL limitation, something that cannot be fixed in Python ICAT.
# As a result, the API will get the distinct results and manually
# perform `len()` on the list, using `manual_count` as a flag to
# recognise this situation
query.setAggregate("DISTINCT")
query.manual_count = True
else:
query.setAggregate("DISTINCT")

Expand Down
2 changes: 2 additions & 0 deletions datagateway_api/common/icat/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(
:raises PythonICATError: If a ValueError is raised when creating a Query(), 500
will be returned as a response
"""
# Flag for a count request that uses a distinct filter
self.manual_count = False

try:
log.info("Creating ICATQuery for entity: %s", entity_name)
Expand Down

0 comments on commit abb31bd

Please sign in to comment.