Fix census download crash at 100K: integer out of range
safe_int now clamps values to PostgreSQL INTEGER max (2.1B) and handles scientific notation. Mileage columns changed to BIGINT on prod since carriers can have >2B annual miles. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b59b266a80
commit
1cfda2c119
1 changed files with 7 additions and 4 deletions
|
|
@ -59,13 +59,16 @@ def parse_fmcsa_date(date_str: str | None) -> str | None:
|
|||
return None
|
||||
|
||||
|
||||
def safe_int(val) -> int | None:
|
||||
"""Convert value to int, handling strings and None."""
|
||||
def safe_int(val, max_val: int = 2_147_483_647) -> int | None:
|
||||
"""Convert value to int, handling strings, None, and out-of-range values."""
|
||||
if val is None:
|
||||
return None
|
||||
try:
|
||||
return int(val)
|
||||
except (ValueError, TypeError):
|
||||
v = int(float(val)) # handle "1.234e7" scientific notation
|
||||
if v > max_val or v < -max_val:
|
||||
return None # out of range for PostgreSQL INTEGER
|
||||
return v
|
||||
except (ValueError, TypeError, OverflowError):
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue