Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying of date/datetime with year 1-999 is incorrect on some platforms #404

Open
kotofos opened this issue Aug 11, 2021 · 0 comments
Open

Comments

@kotofos
Copy link

kotofos commented Aug 11, 2021

Pyhive sqlachemy dialect can not do 'where' filtering for dates/timestamps with year lower than 1000 because leading zeros are omitted when %Y is used.
Years 1-999 are rare is real use, but may be used for relative timestamps for example, or when only time part is used.

This problem may appear on some systems depending of strftime implementation of system libraries. See https://bugs.python.org/issue13305

problematic method: pyhive.common.ParamEscaper.escape_datetime

This is a issue with python itself, but a workaround can be implemented for this case.
One solution is to use %04Y to set fixed with of 4, but it is also platform dependent.
Another is to use hack:

dt_str = '{:0>4}{}'.format(
    item.strftime('%Y'),
    item.strftime(format_.lstrip('%Y'))
)

This should be portable.

Reproduce

CREATE TABLE DateTimeTable(
    date DATE,
    timestamp TIMESTAMP
);
insert into DateTimeTable values ('0001-01-01', '0001-01-01T00:00:00.000000Z');
from datetime import datetime
from sqlalchemy import MetaData

...
meta = MetaData()
meta.reflect(bind=dbfs_engine)

ts = datetime.strptime('0001-01-01T00:00:00.000000', "%Y-%m-%dT%H:%M:%S.%f")
print(ts.strftime('%04Y-%m-%d %H:%M:%S.%f'))
print(ts.strftime('%Y-%m-%d %H:%M:%S.%f'))

table = meta.tables['datetimetable']
q = table.select(table.c['timestamp'] == ts)
q.bind = dbfs_engine
res = dbfs_engine.execute(q)

for r in res:
    print(r)
# Nothing here
# Expected 1 row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant