prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Learner activity records partially not visible after upgrade to 0.14b3
  # Observed Behaviour
  
  Upgraded from 0.13.3 to 0.14b3. Learner activity records were partially not visible in Class Home -> Class activity and Reports. Downgraded back to 0.13.3 and they are all visible again.
  
  # Expected behavior
  
  All learner activity should be visible as prior to upgrade
  
  
  # User-facing consequences
  
   Confusion and fear of data loss.
  
  # Errors and logs
  
  None on screen
  
  # Steps to reproduce
  
  Upgrade from 0.13.3 to 0.14.0-b3 and check learner activity records.
  
  
  
  # Context
  Ubuntu 18.04.3
  Package : 0.14.beta3 .deb
  
  
  </issue>
  <code>
  [start of kolibri/core/query.py]
  1 from django.db import connection
  2 from django.db.models import Aggregate
  3 from django.db.models import CharField
  4 from django.db.models import IntegerField
  5 from django.db.models import Subquery
  6 
  7 try:
  8     from django.contrib.postgres.aggregates import ArrayAgg
  9 
  10     class NotNullArrayAgg(ArrayAgg):
  11         def convert_value(self, value, expression, connection, context):
  12             if not value:
  13                 return []
  14             return filter(lambda x: x is not None, value)
  15 
  16 
  17 except ImportError:
  18     NotNullArrayAgg = None
  19 
  20 
  21 class SQCount(Subquery):
  22     # Include ALIAS at the end to support Postgres
  23     template = "(SELECT COUNT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
  24     output_field = IntegerField()
  25 
  26 
  27 class SQSum(Subquery):
  28     # Include ALIAS at the end to support Postgres
  29     template = "(SELECT SUM(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
  30     output_field = IntegerField()
  31 
  32 
  33 class GroupConcatSubquery(Subquery):
  34     template = "(SELECT GROUP_CONCAT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)"
  35     output_field = CharField()
  36 
  37     def as_postgresql(self, compiler, connection):
  38         self.template = (
  39             "(SELECT STRING_AGG(%(field)s, ',') FROM (%(subquery)s) AS %(field)s__sum)"
  40         )
  41         return super(GroupConcatSubquery, self).as_sql(compiler, connection)
  42 
  43 
  44 class GroupConcat(Aggregate):
  45     template = "GROUP_CONCAT(%(field)s)"
  46     output_field = CharField()
  47 
  48     def __init__(self, *args, **kwargs):
  49         self.result_field = kwargs.pop("result_field", None)
  50         super(GroupConcat, self).__init__(*args, **kwargs)
  51 
  52     def convert_value(self, value, expression, connection, context):
  53         if not value:
  54             return []
  55         results = value.split(",")
  56         if self.result_field is not None:
  57             return map(self.result_field.to_python, results)
  58         return results
  59 
  60 
  61 def get_source_field(model, field_path):
  62     # Get the source field from the model so that we can properly coerce values
  63     # this is necessary when we are using GroupConcat to return non-string fields.
  64     paths = field_path.split("__")
  65     while len(paths) > 1:
  66         model = model._meta.get_field(paths.pop(0)).related_model
  67     return model._meta.get_field(paths[0])
  68 
  69 
  70 def annotate_array_aggregate(queryset, **kwargs):
  71     if connection.vendor == "postgresql" and NotNullArrayAgg is not None:
  72         return queryset.annotate(
  73             **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}
  74         )
  75     model = queryset.model
  76     # Call values on "pk" to insert a GROUP BY to ensure the GROUP CONCAT
  77     # is called by row and not across the entire queryset.
  78     return queryset.values("pk").annotate(
  79         **{
  80             target: GroupConcat(source, result_field=get_source_field(model, source))
  81             for target, source in kwargs.items()
  82         }
  83     )
  84
  [end of kolibri/core/query.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/kolibri/core/query.py b/kolibri/core/query.py
  --- a/kolibri/core/query.py
  +++ b/kolibri/core/query.py
  @@ -8,10 +8,17 @@
       from django.contrib.postgres.aggregates import ArrayAgg
   
       class NotNullArrayAgg(ArrayAgg):
  +        def __init__(self, *args, **kwargs):
  +            self.result_field = kwargs.pop("result_field", None)
  +            super(NotNullArrayAgg, self).__init__(*args, **kwargs)
  +
           def convert_value(self, value, expression, connection, context):
               if not value:
                   return []
  -            return filter(lambda x: x is not None, value)
  +            results = list(filter(lambda x: x is not None, value))
  +            if self.result_field is not None:
  +                return list(map(self.result_field.to_python, results))
  +            return results
   
   
   except ImportError:
  @@ -54,7 +61,7 @@
               return []
           results = value.split(",")
           if self.result_field is not None:
  -            return map(self.result_field.to_python, results)
  +            return list(map(self.result_field.to_python, results))
           return results
   
   
  @@ -68,11 +75,16 @@
   
   
   def annotate_array_aggregate(queryset, **kwargs):
  +    model = queryset.model
       if connection.vendor == "postgresql" and NotNullArrayAgg is not None:
           return queryset.annotate(
  -            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}
  +            **{
  +                target: NotNullArrayAgg(
  +                    source, result_field=get_source_field(model, source)
  +                )
  +                for target, source in kwargs.items()
  +            }
           )
  -    model = queryset.model
       # Call values on "pk" to insert a GROUP BY to ensure the GROUP CONCAT
       # is called by row and not across the entire queryset.
       return queryset.values("pk").annotate(
  
verification_info: {"golden_diff": "diff --git a/kolibri/core/query.py b/kolibri/core/query.py\n--- a/kolibri/core/query.py\n+++ b/kolibri/core/query.py\n@@ -8,10 +8,17 @@\n     from django.contrib.postgres.aggregates import ArrayAgg\n \n     class NotNullArrayAgg(ArrayAgg):\n+        def __init__(self, *args, **kwargs):\n+            self.result_field = kwargs.pop(\"result_field\", None)\n+            super(NotNullArrayAgg, self).__init__(*args, **kwargs)\n+\n         def convert_value(self, value, expression, connection, context):\n             if not value:\n                 return []\n-            return filter(lambda x: x is not None, value)\n+            results = list(filter(lambda x: x is not None, value))\n+            if self.result_field is not None:\n+                return list(map(self.result_field.to_python, results))\n+            return results\n \n \n except ImportError:\n@@ -54,7 +61,7 @@\n             return []\n         results = value.split(\",\")\n         if self.result_field is not None:\n-            return map(self.result_field.to_python, results)\n+            return list(map(self.result_field.to_python, results))\n         return results\n \n \n@@ -68,11 +75,16 @@\n \n \n def annotate_array_aggregate(queryset, **kwargs):\n+    model = queryset.model\n     if connection.vendor == \"postgresql\" and NotNullArrayAgg is not None:\n         return queryset.annotate(\n-            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}\n+            **{\n+                target: NotNullArrayAgg(\n+                    source, result_field=get_source_field(model, source)\n+                )\n+                for target, source in kwargs.items()\n+            }\n         )\n-    model = queryset.model\n     # Call values on \"pk\" to insert a GROUP BY to ensure the GROUP CONCAT\n     # is called by row and not across the entire queryset.\n     return queryset.values(\"pk\").annotate(\n", "issue": "Learner activity records partially not visible after upgrade to 0.14b3\n# Observed Behaviour\n\nUpgraded from 0.13.3 to 0.14b3. Learner activity records were partially not visible in Class Home -> Class activity and Reports. Downgraded back to 0.13.3 and they are all visible again.\n\n# Expected behavior\n\nAll learner activity should be visible as prior to upgrade\n\n\n# User-facing consequences\n\n Confusion and fear of data loss.\n\n# Errors and logs\n\nNone on screen\n\n# Steps to reproduce\n\nUpgrade from 0.13.3 to 0.14.0-b3 and check learner activity records.\n\n\n\n# Context\nUbuntu 18.04.3\nPackage : 0.14.beta3 .deb\n\n", "before_files": [{"content": "from django.db import connection\nfrom django.db.models import Aggregate\nfrom django.db.models import CharField\nfrom django.db.models import IntegerField\nfrom django.db.models import Subquery\n\ntry:\n    from django.contrib.postgres.aggregates import ArrayAgg\n\n    class NotNullArrayAgg(ArrayAgg):\n        def convert_value(self, value, expression, connection, context):\n            if not value:\n                return []\n            return filter(lambda x: x is not None, value)\n\n\nexcept ImportError:\n    NotNullArrayAgg = None\n\n\nclass SQCount(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT COUNT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass SQSum(Subquery):\n    # Include ALIAS at the end to support Postgres\n    template = \"(SELECT SUM(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = IntegerField()\n\n\nclass GroupConcatSubquery(Subquery):\n    template = \"(SELECT GROUP_CONCAT(%(field)s) FROM (%(subquery)s) AS %(field)s__sum)\"\n    output_field = CharField()\n\n    def as_postgresql(self, compiler, connection):\n        self.template = (\n            \"(SELECT STRING_AGG(%(field)s, ',') FROM (%(subquery)s) AS %(field)s__sum)\"\n        )\n        return super(GroupConcatSubquery, self).as_sql(compiler, connection)\n\n\nclass GroupConcat(Aggregate):\n    template = \"GROUP_CONCAT(%(field)s)\"\n    output_field = CharField()\n\n    def __init__(self, *args, **kwargs):\n        self.result_field = kwargs.pop(\"result_field\", None)\n        super(GroupConcat, self).__init__(*args, **kwargs)\n\n    def convert_value(self, value, expression, connection, context):\n        if not value:\n            return []\n        results = value.split(\",\")\n        if self.result_field is not None:\n            return map(self.result_field.to_python, results)\n        return results\n\n\ndef get_source_field(model, field_path):\n    # Get the source field from the model so that we can properly coerce values\n    # this is necessary when we are using GroupConcat to return non-string fields.\n    paths = field_path.split(\"__\")\n    while len(paths) > 1:\n        model = model._meta.get_field(paths.pop(0)).related_model\n    return model._meta.get_field(paths[0])\n\n\ndef annotate_array_aggregate(queryset, **kwargs):\n    if connection.vendor == \"postgresql\" and NotNullArrayAgg is not None:\n        return queryset.annotate(\n            **{target: NotNullArrayAgg(source) for target, source in kwargs.items()}\n        )\n    model = queryset.model\n    # Call values on \"pk\" to insert a GROUP BY to ensure the GROUP CONCAT\n    # is called by row and not across the entire queryset.\n    return queryset.values(\"pk\").annotate(\n        **{\n            target: GroupConcat(source, result_field=get_source_field(model, source))\n            for target, source in kwargs.items()\n        }\n    )\n", "path": "kolibri/core/query.py"}]}