]> git.openstreetmap.org Git - nominatim.git/blob - test/python/indexer/test_indexing.py
Merge pull request #2559 from lonvia/disable-jit-in-queries
[nominatim.git] / test / python / indexer / test_indexing.py
1 """
2 Tests for running the indexing.
3 """
4 import itertools
5 import pytest
6
7 from nominatim.indexer import indexer
8 from nominatim.tokenizer import factory
9
10 class IndexerTestDB:
11
12     def __init__(self, conn):
13         self.placex_id = itertools.count(100000)
14         self.osmline_id = itertools.count(500000)
15         self.postcode_id = itertools.count(700000)
16
17         self.conn = conn
18         self.conn.set_isolation_level(0)
19         with self.conn.cursor() as cur:
20             cur.execute('CREATE EXTENSION hstore')
21             cur.execute("""CREATE TABLE placex (place_id BIGINT,
22                                                 name HSTORE,
23                                                 class TEXT,
24                                                 type TEXT,
25                                                 linked_place_id BIGINT,
26                                                 rank_address SMALLINT,
27                                                 rank_search SMALLINT,
28                                                 indexed_status SMALLINT,
29                                                 indexed_date TIMESTAMP,
30                                                 partition SMALLINT,
31                                                 admin_level SMALLINT,
32                                                 country_code TEXT,
33                                                 address HSTORE,
34                                                 token_info JSONB,
35                                                 geometry_sector INTEGER)""")
36             cur.execute("""CREATE TABLE location_property_osmline (
37                                place_id BIGINT,
38                                osm_id BIGINT,
39                                address HSTORE,
40                                token_info JSONB,
41                                indexed_status SMALLINT,
42                                indexed_date TIMESTAMP,
43                                geometry_sector INTEGER)""")
44             cur.execute("""CREATE TABLE location_postcode (
45                                place_id BIGINT,
46                                indexed_status SMALLINT,
47                                indexed_date TIMESTAMP,
48                                country_code varchar(2),
49                                postcode TEXT)""")
50             cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
51                            AS $$
52                            BEGIN
53                              IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
54                                NEW.indexed_date = now();
55                              END IF;
56                              RETURN NEW;
57                            END; $$ LANGUAGE plpgsql;""")
58             cur.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
59             cur.execute("""CREATE TYPE prepare_update_info AS (
60                              name HSTORE,
61                              address HSTORE,
62                              rank_address SMALLINT,
63                              country_code TEXT,
64                              class TEXT,
65                              type TEXT,
66                              linked_place_id BIGINT
67                            )""")
68             cur.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
69                                                      OUT result prepare_update_info)
70                            AS $$
71                            BEGIN
72                              result.address := p.address;
73                              result.name := p.name;
74                              result.class := p.class;
75                              result.type := p.type;
76                              result.country_code := p.country_code;
77                              result.rank_address := p.rank_address;
78                            END;
79                            $$ LANGUAGE plpgsql STABLE;
80                         """)
81             cur.execute("""CREATE OR REPLACE FUNCTION
82                              get_interpolation_address(in_address HSTORE, wayid BIGINT)
83                            RETURNS HSTORE AS $$
84                            BEGIN
85                              RETURN in_address;
86                            END;
87                            $$ LANGUAGE plpgsql STABLE;
88                         """)
89
90             for table in ('placex', 'location_property_osmline', 'location_postcode'):
91                 cur.execute("""CREATE TRIGGER {0}_update BEFORE UPDATE ON {0}
92                                FOR EACH ROW EXECUTE PROCEDURE date_update()
93                             """.format(table))
94
95     def scalar(self, query):
96         with self.conn.cursor() as cur:
97             cur.execute(query)
98             return cur.fetchone()[0]
99
100     def add_place(self, cls='place', typ='locality',
101                   rank_search=30, rank_address=30, sector=20):
102         next_id = next(self.placex_id)
103         with self.conn.cursor() as cur:
104             cur.execute("""INSERT INTO placex
105                               (place_id, class, type, rank_search, rank_address,
106                                indexed_status, geometry_sector)
107                               VALUES (%s, %s, %s, %s, %s, 1, %s)""",
108                         (next_id, cls, typ, rank_search, rank_address, sector))
109         return next_id
110
111     def add_admin(self, **kwargs):
112         kwargs['cls'] = 'boundary'
113         kwargs['typ'] = 'administrative'
114         return self.add_place(**kwargs)
115
116     def add_osmline(self, sector=20):
117         next_id = next(self.osmline_id)
118         with self.conn.cursor() as cur:
119             cur.execute("""INSERT INTO location_property_osmline
120                               (place_id, osm_id, indexed_status, geometry_sector)
121                               VALUES (%s, %s, 1, %s)""",
122                         (next_id, next_id, sector))
123         return next_id
124
125     def add_postcode(self, country, postcode):
126         next_id = next(self.postcode_id)
127         with self.conn.cursor() as cur:
128             cur.execute("""INSERT INTO location_postcode
129                             (place_id, indexed_status, country_code, postcode)
130                             VALUES (%s, 1, %s, %s)""",
131                         (next_id, country, postcode))
132         return next_id
133
134     def placex_unindexed(self):
135         return self.scalar('SELECT count(*) from placex where indexed_status > 0')
136
137     def osmline_unindexed(self):
138         return self.scalar("""SELECT count(*) from location_property_osmline
139                               WHERE indexed_status > 0""")
140
141
142 @pytest.fixture
143 def test_db(temp_db_conn):
144     yield IndexerTestDB(temp_db_conn)
145
146
147 @pytest.fixture
148 def test_tokenizer(tokenizer_mock, project_env):
149     return factory.create_tokenizer(project_env)
150
151
152 @pytest.mark.parametrize("threads", [1, 15])
153 def test_index_all_by_rank(test_db, threads, test_tokenizer):
154     for rank in range(31):
155         test_db.add_place(rank_address=rank, rank_search=rank)
156     test_db.add_osmline()
157
158     assert test_db.placex_unindexed() == 31
159     assert test_db.osmline_unindexed() == 1
160
161     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
162     idx.index_by_rank(0, 30)
163
164     assert test_db.placex_unindexed() == 0
165     assert test_db.osmline_unindexed() == 0
166
167     assert test_db.scalar("""SELECT count(*) from placex
168                              WHERE indexed_status = 0 and indexed_date is null""") == 0
169     # ranks come in order of rank address
170     assert test_db.scalar("""
171         SELECT count(*) FROM placex p WHERE rank_address > 0
172           AND indexed_date >= (SELECT min(indexed_date) FROM placex o
173                                WHERE p.rank_address < o.rank_address)""") == 0
174     # placex rank < 30 objects come before interpolations
175     assert test_db.scalar(
176         """SELECT count(*) FROM placex WHERE rank_address < 30
177              AND indexed_date >
178                    (SELECT min(indexed_date) FROM location_property_osmline)""") == 0
179     # placex rank = 30 objects come after interpolations
180     assert test_db.scalar(
181         """SELECT count(*) FROM placex WHERE rank_address = 30
182              AND indexed_date <
183                    (SELECT max(indexed_date) FROM location_property_osmline)""") == 0
184     # rank 0 comes after rank 29 and before rank 30
185     assert test_db.scalar(
186         """SELECT count(*) FROM placex WHERE rank_address < 30
187              AND indexed_date >
188                    (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
189     assert test_db.scalar(
190         """SELECT count(*) FROM placex WHERE rank_address = 30
191              AND indexed_date <
192                    (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
193
194
195 @pytest.mark.parametrize("threads", [1, 15])
196 def test_index_partial_without_30(test_db, threads, test_tokenizer):
197     for rank in range(31):
198         test_db.add_place(rank_address=rank, rank_search=rank)
199     test_db.add_osmline()
200
201     assert test_db.placex_unindexed() == 31
202     assert test_db.osmline_unindexed() == 1
203
204     idx = indexer.Indexer('dbname=test_nominatim_python_unittest',
205                           test_tokenizer, threads)
206     idx.index_by_rank(4, 15)
207
208     assert test_db.placex_unindexed() == 19
209     assert test_db.osmline_unindexed() == 1
210
211     assert test_db.scalar("""
212                     SELECT count(*) FROM placex
213                       WHERE indexed_status = 0 AND not rank_address between 4 and 15""") == 0
214
215
216 @pytest.mark.parametrize("threads", [1, 15])
217 def test_index_partial_with_30(test_db, threads, test_tokenizer):
218     for rank in range(31):
219         test_db.add_place(rank_address=rank, rank_search=rank)
220     test_db.add_osmline()
221
222     assert test_db.placex_unindexed() == 31
223     assert test_db.osmline_unindexed() == 1
224
225     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
226     idx.index_by_rank(28, 30)
227
228     assert test_db.placex_unindexed() == 27
229     assert test_db.osmline_unindexed() == 0
230
231     assert test_db.scalar("""
232                     SELECT count(*) FROM placex
233                       WHERE indexed_status = 0 AND rank_address between 1 and 27""") == 0
234
235 @pytest.mark.parametrize("threads", [1, 15])
236 def test_index_boundaries(test_db, threads, test_tokenizer):
237     for rank in range(4, 10):
238         test_db.add_admin(rank_address=rank, rank_search=rank)
239     for rank in range(31):
240         test_db.add_place(rank_address=rank, rank_search=rank)
241     test_db.add_osmline()
242
243     assert test_db.placex_unindexed() == 37
244     assert test_db.osmline_unindexed() == 1
245
246     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
247     idx.index_boundaries(0, 30)
248
249     assert test_db.placex_unindexed() == 31
250     assert test_db.osmline_unindexed() == 1
251
252     assert test_db.scalar("""
253                     SELECT count(*) FROM placex
254                       WHERE indexed_status = 0 AND class != 'boundary'""") == 0
255
256
257 @pytest.mark.parametrize("threads", [1, 15])
258 def test_index_postcodes(test_db, threads, test_tokenizer):
259     for postcode in range(1000):
260         test_db.add_postcode('de', postcode)
261     for postcode in range(32000, 33000):
262         test_db.add_postcode('us', postcode)
263
264     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
265     idx.index_postcodes()
266
267     assert test_db.scalar("""SELECT count(*) FROM location_postcode
268                                   WHERE indexed_status != 0""") == 0
269
270
271 @pytest.mark.parametrize("analyse", [True, False])
272 def test_index_full(test_db, analyse, test_tokenizer):
273     for rank in range(4, 10):
274         test_db.add_admin(rank_address=rank, rank_search=rank)
275     for rank in range(31):
276         test_db.add_place(rank_address=rank, rank_search=rank)
277     test_db.add_osmline()
278     for postcode in range(1000):
279         test_db.add_postcode('de', postcode)
280
281     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, 4)
282     idx.index_full(analyse=analyse)
283
284     assert test_db.placex_unindexed() == 0
285     assert test_db.osmline_unindexed() == 0
286     assert test_db.scalar("""SELECT count(*) FROM location_postcode
287                              WHERE indexed_status != 0""") == 0
288
289
290 @pytest.mark.parametrize("threads", [1, 15])
291 def test_index_reopen_connection(test_db, threads, monkeypatch, test_tokenizer):
292     monkeypatch.setattr(indexer.WorkerPool, "REOPEN_CONNECTIONS_AFTER", 15)
293
294     for _ in range(1000):
295         test_db.add_place(rank_address=30, rank_search=30)
296
297     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
298     idx.index_by_rank(28, 30)
299
300     assert test_db.placex_unindexed() == 0