#34255: Annotation/group by with an expression on psycopg3
-------------------------------------+-------------------------------------
     Reporter:  Guillaume Andreu     |                    Owner:  nobody
  Sabater                            |
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:  orm postgres         |             Triage Stage:  Accepted
  psycopg3 annotation groupby        |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 > I would love to see this done for Django in general and not at the
 individual backend level. That said it is probably to late for 4.1

 Agreed that it would be nice to get but there's no way we can squeeze it
 in for 4.2.

 > If we cannot fix the issue at hand nicely I think we could fall back to
 client side cursors and provide a setting (backend option) to switch back
 to server side cursors so people can experiment with them. Btw from the
 looks of it and issues we ran into developing psycopg3 support, this also
 means that the postgresql backend is basically the only backend using
 server side bindings or is it just stricter than every other backend?

 I'm not sure what you mean here by server side cursors. Did you mean
 server side bindings?

 I'm not sure what's causing it to be stricter but if pyscopg>=3 allows for
 psycopg2 style of parameter bindings we should default to it until we can
 figure out an adequate solution here. If that doesn't work I can commit
 time to solving this issue but I fear the solution won't be pretty.

 Basically we'd have to adapt `SQLCompiler.get_group_by` to do the
 following

 1. Change its return signature so it's allowed to return members that
 should be added to the `SELECT` clause
 2. Adjust
 
[https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/compiler.py#L172
 its compile logic] so when a compiled expression is parametrized
 (`len(params) >= 1`) and is not part of `selected_expr_indices` then it's
 added to of the members that must be added to the `SELECT` and are
 returned by the method.
 3. Then adjust `.as_sql` to the `SELECT *original_select (SELECT
 *original_select, *extra_group_by_select ... GROUP BY ...
 *extra_group_by_select_indices)` when there are `extra_group_by_select`

 The only interesting bit is that we might be able to reuse some logic that
 was added to implement window function filtering.

 That's a lot of work to support server side bindings and that's only for
 the group by clause, there might be other cases lurking around.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34255#comment:10>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070185ae79e1e7-ed085ada-6d77-4a2b-aed7-bce8d50f880f-000000%40eu-central-1.amazonses.com.

Reply via email to