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

Fix handling of timestamps when ingesting from CSV #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

daniel-thom
Copy link
Collaborator

No description provided.

Comment on lines +21 to +29
time_config = schema.time_config
exprs = []
for i, column in enumerate(rel.columns):
expr = column
if isinstance(time_config, DatetimeRange) and column == time_config.time_column:
time_type = rel.types[i]
if time_type == duckdb.typing.TIMESTAMP and time_config.start.tzinfo is not None: # type: ignore
expr = f"timezone('{time_config.start.tzinfo.key}', {column}) AS {column}" # type: ignore
exprs.append(expr)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the main problem. Now we are applying time zones.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we may not need any of the timezone enums we created in time.py since we can use tzinfo directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be the case. We might need them if we accept user input definitions as command line arguments or in configuration files.

table.create(self._engine)

with self._engine.begin() as conn:
write_database(rel.to_df(), conn, dst_schema)
try:
check_timestamps(conn, table, dst_schema)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous behavior was incorrect. We need to perform checks on the final table and rollback on error.


from chronify.exceptions import InvalidTable
from chronify.models import TableSchema
from chronify.sqlalchemy.functions import read_database
from chronify.utils.sql import make_temp_view_name


def check_timestamps(conn: Connection, table: Table, schema: TableSchema) -> None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semantics of this are now different. The user calls this function instead of instantiating the class. The function receives the connection instead of the engine and metadata because this needs to happen within a transaction so that erroneous changes can be rolled back.

@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 91.54930% with 12 lines in your changes missing coverage. Please review.

Project coverage is 93.50%. Comparing base (8e4a674) to head (ba28060).

Files with missing lines Patch % Lines
src/chronify/models.py 70.00% 9 Missing ⚠️
tests/test_store.py 91.42% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #16      +/-   ##
==========================================
- Coverage   94.24%   93.50%   -0.75%     
==========================================
  Files          18       18              
  Lines         782      831      +49     
==========================================
+ Hits          737      777      +40     
- Misses         45       54       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

filters = [f"{x} IS NOT NULL" for x in schema.time_config.list_time_columns()]
def _run_timestamp_checks_on_tmp_table(self, table_name: str) -> None:
id_cols = ",".join(self._schema.time_array_id_columns)
filters = [f"{x} IS NOT NULL" for x in self._schema.time_config.list_time_columns()]
where_clause = "AND ".join(filters)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be " AND ", space in front of and behind.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed.

Comment on lines +21 to +29
time_config = schema.time_config
exprs = []
for i, column in enumerate(rel.columns):
expr = column
if isinstance(time_config, DatetimeRange) and column == time_config.time_column:
time_type = rel.types[i]
if time_type == duckdb.typing.TIMESTAMP and time_config.start.tzinfo is not None: # type: ignore
expr = f"timezone('{time_config.start.tzinfo.key}', {column}) AS {column}" # type: ignore
exprs.append(expr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we may not need any of the timezone enums we created in time.py since we can use tzinfo directly.

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

Successfully merging this pull request may close these issues.

3 participants