Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85b6aa0d0a | ||
|
|
e62aad148b |
@@ -3,11 +3,7 @@
|
||||
-- Broad coverage (140K+ locations) enables Gemeinde-level market intelligence.
|
||||
-- One row per geoname_id (GeoNames stable numeric identifier).
|
||||
--
|
||||
-- Supports two landing formats (UNION ALL during migration):
|
||||
-- New: cities_global.jsonl.gz — one city per line, columns directly accessible
|
||||
-- Old: cities_global.json.gz — {"rows": [...]} blob (UNNEST required)
|
||||
--
|
||||
-- Source: data/landing/geonames/{year}/{month}/cities_global.{jsonl,json}.gz
|
||||
-- Source: data/landing/geonames/{year}/{month}/cities_global.jsonl.gz
|
||||
|
||||
MODEL (
|
||||
name staging.stg_population_geonames,
|
||||
@@ -16,17 +12,14 @@ MODEL (
|
||||
grain geoname_id
|
||||
);
|
||||
|
||||
WITH
|
||||
-- New format: one city per JSONL line
|
||||
jsonl_rows AS (
|
||||
SELECT
|
||||
TRY_CAST(geoname_id AS INTEGER) AS geoname_id,
|
||||
city_name,
|
||||
country_code,
|
||||
TRIM(city_name) AS city_name,
|
||||
UPPER(country_code) AS country_code,
|
||||
TRY_CAST(lat AS DOUBLE) AS lat,
|
||||
TRY_CAST(lon AS DOUBLE) AS lon,
|
||||
admin1_code,
|
||||
admin2_code,
|
||||
NULLIF(TRIM(admin1_code), '') AS admin1_code,
|
||||
NULLIF(TRIM(admin2_code), '') AS admin2_code,
|
||||
TRY_CAST(population AS BIGINT) AS population,
|
||||
TRY_CAST(ref_year AS INTEGER) AS ref_year,
|
||||
CURRENT_DATE AS extracted_date
|
||||
@@ -40,50 +33,8 @@ jsonl_rows AS (
|
||||
}
|
||||
)
|
||||
WHERE geoname_id IS NOT NULL
|
||||
),
|
||||
-- Old format: {"rows": [...]} blob — kept for transition
|
||||
blob_rows AS (
|
||||
SELECT
|
||||
TRY_CAST(row ->> 'geoname_id' AS INTEGER) AS geoname_id,
|
||||
row ->> 'city_name' AS city_name,
|
||||
row ->> 'country_code' AS country_code,
|
||||
TRY_CAST(row ->> 'lat' AS DOUBLE) AS lat,
|
||||
TRY_CAST(row ->> 'lon' AS DOUBLE) AS lon,
|
||||
row ->> 'admin1_code' AS admin1_code,
|
||||
row ->> 'admin2_code' AS admin2_code,
|
||||
TRY_CAST(row ->> 'population' AS BIGINT) AS population,
|
||||
TRY_CAST(row ->> 'ref_year' AS INTEGER) AS ref_year,
|
||||
CURRENT_DATE AS extracted_date
|
||||
FROM (
|
||||
SELECT UNNEST(rows) AS row
|
||||
FROM read_json(
|
||||
@LANDING_DIR || '/geonames/*/*/cities_global.json.gz',
|
||||
auto_detect = true,
|
||||
maximum_object_size = 40000000
|
||||
)
|
||||
)
|
||||
WHERE (row ->> 'geoname_id') IS NOT NULL
|
||||
),
|
||||
all_rows AS (
|
||||
SELECT * FROM jsonl_rows
|
||||
UNION ALL
|
||||
SELECT * FROM blob_rows
|
||||
)
|
||||
SELECT
|
||||
geoname_id,
|
||||
TRIM(city_name) AS city_name,
|
||||
UPPER(country_code) AS country_code,
|
||||
lat,
|
||||
lon,
|
||||
NULLIF(TRIM(admin1_code), '') AS admin1_code,
|
||||
NULLIF(TRIM(admin2_code), '') AS admin2_code,
|
||||
population,
|
||||
ref_year,
|
||||
extracted_date
|
||||
FROM all_rows
|
||||
WHERE population IS NOT NULL
|
||||
AND population IS NOT NULL
|
||||
AND population > 0
|
||||
AND geoname_id IS NOT NULL
|
||||
AND city_name IS NOT NULL
|
||||
AND lat IS NOT NULL
|
||||
AND lon IS NOT NULL
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
"""Create minimal seed files for SQLMesh staging models that require landing data."""
|
||||
"""Create minimal seed files for SQLMesh staging models that require landing data.
|
||||
|
||||
Seeds are empty JSONL gzip files — they satisfy DuckDB's file-not-found check
|
||||
while contributing zero rows to the staging models.
|
||||
"""
|
||||
import gzip
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
seed = {
|
||||
"date": "1970-01-01",
|
||||
"captured_at_utc": "1970-01-01T00:00:00Z",
|
||||
"venue_count": 0,
|
||||
"venues_errored": 0,
|
||||
"venues": [],
|
||||
}
|
||||
morning = Path("data/landing/playtomic/1970/01/availability_1970-01-01.json.gz")
|
||||
recheck = Path("data/landing/playtomic/1970/01/availability_1970-01-01_recheck_00.json.gz")
|
||||
# stg_playtomic_availability requires at least one morning and one recheck file
|
||||
morning = Path("data/landing/playtomic/1970/01/availability_1970-01-01.jsonl.gz")
|
||||
recheck = Path("data/landing/playtomic/1970/01/availability_1970-01-01_recheck_00.jsonl.gz")
|
||||
morning.parent.mkdir(parents=True, exist_ok=True)
|
||||
for p in [morning, recheck]:
|
||||
if not p.exists():
|
||||
with gzip.open(p, "wt") as f:
|
||||
json.dump(seed, f)
|
||||
with gzip.open(p, "wb") as f:
|
||||
pass # empty JSONL — 0 rows, no error
|
||||
print("created", p)
|
||||
else:
|
||||
print("exists ", p)
|
||||
|
||||
Reference in New Issue
Block a user