Opened 4 years ago
Closed 3 years ago
#32776 closed New feature (fixed)
Support Array subqueries on PostgreSQL
Reported by: | Hannes Ljungberg | Owned by: | Hannes Ljungberg |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I think we should support constructing arrays in PostgreSQL() without requiring aggregation through array_agg()
. This can be done by correlated subqueries by passing a subquery to the ARRAY
constructor. In its simplest form it could look something like this:
class ArraySubquery(Subquery): template = 'ARRAY(%(subquery)s)'
Currently all subqueries get their ordering cleared if they aren't fulfilling some conditions like being sliced and using distinct fields. This would need to be refactored to enable array subqueries since the ordering matters due to the subquery producing an array.
There've been a couple of tickets related to this:
https://code.djangoproject.com/ticket/31687
https://code.djangoproject.com/ticket/32658
Simons suggestion to move the clearing of ordering from Query.resolve_expression
to In(Lookup).process_rhs
seems like a good idea to me.
I also think that adding support for array subqueries would help in resolving this ticket: https://code.djangoproject.com/ticket/26056
Change History (6)
comment:1 by , 4 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 3 years ago
Patch needs improvement: | set |
---|
comment:4 by , 3 years ago
Patch needs improvement: | unset |
---|
comment:5 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Another edge case that came to mind is that we also likely want to keep clearing ordering when doing existence checks so it should only be cleared when using
filter(field__in=query)
Exists
Queryset.exists()