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

Table with TIMESTAMP column fetch wrong amount of microseconds #229

Open
luminosoda opened this issue May 17, 2023 · 3 comments
Open

Table with TIMESTAMP column fetch wrong amount of microseconds #229

luminosoda opened this issue May 17, 2023 · 3 comments

Comments

@luminosoda
Copy link

luminosoda commented May 17, 2023

import jaydebeapi

from datetime import datetime

conn = jaydebeapi.connect("org.h2.Driver", url="jdbc:h2", driver_args=["login", "password"], jars="h2-2.1.212.jar")

format_ = "%Y-%m-%d %H:%M:%S.%f"
dt1 = datetime(2023, 5, 16, 18, 23, 15, 200000)
dt1_str = dt1.strftime(format_)
dt2 = datetime(2023, 5, 16, 18, 23, 15, 90000)
dt2_str = dt2.strftime(format_)

conn.cursor().execute("CREATE TABLE IF NOT EXISTS example (column TIMESTAMP)")
conn.cursor().execute(f"INSERT INTO example(column) VALUES '{dt1_str}', '{dt2_str}'")
cursor = conn.cursor()
cursor.execute("SELECT * FROM example")
values = cursor.fetchall()

dt1_fetch = datetime.strptime(values[0][0], format_)
print(dt1.microsecond, dt1_fetch.microsecond)
assert dt1 == dt1_fetch
dt2_fetch = datetime.strptime(values[1][0], format_)
print(dt2.microsecond, dt2_fetch.microsecond)
assert dt2 == dt2_fetch

dt1 = 200000, dt1_fetch = 200000
dt2 = 90000, dt2_fetch = 900000 (extra zero)

luminosoda added a commit to luminosoda/jaydebeapi that referenced this issue May 17, 2023
@luminosoda
Copy link
Author

@baztian please merge, this bug cause data inconsistency

@luminosoda
Copy link
Author

Temporary solution:

def _to_datetime_patched(rs, col):
    java_val = rs.getTimestamp(col)
    if not java_val:
        return
    d = datetime.datetime.strptime(str(java_val)[:19], "%Y-%m-%d %H:%M:%S")
    d = d.replace(microsecond=java_val.getNanos() // 1000)
    return str(d)
 
conn = jaydebeapi.connect(...)
# Only after the connection
# 93 - TIMESTAMP
jaydebeapi._converters[93] = _to_datetime_patched

@baztian
Copy link
Owner

baztian commented May 19, 2023

I will only merge changes that have an unit test

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

2 participants