Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

tests/integration/test_reads.py Show resolved Hide resolved
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 🚀

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.