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

feat: support datetime objects in literal instantiation #1542

Merged
merged 4 commits into from
Jan 22, 2025

Conversation

jayceslesar
Copy link
Contributor

@jayceslesar jayceslesar commented Jan 20, 2025

Support datetime objects being passed into the literal function.

Allows situations like the following:

from datetime import datetime
...
row_filter=And(
            GreaterThanOrEqual("my_timestamp_column", datetime.now()),
        ),

Which previously failed

Closes #1456

@kevinjqliu
Copy link
Contributor

Thanks for the PR! Lets add a test for the example in the description with row_filter

@jayceslesar
Copy link
Contributor Author

Thanks for the PR! Lets add a test for the example in the description with row_filter

Added, let me know if we want anything more explicit there

def test_scan_with_datetime(catalog: Catalog) -> None:
table = create_table(catalog)
# test that this doesn't raise an exception...
table.scan(row_filter=GreaterThanOrEqual("datetime", datetime.now())).to_pandas()
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: the table has datetime column, what if we add a row and then filter datetime before and after and assert the results.

maybe something like,

yesterday = ...
table.append({"datatime": yesterday)
assert len(table.scan(row_filter=GreaterThanOrEqual("datetime", datetime.now()))) == 0
assert len(table.scan(row_filter=LessThanOrEqual("datetime", datetime.now()))) == 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, looking much better now, took me too long to figure out I can run make test-integration PYTEST_ARGS='-k test_scan_with_datetime' to only select that test hahaha

Copy link
Contributor

@kevinjqliu kevinjqliu left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks again

@kevinjqliu kevinjqliu requested a review from Fokko January 21, 2025 06:06
Copy link
Contributor

@Fokko Fokko left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for adding this @jayceslesar 🙌 Thanks for the review @kevinjqliu 🚀

@@ -145,6 +147,8 @@ def literal(value: L) -> Literal[L]:
return BinaryLiteral(value)
elif isinstance(value, Decimal):
return DecimalLiteral(value)
elif isinstance(value, datetime):
return TimestampLiteral(datetime_to_micros(value))
Copy link
Contributor

Choose a reason for hiding this comment

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

The tzinfo is handled here:

def datetime_to_micros(dt: datetime) -> int:
"""Convert a datetime to microseconds from 1970-01-01T00:00:00.000000."""
if dt.tzinfo:
delta = dt - EPOCH_TIMESTAMPTZ
else:
delta = dt - EPOCH_TIMESTAMP
return (delta.days * 86400 + delta.seconds) * 1_000_000 + delta.microseconds

@jayceslesar
Copy link
Contributor Author

this introduces sort of a tricky typing problem...

tests/expressions/test_literals.py:910: error: Value of type variable "L" of "literal" cannot be "datetime"  [type-var]
tests/integration/test_reads.py:976: error: Value of type variable "L" of "GreaterThanOrEqual" cannot be "datetime"  [type-var]
tests/integration/test_reads.py:979: error: Value of type variable "L" of "LessThan" cannot be "datetime"  [type-var]

Will check it out tonight but can't just fix by adding datetime to the L type

@kevinjqliu kevinjqliu merged commit 2cd4e78 into apache:main Jan 22, 2025
7 checks passed
@kevinjqliu
Copy link
Contributor

Thanks for the contribution @jayceslesar! and thanks for the review @Fokko :)

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.

datetime objects in row_filter expressions are not casted and raise an error
3 participants