Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #26192 -- Fixed crash of ordering by constants on PostgreSQL. #11396

Merged
merged 2 commits into from May 31, 2019

Conversation

felixxm
Copy link
Member

@felixxm felixxm commented May 21, 2019

Ordering by constants is reasonable in some cases (e.g. in compound queries), but in the same time it can be problematic on PostgreSQL without specified output type (see ticket 26192).

IMO we should raise FieldError when output_field cannot be resolved.

@charettes What do you think?

@cansarigol
Copy link
Contributor

I remembered @charettes pr about this.

@charettes
Copy link
Member

charettes commented May 27, 2019

@felixxm I'm not sure this is the right approach.

Somewhere between when the ticket was initially created (1.8 era) and now the annotate(constant=Value('foo')).order_by('constant') expression changed from being compiled to

SELECT ..., 'foo' AS constant ORDER BY 'foo'

to

SELECT ..., 'foo' AS constant ORDER BY constant

Which PostgreSQL doesn't have issues with.

The main difference now is that order_by reuses existing aliases which explains why your test_order_by_constant_value test passes. The original issue can still be reproduced by passing an expression directly to order_by though even if output_field is specified:

def test_order_by_constant_value(self):
    qs = Article.objects.order_by(
        constant=Value('1', output_field=IntegerField()),
    )
    list(qs)

The reason for this is that output_field resolution doesn't result in a cast (e.g. ::text) which is what seems to be tripping PostgreSQL here. Also the test_order_by_constant_value_without_output_field will to fail if #11359 is merged as @cansarigol pointed out.

Here's my take on how this could be resolved:

  1. Make order_by simply ignore Value expressions. They are constants anyway so there's no point in ordering by them.
  2. Require that Value expressions passed to order_by be resolvable like you did here and wrap them in Cast(val, val.output_field).

@felixxm felixxm force-pushed the issue-26192 branch 2 times, most recently from 77ec256 to 22a52fd Compare May 28, 2019 12:03
@felixxm
Copy link
Member Author

felixxm commented May 28, 2019

@charettes Thanks for your analysis 🙇‍♂️ I added Cast() expressions and extra tests.

I agree that original issue can be reproduced by passing an expression directly to order_by but only when output_field is not an IntegerField, because integers are interpreted as column numbers (what is misleading).

I also know test_order_by_constant_value_without_output_field will not raise exceptions after #11359 but that's OK (IMO) we can move these use cases into test_order_by_constant_value.

@felixxm felixxm changed the title Fixed #26192 -- Raised a FieldError when ordering by expressions with unresolved output_field. Fixed #26192 -- Fixed crash of ordering by constants on PostgreSQL. May 28, 2019
@felixxm felixxm requested a review from charettes May 29, 2019 07:20
Copy link
Member

@charettes charettes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for the adjustments.

tests/ordering/tests.py Outdated Show resolved Hide resolved
django/db/models/sql/compiler.py Outdated Show resolved Hide resolved
@felixxm
Copy link
Member Author

felixxm commented May 30, 2019

@charettes Thanks!

@felixxm felixxm self-assigned this May 30, 2019
@felixxm felixxm force-pushed the issue-26192 branch 2 times, most recently from 40ea163 to 04d8365 Compare May 30, 2019 17:56
@felixxm felixxm merged commit f6075fb into django:master May 31, 2019
@felixxm felixxm deleted the issue-26192 branch May 31, 2019 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants