Skip to content

fix: Restore feature table timestamp bounds in Snowflake point-in-time join - #7230

Open
daniel-kristjansson wants to merge 3 commits into
feast-dev:masterfrom
daniel-kristjansson:fix-snowflake-pit-timestamp-bounds
Open

daniel-kristjansson wants to merge 3 commits into
feast-dev:masterfrom
daniel-kristjansson:fix-snowflake-pit-timestamp-bounds

Conversation

@daniel-kristjansson

@daniel-kristjansson daniel-kristjansson commented Nov 24, 2026 •

Copy link
Copy Markdown

What this PR does / why we need it:

Partially restores feature-table pruning in the Snowflake point-in-time join. This addresses a performance regression introduced by #4850 ("Use ASOF JOIN in Snowflake offline store query", released in v0.43.0).

Before #4850, the Snowflake template filtered each feature table on its timestamp column, using the entity dataframe's timestamp range:

FROM {{ featureview.table_subquery }}
WHERE "{{ featureview.timestamp_field }}" <= '{{ featureview.max_event_timestamp }}'
{% if featureview.ttl %}AND "{{ featureview.timestamp_field }}" >= '{{ featureview.min_event_timestamp }}'{% endif %}

#4850 replaced the dedup + LEFT JOIN template with the faster ASOF JOIN template, but dropped both predicates. The ASOF template has no filter on the feature table at all. On every get_historical_features call, Snowflake scans every micro-partition of the feature table: clustering on the timestamp column cannot prune, and the TTL does not limit the scan.

This PR restores both bounds inside the ASOF template's __subquery CTE, cast as TIMESTAMP '...' to match get_timestamp_filter_sql:

  • "<timestamp_field>" <= TIMESTAMP '<max entity timestamp + 1 ms>', always emitted.
  • "<timestamp_field>" >= TIMESTAMP '<min entity timestamp − TTL>', emitted only when ttl > 0. With unlimited TTL there is no lower bound.

The predicates may only remove rows that the ASOF JOIN and the per-row TTL filter would discard anyway. To guarantee that:

  • Bounds come from the uploaded entity snapshot. A SQL entity_df is re-executed by _upload_entity_df when the job materializes, so the range is measured with MIN/MAX on the uploaded temp table, not reused from get_historical_features() time. A DataFrame entity_df is uploaded unchanged, so its in-memory range is used. RetrievalMetadata.min_event_timestamp / max_event_timestamp still report the range measured when the job is created.
  • The TTL is subtracted as UTC elapsed time. The minimum entity timestamp is converted to UTC before the TTL is subtracted. With coerce_tz_aware=False and a ZoneInfo timestamp, subtracting first is wall-clock arithmetic and is off by an hour across DST, while the SQL TTL counts elapsed seconds.
  • The upper bound is padded by 1 ms. Python datetimes hold microseconds, while the entity rows and feature table can hold nanoseconds.

The change is limited to the Snowflake store. The shared helpers in offline_utils.py are unchanged because BigQuery, Redshift and other stores also use them. Those stores can be fixed separately.

Testing

  • tests/unit/infra/offline_stores/test_snowflake.py: renders the point-in-time SQL without a Snowflake account and checks the bounds for sub-microsecond entity timestamps, a spring-forward DST minimum, and a SQL entity_df whose result changes between job creation and upload.
  • tests/integration/offline_store/test_snowflake_pruning.py: the same three cases end to end against Snowflake. Each one asserts that a feature row eligible for the join is still returned. These tests are skipped unless SNOWFLAKE_CI_DEPLOYMENT is set, and make test-python-integration currently excludes Snowflake (-k "not snowflake"), so they run only manually for now. They pass against a real account, and the stale-SQL and DST tests fail without the fix.

Benchmarks

We measured get_historical_features on production Snowflake feature tables clustered on the timestamp column. We compared two feature tables with different layouts, at entity dataframe sizes from ~100k to ~13M rows:

Feature table / entity rows Data scanned (with fix vs. without) Speedup
Table A / ~100k 11.1 GB vs 62.7 GB (−82%) 1.6×
Table A / ~13M 16.2 GB vs 73.5 GB (−78%) 3.1×
Table B / ~100k 16.0 GB vs 105.3 GB (−85%) 3.1×
Table B / ~6M 10.0 GB vs 65.5 GB (−85%) 6.0×

