Skip to content

Commit

Permalink
Fixed #31002 -- Fixed GIS lookups crash against a subquery annotation.
Browse files Browse the repository at this point in the history
Thanks Vasileios Bouzas for the report.
  • Loading branch information
charettes authored and felixxm committed Nov 25, 2019
1 parent 29d8198 commit 0290e01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions django/contrib/gis/db/backends/oracle/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def check_relate_argument(self, arg):
raise ValueError('Invalid SDO_RELATE mask: "%s"' % arg)

def as_sql(self, connection, lookup, template_params, sql_params):
template_params['mask'] = sql_params.pop()
return super().as_sql(connection, lookup, template_params, sql_params)
template_params['mask'] = sql_params[-1]
return super().as_sql(connection, lookup, template_params, sql_params[:-1])


class OracleOperations(BaseSpatialOperations, DatabaseOperations):
Expand Down
4 changes: 2 additions & 2 deletions django/contrib/gis/db/models/lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def get_rhs_op(self, connection, rhs):
return connection.ops.gis_operators[self.lookup_name]

def as_sql(self, compiler, connection):
lhs_sql, sql_params = self.process_lhs(compiler, connection)
lhs_sql, lhs_params = self.process_lhs(compiler, connection)
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
sql_params.extend(rhs_params)
sql_params = (*lhs_params, *rhs_params)

template_params = {'lhs': lhs_sql, 'rhs': rhs_sql, 'value': '%s', **self.template_params}
rhs_op = self.get_rhs_op(connection, rhs_sql)
Expand Down
20 changes: 18 additions & 2 deletions tests/gis_tests/geoapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
)
from django.core.management import call_command
from django.db import NotSupportedError, connection
from django.db.models import F, OuterRef, Subquery
from django.test import TestCase, skipUnlessDBFeature

from ..utils import (
mariadb, mysql, no_oracle, oracle, postgis, skipUnlessGISLookup,
spatialite,
)
from .models import (
City, Country, Feature, MinusOneSRID, NonConcreteModel, PennsylvaniaCity,
State, Track,
City, Country, Feature, MinusOneSRID, MultiFields, NonConcreteModel,
PennsylvaniaCity, State, Track,
)


Expand Down Expand Up @@ -494,6 +495,21 @@ def test_gis_lookups_with_complex_expressions(self):
with self.subTest(lookup):
City.objects.filter(**{'point__' + lookup: functions.Union('point', 'point')}).exists()

def test_subquery_annotation(self):
multifields = MultiFields.objects.create(
city=City.objects.create(point=Point(1, 1)),
point=Point(2, 2),
poly=Polygon.from_bbox((0, 0, 2, 2)),
)
qs = MultiFields.objects.annotate(
city_point=Subquery(City.objects.filter(
id=OuterRef('city'),
).values('point')),
).filter(
city_point__within=F('poly'),
)
self.assertEqual(qs.get(), multifields)


class GeoQuerySetTest(TestCase):
# TODO: GeoQuerySet is removed, organize these test better.
Expand Down

0 comments on commit 0290e01

Please sign in to comment.