Datakey caches over ranges of days
The tutorial caches one date per query. A dashboard asks for ranges: a line over the last 7 days, a total over a month. With the tutorial's model, a 3-day query never reads the daily rows, for two reasons:
- Its parameters are the range.
KS_COUNTRIESandKS_ORDERSreceive another value, so every datakey differs from the daily ones. - Its sources read one date.
ON SELF[ds] = EVAL_CONST(DS AS DATE)takes one value per query. A range has no singleDS.
The fix is to declare on each keyset what one partition is, and to key each cached value by something that carries its day. FeatureMesh then knows that a range is the union of its days, and reads one row per day.
Declare the partition on the keyset
A third day of orders lands, and a cache table gets one row per day:
INSERT INTO docs_cache.orders VALUES (DATE '2026-10-09', 107, 1, 12);
DROP TABLE IF EXISTS docs_cache.cache_per_day;
CREATE TABLE docs_cache.cache_per_day (ds DATE, day DATE, day_revenue BIGINT, day_orders BIGINT);
SELECT CAST(COUNT(*) AS INTEGER) AS Count FROM docs_cache.orders;| Count BIGINT |
|---|
| 7 |
The features of this section live in FM.DOCS.DAILY. They read the same orders table, with no CONST filter: the keysets choose the days.
CREATE OR REPLACE FEATURES IN FM.DOCS.DAILY AS
PERSIST
ORDERS := ENTITY(),
DAYS := ENTITY(),
ORDER_ID := INPUT(BIGINT#ORDERS),
DAY := INPUT(DATE#DAYS),
KS_ORDERS := KEYSET(ORDERS,
'SELECT order_id FROM docs_cache.orders WHERE ds = :ds'
PARTITION BY (ds = ORDER_DS)),
KS_DAYS := KEYSET(DAYS,
'SELECT CAST(:ds AS DATE) AS day'
PARTITION BY (ds = DAY)),
TABLES.ORDERS := EXTERNAL_COLUMNS(
order_id BIGINT#ORDERS BIND TO ORDER_ID,
ds DATE,
customer_id BIGINT,
amount BIGINT
FROM TABLE(docs_cache.orders)
),
ORDER_DS := TABLES.ORDERS[ds],
ORDER_DAY := CAST(ORDER_DS AS DATE#DAYS),
ORDER_AMOUNT := TABLES.ORDERS[amount],
DAY_REVENUE := DAY.RELATED(SUM(ORDER_AMOUNT) GROUP BY ORDER_DAY),
DAY_ORDERS := DAY.RELATED(COUNT() GROUP BY ORDER_DAY),
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.DOCS.DAILY.ORDERS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.DAYS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.ORDER_ID | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.DAY | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.KS_ORDERS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.KS_DAYS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.TABLES.ORDERS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.ORDER_DS | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.ORDER_DAY | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.ORDER_AMOUNT | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.DAY_REVENUE | CREATED | Feature created as not exists |
| FM.DOCS.DAILY.DAY_ORDERS | CREATED | Feature created as not exists |
PARTITION BY (ds = ORDER_DS) says two things about KS_ORDERS, for any set of days bound to ds:
- The set is the union of its days. The orders of 07, 08 and 09 are the orders of 07, plus those of 08, plus those of 09. Each order belongs to exactly one day.
- Each key knows its day. An order of partition
dshasORDER_DS = ds.
The right-hand side is an attribute of the key the keyset binds:
| Right-hand side | Example | Keyset |
|---|---|---|
| A column of the source bound to the key | ds = ORDER_DS | KS_ORDERS: one row per order |
| The key itself | ds = DAY | KS_DAYS: one row per day |
A field of a ROW key | ds = COUNTRY_DAY[day] | one row per (country, day) pair |
A value computed from the key, such as CAST(ORDER_TS AS DATE), is refused with KEYSET-PARTITION-NOT-A-KEY-ATTRIBUTE. FeatureMesh trusts the declaration and does not check it against the data. A false declaration gives a wrong answer, like a false BIND TO or a false cache row.
The keyset SQL still describes one day, with WHERE ds = :ds, as in step 1 . A keyset written WHERE ds BETWEEN :ds_from AND :ds_to cannot be split into days, so its values are never shared across ranges.
Bind a set of days
A partitioned parameter takes one day or an ARRAY of days. SEQUENCE() writes a range, one day apart by default:
CONST PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
RETURN
day,
day_revenue,
day_orders
FROM FM.DOCS.DAILY
FOR
day := @BIND_KEYSET(ks_days, ROW(period AS ds)),
NESTED order_id := @BIND_KEYSET(ks_orders, ROW(period AS ds))
ORDER BY day
;| "FM.DOCS.DAILY.DAY" DATE#FM.DOCS.DAILY.DAYS | "FM.DOCS.DAILY.DAY_REVENUE" BIGINT | "FM.DOCS.DAILY.DAY_ORDERS" BIGINT |
|---|---|---|
| 2026-10-07T00:00:00 | 70 | 4 |
| 2026-10-08T00:00:00 | 50 | 2 |
| 2026-10-09T00:00:00 | 12 | 1 |
The binding is a set:
- one day and a one-element array are the same binding;
- repeated days count once, and their order does not matter;
- gaps are allowed:
ARRAY(DATE '2026-10-07', DATE '2026-10-09')binds two days; - an empty array is an empty scope, and a
NULLday is an error (KEYSET-PARTITION-NULL-VALUE).
The days must be known when the query compiles: literals, a CONST, or an expression of them. "The last 7 complete days" is written by the dashboard, in its own timezone:
CONST
YESTERDAY := DATE '2026-10-09',
PERIOD := SEQUENCE(DATE_ADD(YESTERDAY, -6, 'day'), YESTERDAY) FeatureMesh runs the keyset SQL once for the whole set when it can. EXPLAIN shows how, for each partitioned binding:
EXPLAIN
CONST
PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| keyset_resolutions[?keyset_name == 'FM.DOCS.DAILY.KS_DAYS'].partition.lowering | [0] | "VALUES" |
| keyset_resolutions[?keyset_name == 'FM.DOCS.DAILY.KS_ORDERS'].partition.lowering | [0] | "IN" |
lowering | When | Cost |
|---|---|---|
IN | :ds appears only in <column> = :ds conditions of the top-level WHERE | One scan: ds = :ds becomes ds IN (…) |
VALUES | :ds appears only in the SELECT list, as in KS_DAYS | One scan: the days are a list joined to the FROM |
UNION_ALL | Any other keyset SQL | One run of the keyset SQL per day |
A fact keyset that falls back to UNION_ALL scans its table once per day of the range. Rewrite its filter as WHERE ds = :ds.
One datakey for every day
DAY_REVENUE reads the orders of its own day and nothing else. So its value for 07 is the same in a 1-day query and in a 3-day query. FeatureMesh gives it one datakey, whatever the days. The days of the query are listed beside it, in partition_values:
EXPLAIN (FIELDS (DATA_KEYS))
CONST
PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].data_key | [0] | "dk1:c51016dfb51e502f23d0acd9a8b51817f0880f4328c151d245665f1d8ef2e113" |
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].reuse | [0] | "UNION" |
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].footprints | [0] | {"FM.DOCS.DAILY.ORDER_ID": "<footprint {k}>"} |
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].partition_values | [0] | ["DATE '2026-10-07'", "DATE '2026-10-08'", "DATE '2026-10-09'"] |
The daily build of 2026-10-07 has the same datakey:
EXPLAIN (FIELDS (DATA_KEYS))
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := DATE '2026-10-07')),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := DATE '2026-10-07'))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].data_key | [0] | "dk1:c51016dfb51e502f23d0acd9a8b51817f0880f4328c151d245665f1d8ef2e113" |
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].partition_values | [0] | ["DATE '2026-10-07'"] |
reuse says whether the value is shared across days:
UNION: the key carries its day, and the value reads a window of days fixed around it. One row per day serves any range.NONE: the datakey covers exactly the days of the query, as in the tutorial.reuse_reasonsays why.
footprints gives the window each nested population reads, relative to the key's day k. <footprint {k}> is the key's own day.
Build and register each day in one statement
CREATE CACHES … AS (query) runs the build and registers its rows. Give FILTER_COLUMN and no FILTER_VALUE: each row gets the day it holds.
CREATE CACHES
(FEATURE = DAY_REVENUE, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_revenue', FILTER_COLUMN = 'ds'),
(FEATURE = DAY_ORDERS, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_orders', FILTER_COLUMN = 'ds')
AS (
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := DATE '2026-10-07')),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := DATE '2026-10-07'))
);| Count BIGINT |
|---|
| 1 |
The statement:
- deletes the partition
ds = DATE '2026-10-07'from the cache table, then inserts the build, so a rerun does not duplicate keys; - computes from the source tables, as if the query carried
/* NO-CACHE */; - writes the day into the
dscolumn; - registers one row per feature, with
FILTER_VALUE = DATE '2026-10-07'; - returns the number of rows written in that partition.
The rows are registered when the statement compiles, before the build runs. If the build fails, drop them, or run the statement again.
The next day, the orchestrator runs the same statement for 2026-10-08:
CREATE CACHES
(FEATURE = DAY_REVENUE, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_revenue', FILTER_COLUMN = 'ds'),
(FEATURE = DAY_ORDERS, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_orders', FILTER_COLUMN = 'ds')
AS (
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := DATE '2026-10-08')),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := DATE '2026-10-08'))
);| Count BIGINT |
|---|
| 1 |
Read a range, and find the missing days
A query over 07 to 09 now needs 09, which nobody built. A feature is read from caches only when every day it needs has a row. Otherwise it is computed from the source tables for the whole range. The response lists what is missing:
client translate
{"query": "CONST PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09') RETURN DAY, DAY_REVENUE, DAY_ORDERS FROM FM.DOCS.DAILY FOR DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)), NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD));"}| FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| full_response.data.cache_reads | [] |
| full_response.data.cache_missing_partitions[*].name | ["FM.DOCS.DAILY.DAY_ORDERS", "FM.DOCS.DAILY.DAY_REVENUE"] |
| full_response.data.cache_missing_partitions[0].table_name | "docs_cache.cache_per_day" |
| full_response.data.cache_missing_partitions[0].registered | ["DATE '2026-10-07'", "DATE '2026-10-08'"] |
| full_response.data.cache_missing_partitions[0].missing | ["DATE '2026-10-09'"] |
Each entry of cache_missing_partitions has:
| Field | Meaning |
|---|---|
name, data_key | The feature, and the datakey to register for each missing day |
table_name | The table that already holds the other days |
registered, missing | The days that have a row, and those that do not |
work_units_if_complete | What the query would save once the missing days are registered |
An entry appears only when registering the missing days would change the plan. Backfill those days with the same statement as the daily build:
CREATE CACHES
(FEATURE = DAY_REVENUE, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_revenue', FILTER_COLUMN = 'ds'),
(FEATURE = DAY_ORDERS, BACKEND = 'duckdb', TABLE_NAME = 'docs_cache.cache_per_day',
KEY_COLUMN = 'day', VALUE_COLUMN = 'day_orders', FILTER_COLUMN = 'ds')
AS (
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := DATE '2026-10-09')),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := DATE '2026-10-09'))
);| Count BIGINT |
|---|
| 1 |
The range is now read from the cache table, one row per day and per feature, in one join. The orders are not read:
client translate
{"query": "CONST PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09') RETURN DAY, DAY_REVENUE, DAY_ORDERS FROM FM.DOCS.DAILY FOR DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)), NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD));"}| FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| length(full_response.data.cache_reads) | 6 |
| contains(join(' ', full_response.data.steps[].sql), 'docs_cache.orders') | false |
| full_response.data.cache_missing_partitions | [] |
CONST PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
RETURN
day,
day_revenue,
day_orders
FROM FM.DOCS.DAILY
FOR
day := @BIND_KEYSET(ks_days, ROW(period AS ds)),
NESTED order_id := @BIND_KEYSET(ks_orders, ROW(period AS ds))
ORDER BY day
;| "FM.DOCS.DAILY.DAY" DATE#FM.DOCS.DAILY.DAYS | "FM.DOCS.DAILY.DAY_REVENUE" BIGINT | "FM.DOCS.DAILY.DAY_ORDERS" BIGINT |
|---|---|---|
| 2026-10-07T00:00:00 | 70 | 4 |
| 2026-10-08T00:00:00 | 50 | 2 |
| 2026-10-09T00:00:00 | 12 | 1 |
Any subset of the registered days reads the same rows. Here 07 and 09, with a gap:
EXPLAIN (FIELDS (CACHE_PLAN))
CONST
PERIOD := ARRAY[DATE '2026-10-09', DATE '2026-10-07']
RETURN
DAY,
DAY_REVENUE,
DAY_ORDERS
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| cache_plan.joins_added | 1 |
| cache_plan.groups[0].table_name | "docs_cache.cache_per_day" |
| cache_plan.groups[0].filter_values | ["DATE '2026-10-07'", "DATE '2026-10-09'"] |
| cache_plan.candidates[*].decision | ["USED", "USED", "USED", "USED"] |
Registering a missing day invalidates the cached translations of the ranges that contain it, and only those.
Windows that read the days before
A trailing sum reads the day before its key. Its footprint is [k-1, k+0]: another datakey than DAY_REVENUE, and another one again for a 3-day window. In a query over 07 to 09, the window of 07 needs 06, which the query does not bind. Its value for 07 is truncated, so no row can stand in for it:
EXPLAIN (FIELDS (DATA_KEYS))
CONST
PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
DEFINE
DAY_REVENUE_2D := RELATED(SUM(ORDER_AMOUNT)
ON ORDER_DS BETWEEN CAST(KEY_TO_SCALAR(DAY) - INTERVAL '1 day' AS DATE) AND KEY_TO_SCALAR(DAY))
RETURN
DAY,
DAY_REVENUE_2D
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| data_keys[?name == 'DAY_REVENUE_2D'].reuse | [0] | "UNION" |
| data_keys[?name == 'DAY_REVENUE_2D'].footprints | [0] | {"FM.DOCS.DAILY.ORDER_ID": "<footprint [k-1, k+0]>"} |
| data_keys[?name == 'DAY_REVENUE_2D'].partition_values | [0] | ["DATE '2026-10-08'", "DATE '2026-10-09'"] |
| data_keys[?name == 'DAY_REVENUE_2D'].truncated_partitions | [0] | ["DATE '2026-10-07'"] |
truncated_partitions lists those days. The cache plan marks the feature TRUNCATED-WINDOW and computes it.
So the daily build binds the window. To build 09, bind the key to 09 and the orders to 08 and 09. The build then has a full window for 09, and registers that day only:
EXPLAIN (FIELDS (DATA_KEYS))
DEFINE
DAY_REVENUE_2D := RELATED(SUM(ORDER_AMOUNT)
ON ORDER_DS BETWEEN CAST(KEY_TO_SCALAR(DAY) - INTERVAL '1 day' AS DATE) AND KEY_TO_SCALAR(DAY))
RETURN
DAY,
DAY_REVENUE_2D
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := DATE '2026-10-09')),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := SEQUENCE(DATE '2026-10-08', DATE '2026-10-09')))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| data_keys[?name == 'DAY_REVENUE_2D'].partition_values | [0] | ["DATE '2026-10-09'"] |
| data_keys[?name == 'DAY_REVENUE_2D'].truncated_partitions | [0] | [] |
The dashboard binds the lookback the same way: a chart of 08 and 09 reads the rows of 08 and 09 when its orders cover 07 to 09.
FeatureMesh recognises two kinds of window: an aggregate grouped by the key's day ({k}), and RELATED(… ON P BETWEEN k - n days AND k + m days). A window that grows with the key, such as a running total since the first order, keeps an exact datakey (UNRECOGNISED-FOOTPRINT).
What a range cannot reuse
Some values read the other days of the query. Their value for a day changes with the range, so they keep an exact datakey (reuse is NONE):
EXPLAIN (FIELDS (DATA_KEYS))
CONST
PERIOD := SEQUENCE(DATE '2026-10-07', DATE '2026-10-09')
DEFINE
SHARE := DAY_REVENUE * 100 / SUM(DAY_REVENUE) OVER (),
PREVIOUS_DAY_REVENUE := LAG(DAY_REVENUE) OVER (ORDER BY DAY)
RETURN
DAY,
DAY_REVENUE,
SHARE,
PREVIOUS_DAY_REVENUE
FROM FM.DOCS.DAILY
FOR
DAY := @BIND_KEYSET(KS_DAYS, ROW(ds := PERIOD)),
NESTED ORDER_ID := @BIND_KEYSET(KS_ORDERS, ROW(ds := PERIOD))
;| OUTPUT FIELD VARCHAR | VALUE VARCHAR |
|---|---|
| data_keys[?name == 'FM.DOCS.DAILY.DAY_REVENUE'].reuse | [0] | "UNION" |
| data_keys[?name == 'SHARE'].reuse_reason | [0] | "OWN-POPULATION" |
| data_keys[?name == 'PREVIOUS_DAY_REVENUE'].reuse_reason | [0] | "OWN-POPULATION" |
DAY_REVENUE under them is still read per day. The window is then computed on top, over the few output rows. Some period results can be reconstructed from daily values. Use the last column to decide whether to combine daily rows or cache the exact range:
| Shape | reuse_reason | Rewrite |
|---|---|---|
A window over the bound keys: a share of the total, LAG, a rank | OWN-POPULATION | Cache the value under the window, compute the window in the query |
A key without a day (COUNTRY_REVENUE over a range) | ROLLUP-V2 | Key the value by ROW(country, day), sum it in the query |
AVG over a period | ROLLUP-V2 on a key without a day | Cache SUM and COUNT per day, divide in the query |
COUNT(DISTINCT), percentiles, ARRAY_AGG over a period | ROLLUP-V2 | None: a value seen on two days counts twice. Cache that exact set of days |
| A window that depends on the key (month to date) | UNRECOGNISED-FOOTPRINT | A running SUM over the cached daily rows, binding the days from the month start |
| A week or a month bucket | — | GROUP BY week over the cached daily rows |
A key without a day, such as COUNTRY in the tutorial, has no partition of its own. To share its values across ranges, key them by a ROW that carries the day, partitioned on its day field:
COUNTRY_DAY := INPUT(ROW(country VARCHAR#COUNTRIES, day DATE#DAYS)),
KS_COUNTRY_DAYS := KEYSET(COUNTRIES,
'SELECT DISTINCT country, CAST(:ds AS DATE) AS day FROM countries'
PARTITION BY (ds = COUNTRY_DAY[day])),
ORDER_COUNTRY_DAY := ROW(ORDER_COUNTRY AS country, ORDER_DAY AS day),
COUNTRY_DAY_REVENUE := COUNTRY_DAY.RELATED(SUM(ORDER_AMOUNT) GROUP BY ORDER_COUNTRY_DAY), A period total per country is then SUM(COUNTRY_DAY_REVENUE) GROUP BY COUNTRY_DAY[country]. Period over period is a FILTER on COUNTRY_DAY[day].
Keep daily rows fresh
A daily row is still the declaration of the contract. Ranges make staleness more visible: a 90-day chart reads 90 rows built on 90 days.
- Late facts. An order that lands in a closed day makes that day stale, and every window that contains it: for a footprint
[k-1, k+0], the day itself and the day after. Run the build statement again for each of them. It rewrites the same slice and registers the same ids. - Dimensions. A daily row freezes what the value read on its build day. A value that reads the latest state of a dimension, such as the current country of a customer, gives another answer after the dimension changes. Read the dimension as of the partition date, from a snapshot keyed by
ROW(customer_id, ds). Its rows never change once written, and neither do the daily rows built from it. - Two rows for one day. A row built into another table for a day already registered leaves two rows for that day. The planner reads the newest one (
CREATED_AT, then id). Drop the old row. - Only closed days. Register a day once its partition is complete. A query over a day that is still filling computes from the source tables until the day is built.
Next
The daily job and the backfill as Python: Rebuild scripts .