Design a workflow¶
Decisions to take before you register the first process. Each one is costly to change afterwards: moving a storage table or an entity key means a relocation or a replay.
1. Data domains and entities¶
A data domain is the project namespace. It isolates features, and it owns one
clock. An entity is the grain of a set of features; its key is the list
of columns in entity_id.
- Pick the grain from the business object (customer, product, customer by month), not from the columns the source view happens to have.
- Keep a column in the key only if the other key columns do not determine it. Store a column that does not belong in the key as a feature. The entity scan of the sanity check finds it in the data.
- A NULL key refuses the batch. Replace it at the source, or declare
entity_null_substitute={'COL': 'UNKNOWN'}at registration. - The same feature name under two entities is two features. Name features for what they mean.
2. Processes and storage tables¶
One process is one view and one upload_features call. Cut processes by entity and
theme: transactions, profile and risk are three themes of the same customer entity.
Storage tables are keyed by (data domain, entity, value type):
| Fact | Consequence |
|---|---|
Features of one entity and one value type share an FS_T_* table |
Their processes write the same table |
A MERGE locks its target table |
Two processes on the same table run one after the other, never in parallel |
| Different entities, or different value types, are different tables | Those processes can run in parallel |
storage_tag='X' gives a process a table of its own (__X) |
Use it to parallelise two processes of one entity. For existing features: relocate |
The scheduler reads this: analyze_parallel_plan reports serialize_together for processes
of a wave that share a table. When the features are not registered yet, it uses
(data domain, entity) as a conservative key.
3. Layers¶
source tables
-> intermediate views (joins, cleaning; shared by several processes)
-> process views (one per process: the feature logic)
-> FS_T_* storage (written by run / roll_out only)
-> datasets (views joining features of one or more entities)
-> consumers (models, reports, the agent)
- Put shared logic in an intermediate view. Lineage follows it, and the documentation pipeline documents it once.
- A process view returns one row per entity key (and per valid time with
valid_time_column). - Consumers read datasets. A dataset follows the stored features, so a relocation or a new process version does not break it.
- A view of yours that joins a dataset with other tables can be declared part of the store with
attach_objects. Lineage, documentation and removal guards then cover it.
4. The time driver¶
| Driver | Use when | Valid time of the stored values |
|---|---|---|
Domain clock (set_domain_time_manager) |
The default. Features are computed for a business date, and processes must agree on it | The clock's date under roll_out; the database time, read once, under run(data_domain=...) |
valid_time_column |
The dates are in the data: one row per entity and valid time. One run writes the whole history | The column of the source. The manager date only bounds the rows the view returns |
| Real time | Event feeds, with nothing to replay | The database time, read once per run |
Real time is a fallback, not a design. If you can name a business date, use a clock.
5. Segmentation¶
Use a FilterManager when one pass over the source exhausts spool or a window, or when you
want one region or category at a time.
- Segment on the source's partitioning columns, so each segment reads its own partitions.
- With
close_absent_entities, every filter column must be an entity column. - A failed segment is recorded
FAILEDin the follow-up. Chooseon_failurefor the process.
See FilterManager.
6. Options that change what a run does¶
| Option | Switch it on when | Cost |
|---|---|---|
close_absent_entities=True |
An entity that leaves the source must stop appearing in current datasets | One staging table and one sequenced DELETE per storage table. A run that returns no row closes nothing |
track_entity_refresh=True |
You must tell an entity the source dropped from an unchanged one. An unchanged value writes nothing, so a row's start is the last change | One SELECT DISTINCT and one MERGE per batch, into an admin table |
valid_time_column='COL' |
The source holds the history | A different write path; see upload_features |
The options are recorded with the process. set_process_options changes them later.
7. Primary index and partitioning¶
The storage tables default to the entity columns as primary index, with no partitioning.
- Choose
primary_indexso that a lookup by the real key hits one AMP. - Choose
partitioningto match the largest source when it is partitioned on an entity column (findingpartition_not_aligned_with_source). - Set both at the first registration. Changing them later is a relocation with a copy of the history.
8. Plan for the scheduler¶
analyze_parallel_plan turns the lineage into waves. Processes in a wave do not depend on
one another and can run together. Waves run in order.
- A process depends on another only through a view it feeds or a dataset. Lineage shows the edge.
- Two processes of one entity and value type in one wave are serialised. Tag one of them to run both in parallel.
- On a store that predates the persisted graph, rebuild lineage first:
tdfs4ds.rebuild_lineage_graph(all_data_domains=True).
import tdfs4ds
plan = tdfs4ds.analyze_parallel_plan(seeds=['DS_CUSTOMER_360', 'DS_PRODUCT_SALES'])
plan['waves'] # one list per wave
plan['serialize_together'] # processes that must not overlap
path = tdfs4ds.write_execution_script(plan, max_workers=4)
Worked example¶
One source table of sales lines feeds three processes and two datasets.
| Object | Kind | Entity | Reads | Storage |
|---|---|---|---|---|
SALES_LINES |
source table | |||
VW_CUSTOMER_SPEND |
process P1 | CUSTOMER_ID |
SALES_LINES |
the CUSTOMER FLOAT table |
VW_CUSTOMER_RISK |
process P2 | CUSTOMER_ID |
SALES_LINES |
the same table, unless storage_tag='RISK' |
VW_PRODUCT_SALES |
process P3 | PRODUCT_ID |
SALES_LINES |
the PRODUCT FLOAT table |
DS_CUSTOMER_360 |
dataset | CUSTOMER_ID |
features of P1, P2 | none (a view) |
DS_PRODUCT_SALES |
dataset | PRODUCT_ID |
features of P3 | none (a view) |
SALES_LINES --> P1 (CUSTOMER) --+--> DS_CUSTOMER_360
|--> P2 (CUSTOMER) --+
'--> P3 (PRODUCT) -----> DS_PRODUCT_SALES
What runs in parallel:
| Case | Processes | Why |
|---|---|---|
| P1 and P3 | together, wave 0 | Neither reads the other. They write different entities, so different tables |
| P1 and P2, no tag | one after the other | Both write the CUSTOMER FLOAT table, and a MERGE locks it (serialize_together) |
P1 and P2, storage_tag='RISK' on P2 |
together with P3, wave 0 | P2 has a table of its own |
Datasets are views: nothing runs for them. They read what P1 to P3 stored, so they are current as soon as the processes complete. With one clock for the domain, all three processes compute at the same date.
import tdfs4ds
pid_1 = tdfs4ds.upload_features(
'MY_DB.VW_CUSTOMER_SPEND', entity_id=['CUSTOMER_ID'],
feature_names=['spend_30d', 'nb_orders_30d'], return_process_id=True)
pid_2 = tdfs4ds.upload_features(
'MY_DB.VW_CUSTOMER_RISK', entity_id=['CUSTOMER_ID'],
feature_names=['return_rate'], storage_tag='RISK', return_process_id=True)
pid_3 = tdfs4ds.upload_features(
'MY_DB.VW_PRODUCT_SALES', entity_id=['PRODUCT_ID'],
feature_names=['units_sold_30d'], return_process_id=True)