diff options
| author | Simon Charette <simon.charette@zapier.com> | 2019-05-20 18:53:13 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-05-21 07:11:26 +0200 |
| commit | 4d1420947e79bc89d266229feea305294ec896ee (patch) | |
| tree | 665c73485aabb065f71e80452bf1898c0ab544b7 /django | |
| parent | 1d0bab0bfd77edcf1228d45bf654457a8ff1890d (diff) | |
Fixed #30494 -- Disabled __year lookup optimization for indirect values.
The previous heuristics were naively enabling the BETWEEN optimization on
successful cast of the first rhs SQL params to an integer while it was
not appropriate for a lot of database resolved expressions.
Thanks Alexey Chernov for the report.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/lookups.py | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 70cd525f30..d4edf2ce0f 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -508,21 +508,16 @@ class YearExact(YearLookup, Exact): lookup_name = 'exact' def as_sql(self, compiler, connection): - # We will need to skip the extract part and instead go - # directly with the originating field, that is self.lhs.lhs. - lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs) - rhs_sql, rhs_params = self.process_rhs(compiler, connection) - try: - # Check that rhs_params[0] exists (IndexError), - # it isn't None (TypeError), and is a number (ValueError) - int(rhs_params[0]) - except (IndexError, TypeError, ValueError): - # Can't determine the bounds before executing the query, so skip - # optimizations by falling back to a standard exact comparison. - return super().as_sql(compiler, connection) - bounds = self.year_lookup_bounds(connection, rhs_params[0]) - params.extend(bounds) - return '%s BETWEEN %%s AND %%s' % lhs_sql, params + # Avoid the extract operation if the rhs is a direct value to allow + # indexes to be used. + if self.rhs_is_direct_value(): + # Skip the extract part by directly using the originating field, + # that is self.lhs.lhs. + lhs_sql, params = self.process_lhs(compiler, connection, self.lhs.lhs) + bounds = self.year_lookup_bounds(connection, self.rhs) + params.extend(bounds) + return '%s BETWEEN %%s AND %%s' % lhs_sql, params + return super().as_sql(compiler, connection) class YearGt(YearComparisonLookup): |
