Skip to content

Commit 2527624

Browse files
Merge pull request #55 from gauravbarall/fix/history-users
fix(osmsg/history.py): single remote changesets lookup and fix unfiltered users insert
2 parents 02fc11a + 3b99d2f commit 2527624

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

osmsg/history.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def ingest_remote(
178178
)
179179
if filters.users_filter:
180180
names = ", ".join(f"'{u}'" for u in filters.users_filter)
181-
changeset_preds.append(f"uid IN (SELECT uid FROM users WHERE username IN ({names}))")
181+
changeset_preds.append(f"username IN ({names})")
182182
changeset_where = " AND ".join(changeset_preds)
183183

184184
stats_preds = [in_window]
@@ -193,29 +193,35 @@ def ingest_month(month: tuple[int, int]) -> None:
193193
changefiles_src = _partition_list(history_url, "changefiles", [month])
194194
if changesets_src is not None:
195195
conn.execute(
196-
f"""INSERT INTO users
197-
SELECT uid, any_value(username) FROM {changesets_src}
198-
WHERE {in_window} AND username IS NOT NULL
196+
f"""CREATE OR REPLACE TEMP TABLE _remote_cs_month AS
197+
SELECT changeset_id, uid, username, created_at, hashtags, editor,
198+
min_lon, min_lat, max_lon, max_lat
199+
FROM {changesets_src} WHERE {changeset_where}"""
200+
)
201+
conn.execute(
202+
"""INSERT INTO users
203+
SELECT uid, any_value(username) FROM _remote_cs_month
204+
WHERE username IS NOT NULL
199205
GROUP BY uid ON CONFLICT (uid) DO NOTHING"""
200206
)
201207
conn.execute(
202-
f"""INSERT INTO changesets
208+
"""INSERT INTO changesets
203209
SELECT changeset_id, uid, created_at, hashtags, editor,
204-
CASE WHEN min_lon IS NOT NULL
210+
CASE WHEN min_lon IS NOT NULL
205211
THEN ST_MakeEnvelope(min_lon, min_lat, max_lon, max_lat) END
206-
FROM {changesets_src} WHERE {changeset_where}
212+
FROM _remote_cs_month
207213
ON CONFLICT (changeset_id) DO NOTHING"""
208214
)
209215
if changefiles_src is not None:
210216
# changefiles stores `tags` natively (LIST<STRUCT>), so ingest is a direct copy.
211217
conn.execute(
212218
f"""INSERT INTO changeset_stats
213219
SELECT changeset_id, {HISTORY_SEQ_ID} AS seq_id, uid,
214-
nodes_created, nodes_modified, nodes_deleted,
215-
ways_created, ways_modified, ways_deleted,
216-
rels_created, rels_modified, rels_deleted,
217-
poi_created, poi_modified,
218-
COALESCE(tags, []::{TAG_STRUCT_DDL}[]) AS tags
220+
nodes_created, nodes_modified, nodes_deleted,
221+
ways_created, ways_modified, ways_deleted,
222+
rels_created, rels_modified, rels_deleted,
223+
poi_created, poi_modified,
224+
COALESCE(tags, []::{TAG_STRUCT_DDL}[]) AS tags
219225
FROM {changefiles_src} WHERE {stats_where}
220226
ON CONFLICT (seq_id, changeset_id) DO NOTHING"""
221227
)
@@ -233,6 +239,7 @@ def ingest_month(month: tuple[int, int]) -> None:
233239
time.sleep(min(60, 5 * 2**attempt))
234240
advance()
235241

242+
conn.execute("DROP TABLE IF EXISTS _remote_cs_month")
236243
row = conn.execute(f"SELECT count(*) FROM changeset_stats WHERE seq_id = {HISTORY_SEQ_ID}").fetchone()
237244
return row[0] if row else 0
238245

0 commit comments

Comments
 (0)