]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_api/sql/sqlalchemy_functions.py
7abe7d3f33ac12dd9e89a6dabcebd812ece6d6d5
[nominatim.git] / src / nominatim_api / sql / sqlalchemy_functions.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Custom functions and expressions for SQLAlchemy.
9 """
10 from __future__ import annotations
11 from typing import Any
12
13 import sqlalchemy as sa
14 from sqlalchemy.ext.compiler import compiles
15
16 from ..typing import SaColumn
17
18 # pylint: disable=all
19
20 class PlacexGeometryReverseLookuppolygon(sa.sql.functions.GenericFunction[Any]):
21     """ Check for conditions that allow partial index use on
22         'idx_placex_geometry_reverse_lookupPolygon'.
23
24         Needs to be constant, so that the query planner picks them up correctly
25         in prepared statements.
26     """
27     name = 'PlacexGeometryReverseLookuppolygon'
28     inherit_cache = True
29
30
31 @compiles(PlacexGeometryReverseLookuppolygon) # type: ignore[no-untyped-call, misc]
32 def _default_intersects(element: PlacexGeometryReverseLookuppolygon,
33                         compiler: 'sa.Compiled', **kw: Any) -> str:
34     return ("(ST_GeometryType(placex.geometry) in ('ST_Polygon', 'ST_MultiPolygon')"
35             " AND placex.rank_address between 4 and 25"
36             " AND placex.type != 'postcode'"
37             " AND placex.name is not null"
38             " AND placex.indexed_status = 0"
39             " AND placex.linked_place_id is null)")
40
41
42 @compiles(PlacexGeometryReverseLookuppolygon, 'sqlite') # type: ignore[no-untyped-call, misc]
43 def _sqlite_intersects(element: PlacexGeometryReverseLookuppolygon,
44                        compiler: 'sa.Compiled', **kw: Any) -> str:
45     return ("(ST_GeometryType(placex.geometry) in ('POLYGON', 'MULTIPOLYGON')"
46             " AND placex.rank_address between 4 and 25"
47             " AND placex.type != 'postcode'"
48             " AND placex.name is not null"
49             " AND placex.indexed_status = 0"
50             " AND placex.linked_place_id is null)")
51
52
53 class IntersectsReverseDistance(sa.sql.functions.GenericFunction[Any]):
54     name = 'IntersectsReverseDistance'
55     inherit_cache = True
56
57     def __init__(self, table: sa.Table, geom: SaColumn) -> None:
58         super().__init__(table.c.geometry,
59                          table.c.rank_search, geom)
60         self.tablename = table.name
61
62
63 @compiles(IntersectsReverseDistance) # type: ignore[no-untyped-call, misc]
64 def default_reverse_place_diameter(element: IntersectsReverseDistance,
65                                    compiler: 'sa.Compiled', **kw: Any) -> str:
66     table = element.tablename
67     return f"({table}.rank_address between 4 and 25"\
68            f" AND {table}.type != 'postcode'"\
69            f" AND {table}.name is not null"\
70            f" AND {table}.linked_place_id is null"\
71            f" AND {table}.osm_type = 'N'" + \
72            " AND ST_Buffer(%s, reverse_place_diameter(%s)) && %s)" % \
73                tuple(map(lambda c: compiler.process(c, **kw), element.clauses))
74
75
76 @compiles(IntersectsReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc]
77 def sqlite_reverse_place_diameter(element: IntersectsReverseDistance,
78                                   compiler: 'sa.Compiled', **kw: Any) -> str:
79     geom1, rank, geom2 = list(element.clauses)
80     table = element.tablename
81
82     return (f"({table}.rank_address between 4 and 25"\
83             f" AND {table}.type != 'postcode'"\
84             f" AND {table}.name is not null"\
85             f" AND {table}.linked_place_id is null"\
86             f" AND {table}.osm_type = 'N'"\
87              " AND MbrIntersects(%s, ST_Expand(%s, 14.0 * exp(-0.2 * %s) - 0.03))"\
88             f" AND {table}.place_id IN"\
89              " (SELECT place_id FROM placex_place_node_areas"\
90              "  WHERE ROWID IN (SELECT ROWID FROM SpatialIndex"\
91              "  WHERE f_table_name = 'placex_place_node_areas'"\
92              "  AND search_frame = %s)))") % (
93                 compiler.process(geom1, **kw),
94                 compiler.process(geom2, **kw),
95                 compiler.process(rank, **kw),
96                 compiler.process(geom2, **kw))
97
98
99 class IsBelowReverseDistance(sa.sql.functions.GenericFunction[Any]):
100     name = 'IsBelowReverseDistance'
101     inherit_cache = True
102
103
104 @compiles(IsBelowReverseDistance) # type: ignore[no-untyped-call, misc]
105 def default_is_below_reverse_distance(element: IsBelowReverseDistance,
106                                       compiler: 'sa.Compiled', **kw: Any) -> str:
107     dist, rank = list(element.clauses)
108     return "%s < reverse_place_diameter(%s)" % (compiler.process(dist, **kw),
109                                                 compiler.process(rank, **kw))
110
111
112 @compiles(IsBelowReverseDistance, 'sqlite') # type: ignore[no-untyped-call, misc]
113 def sqlite_is_below_reverse_distance(element: IsBelowReverseDistance,
114                                      compiler: 'sa.Compiled', **kw: Any) -> str:
115     dist, rank = list(element.clauses)
116     return "%s < 14.0 * exp(-0.2 * %s) - 0.03" % (compiler.process(dist, **kw),
117                                                   compiler.process(rank, **kw))
118
119
120 class IsAddressPoint(sa.sql.functions.GenericFunction[Any]):
121     name = 'IsAddressPoint'
122     inherit_cache = True
123
124     def __init__(self, table: sa.Table) -> None:
125         super().__init__(table.c.rank_address,
126                          table.c.housenumber, table.c.name)
127
128
129 @compiles(IsAddressPoint) # type: ignore[no-untyped-call, misc]
130 def default_is_address_point(element: IsAddressPoint,
131                              compiler: 'sa.Compiled', **kw: Any) -> str:
132     rank, hnr, name = list(element.clauses)
133     return "(%s = 30 AND (%s IS NOT NULL OR %s ? 'addr:housename'))" % (
134                 compiler.process(rank, **kw),
135                 compiler.process(hnr, **kw),
136                 compiler.process(name, **kw))
137
138
139 @compiles(IsAddressPoint, 'sqlite') # type: ignore[no-untyped-call, misc]
140 def sqlite_is_address_point(element: IsAddressPoint,
141                             compiler: 'sa.Compiled', **kw: Any) -> str:
142     rank, hnr, name = list(element.clauses)
143     return "(%s = 30 AND coalesce(%s, json_extract(%s, '$.addr:housename')) IS NOT NULL)" % (
144                 compiler.process(rank, **kw),
145                 compiler.process(hnr, **kw),
146                 compiler.process(name, **kw))
147
148
149 class CrosscheckNames(sa.sql.functions.GenericFunction[Any]):
150     """ Check if in the given list of names in parameters 1 any of the names
151         from the JSON array in parameter 2 are contained.
152     """
153     name = 'CrosscheckNames'
154     inherit_cache = True
155
156 @compiles(CrosscheckNames) # type: ignore[no-untyped-call, misc]
157 def compile_crosscheck_names(element: CrosscheckNames,
158                              compiler: 'sa.Compiled', **kw: Any) -> str:
159     arg1, arg2 = list(element.clauses)
160     return "coalesce(avals(%s) && ARRAY(SELECT * FROM json_array_elements_text(%s)), false)" % (
161             compiler.process(arg1, **kw), compiler.process(arg2, **kw))
162
163
164 @compiles(CrosscheckNames, 'sqlite') # type: ignore[no-untyped-call, misc]
165 def compile_sqlite_crosscheck_names(element: CrosscheckNames,
166                                     compiler: 'sa.Compiled', **kw: Any) -> str:
167     arg1, arg2 = list(element.clauses)
168     return "EXISTS(SELECT *"\
169            " FROM json_each(%s) as name, json_each(%s) as match_name"\
170            " WHERE name.value = match_name.value)"\
171            % (compiler.process(arg1, **kw), compiler.process(arg2, **kw))
172
173
174 class JsonArrayEach(sa.sql.functions.GenericFunction[Any]):
175     """ Return elements of a json array as a set.
176     """
177     name = 'JsonArrayEach'
178     inherit_cache = True
179
180
181 @compiles(JsonArrayEach) # type: ignore[no-untyped-call, misc]
182 def default_json_array_each(element: JsonArrayEach, compiler: 'sa.Compiled', **kw: Any) -> str:
183     return "json_array_elements(%s)" % compiler.process(element.clauses, **kw)
184
185
186 @compiles(JsonArrayEach, 'sqlite') # type: ignore[no-untyped-call, misc]
187 def sqlite_json_array_each(element: JsonArrayEach, compiler: 'sa.Compiled', **kw: Any) -> str:
188     return "json_each(%s)" % compiler.process(element.clauses, **kw)
189
190
191
192 class Greatest(sa.sql.functions.GenericFunction[Any]):
193     """ Function to compute maximum of all its input parameters.
194     """
195     name = 'greatest'
196     inherit_cache = True
197
198
199 @compiles(Greatest, 'sqlite') # type: ignore[no-untyped-call, misc]
200 def sqlite_greatest(element: Greatest, compiler: 'sa.Compiled', **kw: Any) -> str:
201     return "max(%s)" % compiler.process(element.clauses, **kw)
202
203
204
205 class RegexpWord(sa.sql.functions.GenericFunction[Any]):
206     """ Check if a full word is in a given string.
207     """
208     name = 'RegexpWord'
209     inherit_cache = True
210
211
212 @compiles(RegexpWord, 'postgresql') # type: ignore[no-untyped-call, misc]
213 def postgres_regexp_nocase(element: RegexpWord, compiler: 'sa.Compiled', **kw: Any) -> str:
214     arg1, arg2 = list(element.clauses)
215     return "%s ~* ('\\m(' || %s  || ')\\M')::text" % (compiler.process(arg2, **kw), compiler.process(arg1, **kw))
216
217
218 @compiles(RegexpWord, 'sqlite') # type: ignore[no-untyped-call, misc]
219 def sqlite_regexp_nocase(element: RegexpWord, compiler: 'sa.Compiled', **kw: Any) -> str:
220     arg1, arg2 = list(element.clauses)
221     return "regexp('\\b(' || %s  || ')\\b', %s)" % (compiler.process(arg1, **kw), compiler.process(arg2, **kw))