diff --git a/datagateway_api/common/icat/filters.py b/datagateway_api/common/icat/filters.py index 9bd02436..512f9c5c 100644 --- a/datagateway_api/common/icat/filters.py +++ b/datagateway_api/common/icat/filters.py @@ -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") diff --git a/datagateway_api/common/icat/query.py b/datagateway_api/common/icat/query.py index d1cb962f..9d99e9e2 100644 --- a/datagateway_api/common/icat/query.py +++ b/datagateway_api/common/icat/query.py @@ -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)