#32906: Explain JSONBAgg benefit
--------------------------------------+------------------------------------
     Reporter:  Claude Paroz          |                    Owner:  Abhyudai
         Type:  Cleanup/optimization  |                   Status:  assigned
    Component:  Documentation         |                  Version:  dev
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Abhyudai):

 Replying to [comment:1 Mariusz Felisiak]:
 > I don't think there is a significant difference between them.
 `ARRAY_AGG()` returns array and `JSONB_AGG()` return JSON array, so when
 you aggregate `JSONField`s with `JSONBAgg()` then you can use key and
 index transforms which don't work with `ArrayAgg()` annotations.

 As far I can see, they both are giving the same results.

 {{{#!python
 # model

 class AggregateTestModel(PostgreSQLModel):
     """
     To test postgres-specific general aggregation functions
     """
     json_field = models.JSONField(null=True)

 }}}

 {{{#!sh
 # shell

 >>> AggreTestModel.objects.bulk_create(
      AggregateTestModel(json_field={'lang': 'pl'}),
      AggregateTestModel(json_field={'lang': 'en'}),
      AggregateTestModel(json_field={'breed': 'collie'})
 )

 >>> AggregateTestModel.objects.all().values_list('json_field', flat=True)
 <QuerySet [None, {'lang': 'pl'}, {'lang': 'en'}, {'breed': 'collie'}]>
 >>> AggregateTestModel.objects.aggregate(val=JSONBAgg('json_field'))
 {'val': [None, {'lang': 'pl'}, {'lang': 'en'}, {'breed': 'collie'}]}
 >>> jsonb =
 AggregateTestModel.objects.aggregate(val=JSONBAgg('json_field'))
 >>> jsonb
 {'val': [None, {'lang': 'pl'}, {'lang': 'en'}, {'breed': 'collie'}]}
 >>> arr = AggregateTestModel.objects.aggregate(val=ArrayAgg('json_field'))
 >>> arr
 {'val': [None, {'lang': 'pl'}, {'lang': 'en'}, {'breed': 'collie'}]}
 >>> arr == jsonb
 True
 >>> type(arr)
 <class 'dict'>
 >>> type(jsonb)
 <class 'dict'>

 }}}

 Am I missing something here? The above snippet is just an abstracted
 version of the model inside the package `postgres_tests.models`, used for
 tests.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32906#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.64f6fb84323ae8c89b592f641cb3f1fe%40djangoproject.com.

Reply via email to