Devin Review

…e join

Before feast-dev#4850 the Snowflake point-in-time template filtered each feature
table on its timestamp column, using the entity dataframe's timestamp
range: an upper bound at max_event_timestamp and, when the feature view
has a TTL, a lower bound at min_event_timestamp. The ASOF JOIN template
introduced in feast-dev#4850 dropped both predicates, so the feature table is
scanned in full on every get_historical_features call and Snowflake
cannot prune micro-partitions on the timestamp column.

Restore both bounds in the template's subquery CTE, reusing the existing
query context values. Each bound keeps a superset of the rows the ASOF
JOIN and the TTL filter retain, so results are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Daniel Kristjansson <danielk@cuymedia.net>
@daniel-kristjansson
daniel-kristjansson requested a review from a team as a code owner November 24, 2026 16:59
@daniel-kristjansson daniel-kristjansson changed the title fix: Restore feature table timestamp bounds in Snowflake point-in-tim… fix: Snowflake performance regression fix (timestamp bounds) Nov 24, 2026

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 3 potential issues.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@daniel-kristjansson daniel-kristjansson changed the title fix: Snowflake performance regression fix (timestamp bounds) fix: Restore feature table timestamp bounds in Snowflake point-in-time join Nov 24, 2026
Addresses the Devin review comment "Nanosecond matches lost to
truncation":
feast-dev#7230 (comment)

For a pandas entity dataframe, _get_entity_df_event_timestamp_range
calls to_pydatetime(), which drops sub-microsecond digits, while the
upload to Snowflake keeps them. The upper bound on the feature table
could therefore be earlier than the latest entity timestamp and drop
eligible feature rows in its final microsecond.

Move the upper bound used for the feature table filter up by one
millisecond. The retrieval job's metadata keeps the exact range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Daniel Kristjansson <danielk@cuymedia.net>
entity_df_event_timestamp_range,
(
entity_df_event_timestamp_range[0],
entity_df_event_timestamp_range[1] + timedelta(milliseconds=1),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What does this mean?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When we do "entity_df_event_timestamp.max().to_pydatetime()" it drops the nanoseconds, so anything happening in the entity_df_event_timestamp_range[1] microsecond could would get filtered out before hitting our point-in-time query which is executed at full precision. Adding one microsecond would be enough padding but I figured there was no harm in adding a millisecond as the point-in-time query will filter any strays out.

@franciscojavierarceo franciscojavierarceo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the upper-bound precision padding addresses the nanosecond finding, but the other two pruning concerns still affect this Snowflake change. the range is measured before the retrieval job is returned, while _upload_entity_df reruns SQL when the job materializes, so a newer eligible row can be pruned using the old maximum. the DST lower bound also subtracts TTL before UTC conversion: with coerce_tz_aware=False, noon in New York on March 9 with a 24-hour TTL prunes a valid March 8 16:30 UTC feature. sharing that helper with other stores does not make either new predicate safe here. we need bounds from the uploaded entity snapshot and UTC elapsed-time arithmetic, with regressions for changing SQL input, spring-forward, and sub-microsecond timestamps before merging.

The feature-table pruning predicates in the point-in-time join could drop
rows the ASOF join and per-row TTL would keep:

- A SQL entity_df is executed again by _upload_entity_df when the job
  materializes. The bounds came from the earlier MIN/MAX, so newer entity
  rows lost their features. Measure MIN/MAX from the uploaded temp table
  instead. RetrievalMetadata keeps the range measured up front.
- With coerce_tz_aware=False a ZoneInfo minimum made the TTL subtraction
  wall-clock arithmetic, an hour off across DST. Convert the minimum to UTC
  before the TTL is subtracted.

The 1 ms upper-bound padding for sub-microsecond entity timestamps stays.
The change is limited to the Snowflake store; the shared helpers in
offline_utils are unchanged.

Adds Snowflake integration regressions for the stale SQL snapshot, DST and
sub-microsecond cases, and unit tests that check the rendered bounds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Daniel Kristjansson <danielk@cuymedia.net>
@daniel-kristjansson-sh

Copy link
Copy Markdown

I added the regression tests & added fixes for the pruning bound freshness & UTC issues.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants