Skip to content

Commit

Permalink
Add order by input h3 id in sql command
Browse files Browse the repository at this point in the history
  • Loading branch information
zacdezgeo committed Dec 12, 2024
1 parent 4b31864 commit 1226286
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions space2stats_api/src/space2stats/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ def _get_summaries(self, fields: List[str], h3_ids: List[int]):
SELECT {0}
FROM {1}
WHERE hex_id = ANY (%s)
ORDER BY array_position(%s, hex_id)
"""
).format(pg.sql.SQL(", ").join(cols), pg.sql.Identifier(self.table_name))

# Convert h3_ids to integers to ensure compatibility with psycopg
h3_ids = [
scalar.as_py() if hasattr(scalar, "as_py") else scalar for scalar in h3_ids
]
h3_id_strings = cells_to_string(h3_ids).to_pylist()

with self.conn.cursor() as cur:
cur.execute(
sql_query,
[
cells_to_string(h3_ids).to_pylist(),
],
h3_id_strings,
h3_id_strings,
], # Pass h3_id_strings twice: once for ANY, once for array_position
)
rows = cur.fetchall()
colnames = [desc[0] for desc in cur.description]
Expand Down

0 comments on commit 1226286

Please sign in to comment.