Opened 4 months ago

Last modified 7 weeks ago

#35462 assigned New feature

Add support JSONArrayAgg

Reported by: Robert Silén Owned by: Lufafa Joshua
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords: aggregation, ArrayAgg
Cc: Hisham Mahmood Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Robert Silén)

I suggest to add ArrayAgg and related (aggregation) functions for MariaDB to Django. It has been noted in MariaDB Foundation ticket: https://jira.mariadb.org/browse/MDBF-690

E.g. ArrayAgg is mentioned on PostgreSQL specific aggregation functions:
https://docs.djangoproject.com/en/dev/ref/contrib/postgres/aggregates/#arrayagg

The equivalent in MariaDB is presumably this:
https://mariadb.com/kb/en/json_arrayagg/

More equivalent MariaDB aggregate functions are listed at the following address.
https://mariadb.com/kb/en/aggregate-functions/
Some have same name 1:1. I'm guessing e.g. StringAgg could be done with GROUP_CONCAT.

Feel free to ask more here, or in the MariaDB Foundation ticket.

Change History (8)

comment:1 by Robert Silén, 4 months ago

Description: modified (diff)

comment:2 by Robert Silén, 4 months ago

Description: modified (diff)

comment:3 by Simon Charette, 4 months ago

Component: UncategorizedDatabase layer (models, ORM)
Summary: ArrayAgg and others for MariaDBAdd support JSONArrayAgg
Triage Stage: UnreviewedAccepted

The contrib.postgres.ArrayAgg expression you pointed at uses Postgres arrays and MariaDB doesn't have native array support so they are not equivalent.

JSON_ARRAYAGG is a standard SQL function implemented on Postgres, SQLite, MySQL, SQLite, Oracle, and MariaDB so if we were to include it in core it should be for all supported backends and not only for MariaDB.

It should be trivial to implement by subclassing Aggregate in fact I suspect that the following should work on most backends by default

class JSONArrayAgg(Aggregate):
    function = "JSON_ARRAYAGG"
    output_field = JSONField()
    arity = 1

Or with subtle adjusted as using json_group_array on SQLite.

As for StringAgg with a fallback on GROUP_CONCAT on MySQL and MariaDB it is already being explored in #35444 which plans to add support for configurable support for ORDER BY in aggregates.

comment:4 by Lufafa Joshua, 3 months ago

Owner: changed from nobody to Lufafa Joshua
Status: newassigned

comment:5 by Hisham Mahmood, 2 months ago

Cc: Hisham Mahmood added

comment:6 by Lufafa Joshua, 2 months ago

Has patch: set
Last edited 7 weeks ago by Lufafa Joshua (previous) (diff)

comment:7 by Lufafa Joshua, 7 weeks ago

Needs documentation: set

comment:8 by Simon Charette, 7 weeks ago

Needs tests: set
Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top