Skip to content

fix(duckdb): compare DuckDB timestamps with zoned columns in any time zone - #11523

Open
wbeckler wants to merge 1 commit into
vortex-data:developfrom
wbeckler:wbeckler/duckdb-zoned-filter-literals
Open

wbeckler wants to merge 1 commit into
vortex-data:developfrom
wbeckler:wbeckler/duckdb-zoned-filter-literals

Conversation

@wbeckler

Copy link
Copy Markdown

Summary

A filter or an ORDER BY ... LIMIT on a zoned timestamp column fails to bind in DuckDB unless the column's time zone is UTC, for example with Comparison requires equal dtypes ignoring nullability, got vortex.timestamp[µs, tz=America/New_York](i64?) and vortex.timestamp[µs, tz=UTC](i64?). DuckDB's TIMESTAMP WITH TIME ZONE has no time zone, so its constants and the dynamic value of a Top-N filter convert to Vortex timestamps in UTC, and Vortex only compares timestamps of the same time zone.

Both sides are UTC instants, so this gives those values the time zone of the column they are compared with.

Changes

  • New convert/time_zone.rs: align_time_zones walks a converted filter. Where a node has an operand that is a zoned timestamp over the scan's dtype, its timestamp literals (and lists of them, for IN) of the same unit take that operand's time zone, and a dynamic comparison is rebuilt with the operand's dtype. Nothing else changes, and a column in UTC leaves the expression as it is.
  • try_from_table_filter and try_from_bound_expression apply it to what they return, so table filters, expression filters, complex filters and filters under a pushed aggregate are covered. try_from_bound_expression takes the scan's dtype for this.

I did the rewrite after conversion, not where each literal is made, because the converter builds literals without knowing the dtype of the column next to them.

Tests

  • test_vortex_scan_zoned_timestamp_filters: =, >=, BETWEEN, IN, a filter with OR (the complex filter path) and a Top-N, each over a file in UTC and one in America/New_York. The six America/New_York cases fail on develop.
  • Unit tests for align_time_zones: a comparison, a BETWEEN and an IN list take the column's zone and then bind; a literal in the same zone, in another unit or without a zone, and a column without a zone, are left alone.
  • Ran locally on macOS arm64, each with and without VORTEX_SCAN_V2=1: cargo test -p vortex-duckdb --lib (289 passed, the two S3 tests ignored) and cargo test -p vortex-sqllogictest --test sqllogictests (81 passed; TPC-H, TPC-DS and ClickBench ignored without local data). Clippy with -D warnings, fmt on the pinned nightly and typos are clean.

Notes

  • The dtype of a dynamic comparison's value is not readable from outside vortex-array, so the rewrite rebuilds the comparison whenever its operand is in a zone other than UTC. An accessor would let it skip the ones that already match.
  • Independent of duckdb: support TIMESTAMP_TZ_NS #11521; the two do not touch the same lines.

AI assistance

Written by Claude Code (Claude Fable 5.2), an AI coding agent, working at my direction. It also ran the checks listed above and opened this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SMtfpBcKnfP3CkWWQL6mit

… zone

A filter or a Top-N on a zoned timestamp column failed to bind unless
the column's time zone was UTC. DuckDB's zoned timestamps have no time
zone, so their constants and dynamic filter values convert to Vortex
timestamps in UTC, and Vortex only compares timestamps of the same time
zone.

Give those values the time zone of the column they are compared with
once a pushed filter is converted. Both sides are UTC instants.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SMtfpBcKnfP3CkWWQL6mit
Signed-off-by: William Beckler <wbeckler@gmail.com>

@myrrc myrrc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The right way to solve this is to teach Vortex how to compare timestamps with different time zones, jiff already supports this

Comment thread vortex-duckdb/src/convert/time_zone.rs Resolved
@wbeckler

Copy link
Copy Markdown
Author

Thanks for the review. Agreed that handling this in the comparison itself is cleaner than rewriting literals in the DuckDB converter, and it would cover the other integrations too.

One thing to check before I rewrite it: #11016 made comparisons require equal dtypes, and it calls out timestamps with different time zones as one of the cases that now errors. Should validate_comparison_dtypes be relaxed for timestamps that have the same unit and differ only in time zone? Both sides are UTC instants, so the stored values compare directly. Or did you have something else in mind with jiff? cc @connortsui20 since this touches #11016.

If that is the direction, I'd do the core change as its own PR and reduce this one to the .slt test. I'd also close #11551, since the accessor was only needed for the rewrite here. Either way I'll move the test to an .slt and drop the module comment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/feature A new feature ext/duckdb Relates to the DuckDB integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants