Skip to content

SQL date parser should gracefully handle invalid dates #146

Closed

Description

@geohacker

Bug description
We ran into this issue over at #144. The date SQL functions to parse dates was throwing an error for invalid dates and causing tiles to not generate.

Repro Steps
The data is now fixed on production, but on staging you should be able to run a query like:

SELECT osm_id from osm_admin_areas where tags->'end_date' = '1917-04-31';

Activity

  1. danrademacher commented on Sep 1, 2020

    @danrademacher

    I think this should be a change to paddate over to https://github.com/OpenHistoricalMap/paddate-python

    We should have a two-step solution here:

    1. Add some "Invalid date" messaging so we can see that in SQL console
    2. EITHER return a null value (so tiles don't break) or even better salvage as much of the date as we can -- yyyy-mm if the day portion is invalid or just yyyy if the month and day are wrong, and null if all three are somehow invalid (though years if integer can be presumed correct I think).
  2. gregallensworth commented on Sep 3, 2020

    @gregallensworth

    Correction: Dan's link is to a Python libary. SQL functions for PostgreSQL are at https://github.com/OpenHistoricalMap/DateFunctions-plpgsql/blob/master/datefunctions.sql

    This would be relevant to isodatetodecimaldate for invalid dates such as:

    # SELECT isodatetodecimaldate('1917-04-31');
    ERROR:  Not a valid date 1917, 4, 31
    CONTEXT:  PL/pgSQL function isodatetodecimaldate(character varying) line 18 at RAISE
    
  3. hroest commented on Sep 3, 2020

    @hroest

    @gregallensworth is the process how these dates get processed documented somewhere? Which script runs first, the SQL or the Python script? Where would approximate dates be handled (e.g. "1950s" or "1950..1960") if we were to implement this?

  4. added a commit that references this issue on Sep 3, 2020
  5. gregallensworth commented on Sep 3, 2020

    @gregallensworth

    I have enhanced isodatetodecimaldate(varchar) now accepts an optional second parameter. See OpenHistoricalMap/DateFunctions-plpgsql#1

    The optional parameter trytofixinvalid defines the handling for invalid dates such as 1917-28-31 or 2020-13-31

    • NULL (default) = an invalid date will raise an exception
    • FALSE = an invalid date will return NULL
    • TRUE = try hard to return a number; return the 1st of the month if the month is valid, or the year if even the month is invalid

    Therefore, you now have a choice of how to handle invalid dates:

    • current/legacy = exception
    • add FALSE = return null
    • add TRUE = return whatever we can
  6. danrademacher commented on Sep 3, 2020

    @danrademacher

    Great! So I think to get this into production, @geohacker or others at DevSeed would need to both implement the updated function on the tilerservers and add the trytofixinvalid param to every instance of isodatetodecimaldate() in this file, https://github.com/developmentseed/osm-seed/blob/ohm/tiler-server/config/config.toml

    @hroest I'll file a separate issue on handling uncertain dates and can say more over there. Stand by.

  7. geohacker commented on Feb 3, 2021

    @geohacker
    Author

    This is now deployed on production, so let's close. Thanks so much @gregallensworth and @danrademacher for your work here.

  8. added theissue type on Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions