Skip to content

Commit

Permalink
Update for null values in array
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Sep 10, 2023
1 parent 5451d24 commit 38dc7d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion backend/field_update
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ class Raw:
t2 AS (
SELECT
t1.osm_id,
COALESCE(array_agg(cg.{source_column}),ARRAY[0]::integer[]) AS aa_fids
CASE
WHEN COUNT(cg.{source_column}) = 0 THEN ARRAY[0]::integer[]
ELSE array_agg(COALESCE(cg.id, 0))
END AS aa_fids
FROM
t1
LEFT JOIN
Expand Down
7 changes: 6 additions & 1 deletion backend/replication
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,12 @@ def update_country(conn, timestamp, table_name, c_id=None, boundary=None):
"""Updates country column in table , It intersects it's newly inserted data's geometry to the boundaries loaded to underpass and populates info , in order to use multicolumn indexes with geometry"""
# array value 1000 means rest of the world
with conn.cursor() as cur:
update_query = f"""WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM {table_name} WHERE "timestamp" >= '{timestamp}'), t2 AS (SELECT t1.osm_id, COALESCE(array_agg(cg.id),ARRAY[0]::integer[]) AS aa_fids FROM t1 LEFT JOIN countries cg ON ST_Intersects(t1.geom, cg.geometry) GROUP BY t1.osm_id) UPDATE {table_name} uw SET country = t2.aa_fids FROM t2 WHERE t2.osm_id = uw.osm_id;"""
update_query = f"""WITH t1 AS (SELECT osm_id, ST_Centroid(geom) AS geom FROM {table_name} WHERE "timestamp" >= '{timestamp}'), t2 AS (SELECT t1.osm_id,
CASE
WHEN COUNT(cg.id) = 0 THEN ARRAY[0]::integer[]
ELSE array_agg(COALESCE(cg.id, 0))
END AS aa_fids
FROM t1 LEFT JOIN countries cg ON ST_Intersects(t1.geom, cg.geometry) GROUP BY t1.osm_id) UPDATE {table_name} uw SET country = t2.aa_fids FROM t2 WHERE t2.osm_id = uw.osm_id;"""

cur.execute(update_query)
conn.commit()
Expand Down

0 comments on commit 38dc7d6

Please sign in to comment.