Model-level data retention (TTL): automatically delete rows older than N days

Đang mở
#6,124 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
15/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, sql
Lĩnh vực
data-engineering

Hướng nghiên cứu

The payload names no source files. Start from how the MODEL definition's options are parsed and how incremental kinds such as INCREMENTAL_BY_TIME_RANGE declare their time_column, then look at where post-materialization statements run. Done would need a design agreed with maintainers covering engine-specific deletion (DELETE versus partition drop) and re-enforcement after full refreshes, which the issue leaves open.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Feature Improvement

Is your feature request related to a problem?

Incremental models accumulate history forever. There is no native way to express "keep the last N days" for a model's physical table:

  • start on INCREMENTAL_BY_TIME_RANGE / INCREMENTAL_BY_RANGE bounds what gets processed going forward, but nothing ever removes old data.
  • The janitor only cleans SQLMesh's own metadata (snapshots, environments), not rows in model tables.
  • Warehouse-native mechanisms don't cover this: on Snowflake, DATA_RETENTION_TIME_IN_DAYS only controls Time Travel, it does not expire rows from the live table; other engines have no equivalent at all.

Workarounds and why they fall short

  1. Filter in the model query (WHERE event_ts >= CURRENT_DATE - 30): new old rows never land, but rows written before the filter was added stay until a full rebuild, and backfills against full-history sources silently recompute only the window.
  2. External cleanup task (DELETE FROM ... WHERE event_ts < ... from the orchestrator or a hand-rolled model): works, but retention lives outside the model definition. A breaking change and full refresh of the model resurrects the old rows, and there is nothing in the plan diff that tells a reviewer the table has a retention policy attached.

Proposed solution

A model-level retention/TTL option, e.g.:

MODEL (
  name analytics.events,
  kind INCREMENTAL_BY_TIME_RANGE (
    time_column event_ts
  ),
  retention 30 days
);

SQLMesh would enforce it by issuing a DELETE on the time column after materialization (or, on engines that support it, dropping whole partitions/files). Key properties that make it a feature rather than a snippet:

  • Declarative and visible in the model definition and plan diffs.
  • Applied automatically after full refreshes/backfills, so rebuilding a model can never resurrect expired rows.
  • Engine-aware: partition drop where available (e.g. Iceberg), DELETE elsewhere.

Alternatives considered

A documented macro recipe or a post statement doing the DELETE. We already do this today, but it is invisible in the plan, easy to forget in new models, and re-runs/full refreshes don't re-enforce it.

Environment

  • Open-source SQLMesh with Snowflake as the main engine, orchestrated via Airflow.
Ngôn ngữ chính
Python
Star
3.3k
Fork
471
Merge trung bình
6 ngày 7 giờ
Pull request đã merge (30 ngày)
35

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của SQLMesh/sqlmesh

Tất cả issue của SQLMesh/sqlmesh

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.