Skip to content

Add when_matched support to non-idempotent time-range materialization#11

Open
0xRobin wants to merge 2 commits intoTobikoData:mainfrom
0xRobin:robin/add-when-matched-non-idempotent-time-range
Open

Add when_matched support to non-idempotent time-range materialization#11
0xRobin wants to merge 2 commits intoTobikoData:mainfrom
0xRobin:robin/add-when-matched-non-idempotent-time-range

Conversation

@0xRobin
Copy link

@0xRobin 0xRobin commented Mar 16, 2026

Summary

  • Add optional materialization_properties.when_matched support to non_idempotent_incremental_by_time_range.
  • Parse and normalize when_matched using SQLMesh's expression validation and merge-alias replacement semantics, then pass it through to adapter.merge(...).
  • Add unit coverage to verify custom WHEN MATCHED clauses are respected in generated MERGE SQL.

Why

non_idempotent_incremental_by_time_range currently always updates all columns on key match. This makes it impossible to keep immutable ingestion metadata such as _inserted_at unchanged on updates. Supporting when_matched gives model authors explicit control over update behavior while preserving existing defaults when unset.

Implementation Notes

This follows SQLMesh's existing INCREMENTAL_BY_UNIQUE_KEY flow:

Validation

  • uv run pytest tests/materializations/test_non_idempotent_incremental_by_time_range.py -q

Parse and validate optional materialization_properties.when_matched and pass it through to adapter.merge so update clauses can preserve immutable columns like _inserted_at.
@0xRobin 0xRobin force-pushed the robin/add-when-matched-non-idempotent-time-range branch from bfb9ef6 to bd005ed Compare March 16, 2026 10:13
@0xRobin 0xRobin marked this pull request as ready for review March 16, 2026 10:15
@crericha
Copy link

Can you add more test cases for the other options supported?

  • Lists
  • clause in parens
  • whitespace before/after clause

Add test cases for list input, parenthesized clause, and whitespace handling per reviewer request. Fix mypy union-attr errors by adding isinstance checks before accessing when_matched.
@0xRobin
Copy link
Author

0xRobin commented Mar 17, 2026

@crericha done.

@crericha crericha enabled auto-merge (squash) March 17, 2026 19:38
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.

2 participants