Skip to content

Manage the clock, end to end

This page follows one data domain's clock from creation to removal. It is the map: each step says which call to use and links to the page that holds the detail, so nothing is explained twice.

Topic Where the detail is
Which valid time a run uses, which call computes what Managing time with the domain clock
Calendar rules, due time, refresh_domain, gaps Calendar rules and scheduled refresh
Listing, aligning and tidying managers Keep the TimeManagers under control
The manager object (cursor, cloning, roll_out) TimeManager & roll_out

1. What the pieces are

  calendar rule (optional)      Calendar.monthly(day='last', due='+3d 06:00')
        |  generates
        v
  table of dates (HIDDEN)       one row per business date: TIME_ID 1, 2, 3 ...
        |  read through
        v
  TimeManager (the view)        exposes ONE row: the cursor
        |  declared as
        v
  domain clock                  set_domain_time_manager(clock)      one per data domain
        |  one soft clone per process (same dates, own cursor)
        v
  process views                 read their clone; run() / roll_out() move it
  • A TimeManager is a table of business dates plus a view showing the current one (the cursor). A process view filters its source on that view, so the values it writes carry that business date.
  • The domain clock is the one TimeManager a data domain declares. Its processes share one valid time.
  • A soft clone reads the clock's table of dates and has a cursor of its own. upload_features makes one per process; roll_out(data_domain=...) moves the clock and every clone together.

2. Create the calendar

Either let a rule produce the dates, or load them from data.

import tdfs4ds
from tdfs4ds import Calendar
from tdfs4ds.utils.time_management import TimeManager

# A rule: the dates are generated, and extended later by the same rule.
rule = Calendar.monthly(day='last', due='+3d 06:00')      # business date: month end; due: 3 days later, 06:00
clock = TimeManager('SALES_CLOCK', 'MY_STORE', calendar=rule, start='2024-01-31', until='now')

# Or from the dates of a source (no rule stored; one is inferred later when the dates are regular).
# clock = TimeManager('SALES_CLOCK', 'MY_STORE')
# clock.load_time_steps(dates_df, 'BUSINESS_DATE')        # dates_df: a teradataml DataFrame

tdfs4ds.set_domain_time_manager(clock, data_domain='SALES')

The business date is the valid time of the values. The due time only says when a step may be added (the data has landed); it never shifts the business date. Forms and examples: Calendar rules.

3. Attach a process

With a clock declared, upload_features needs no time_manager=. It reads the lineage of the source view, finds the clock, gives the process its own soft clone <view>_TM, and rewrites the view to read it. Write the process SQL against the clock:

WHERE A.EVENT_DATE <= (SELECT BUSINESS_DATE FROM MY_STORE.SALES_CLOCK)

Register with run_now=False when the history is to be built by roll_out, so no value exists before the first calendar date (a history is built forward only).

Registration Result
view reads the clock, no time_manager= own soft clone, view rewritten
time_manager=clock given paired as before 0.3.6.0, no clone (soft_clone=True asks for one)
view reads a shared filtered view processes share the cursor: clone the shared view
reference process (rules, mappings) domain_roll_out=False: the domain's calls leave it out

Processes registered before the clock existed are brought onto it with align_domain_time_managers (details).

4. Which call to schedule

Job Call Moves the clock? Writes valid time
Build the history, once roll_out(data_domain='SALES') yes, date by date each calendar date
Scheduled refresh refresh_domain('SALES', until='now') extends the table of dates by the rule, then rolls out the new dates each new business date
Same, from run run(data_domain='SALES', advance_clock=True) as above as above
Recompute everything at one instant run(data_domain='SALES') no at=, else FEATURE_STORE_TIME, else database time
One process, a past date run(pid, at='2024-03-31', force_compute=True) no at
Replay a range for chosen processes roll_out([pid_1, pid_2], clock, time_id_start=k) yes each date

The call to schedule is refresh_domain(domain, until='now'). It extends the clock only when the rule says a step is due; when the clock is up to date it returns status='up to date' and runs nothing, so scheduling it more often than the rule is harmless. In this mode the safe defaults apply for the call: on_failure='raise', check_upstream=True, TIME_MANAGER_CHECK='error'.

res = tdfs4ds.refresh_domain('SALES', until='now')
res['status'], res['missing'], res['next_due']
res['statuses']          # {business_date: {process_id: status}}

Do not schedule run(pid) alone: after a roll-out it recomputes the last historical date, finds it done and skips it.

5. Extend the clock to now: small gap, large gap

If the scheduler was down, several dates are missing. refresh_domain decides by the number of missing dates:

Missing dates What happens
none status='up to date', nothing runs
up to max_catch_up (default 3) all are added and rolled out in order
more nothing is written or run; CatchUpRefused carries .result with missing and estimate_seconds

Then choose with a number:

tdfs4ds.refresh_domain('SALES', until='now', catch_up='all')       # every missing date, in order
tdfs4ds.refresh_domain('SALES', until='now', catch_up='latest')    # only the last; the skipped dates are recorded as gaps
tdfs4ds.refresh_domain('SALES', until='now', dry_run=True)         # the plan, nothing written

Dates skipped by catch_up='latest' are stored in the domain setting CALENDAR_GAPS, read by domain_clock_status, reported by the sanity check as calendar_gap, and carried by a migration. Compute them later from the first gap with roll_out(data_domain='SALES', time_id_start=<TIME_ID of the first gap>, force_compute=True).

6. Look before you run

st = tdfs4ds.domain_clock_status('SALES')     # read only, never raises
st['rule'], st['inferred'], st['cursor_date'], st['missing_until_now'], st['next_due'], st['gaps'], st['behind']
st['processes']                                # per process: the last business date it completed

The sanity check reads the same state: clock_behind (more than one step behind now) and calendar_gap; its clock check also inspects the calendar of every TimeManager (calendar_rule_inferable, calendar_irregular, calendar_off_rule_dates) and its windows check the windows written by hand. The Time managers report lists every manager of a domain, its role and its findings: tdfs4ds.write_time_manager_report('time_managers.html') (report).

7. Rebuild a history

A run at business date t writes from t onward and never before the first stored value, so a history is built forward only. To recompute after a logic change, replay from the first date:

tdfs4ds.roll_out(data_domain='SALES', time_id_start=1, force_compute=True)

Without force_compute=True, dates already COMPLETED are skipped and nothing changes. A process first run at a late date cannot be rolled out over earlier dates: roll_out refuses with a ValueError naming it; pass rebuild_history=True (deletes its stored values from the first date rolled out, logged in FS_REMOVAL_LOG) or start at the first stored date. Check the result with tdfs4ds.completed_dates(pids) and a cheap sanity check (checks order, segments).

7a. Recompute only what changed

force_compute=True recomputes every date. After an edit of one process, most of that work is not needed. force_compute='changed' recomputes only the (process, business date) pairs that were computed with SQL that has changed since:

tdfs4ds.changed_processes(data_domain='SALES')   # read only: what would be recomputed, and why
tdfs4ds.roll_out(data_domain='SALES', time_id_start=1, force_compute='changed')
tdfs4ds.run(['<process id>'], force_compute='changed')

changed_processes returns one row per process with REASON:

Reason Meaning
sql_changed The SQL beneath the process view differs from the SQL its runs used (DETAIL shows the versions, e.g. v2 -> v3)
upstream_changed An upstream process of the same data domain changed; recomputed from its first stale date
never_run No successful run recorded
not_decided Cannot be judged from the stored history: treated as unchanged (see below)

What counts as changed. A process version is the SQL of the process view and of the views it reads. Managers are set aside: their SQL changes by design at each step. A date is stale when its latest successful run at that date (per FilterManager segment) was made under another version than the current one.

From the first stale date onward. A run at date d rewrites every later period of the values it writes, so a process stale from d is recomputed at every completed date from d, in date order. Every process downstream of it in the same data domain (lineage graph) is recomputed from d too, for the dates it has completed runs at. Dates before the first stale one are not touched. A stale date before time_id_start is not replayed (a warning names it).

The limits. 'changed' sees only the SQL. These are not seen, and need force_compute=True:

  • corrected source data, a changed function, a changed model or table operator;
  • changed manager contents (a calendar rule, filter values);
  • processes with PROCESS_SQL_VERSIONING off;
  • processes that read their valid time from a column (valid_time_column);
  • dates computed before SQL versioning existed for the process.

The last two are reported as not_decided and left unchanged.

Other rules.

  • A real-time run (no valid time) is never skipped, so it is always computed.
  • max_workers is ignored with 'changed': the forced units run one after the other.
  • True and False keep their meaning. 'changed' is not a synonym of True.
  • refresh_domain(...) and run(..., advance_clock=True) run only the new business dates. Pass force_compute='changed' in their keyword arguments to recompute changed history as well.

7b. Windows that follow the calendar

A process that needs a period (the sales of the step, a count since the last run) should not hard-code its length with ADD_MONTHS(BUSINESS_DATE, -1): the day the clock goes from monthly to weekly, the window would still span a month. Write it against the clock and its previous view instead:

clock = tdfs4ds.get_domain_time_manager('SALES')            # the TimeManager of the clock
clock.enable_previous()                                      # creates <view>_PREVIOUS (opt-in, no table, no date changes)
WHERE A.EVENT_DATE >  (SELECT BUSINESS_DATE FROM MY_STORE.SALES_CLOCK_PREVIOUS)
  AND A.EVENT_DATE <= (SELECT BUSINESS_DATE FROM MY_STORE.SALES_CLOCK)
  • The window is (previous, current]: the previous date is excluded and the current one included, so two consecutive steps never overlap and never leave a gap, whatever the frequency of the calendar.
  • <view>_PREVIOUS is a view over the clock's own table of dates (the largest date below the cursor), so it follows update, roll_out and run(at=...) by itself. Nothing to maintain.
  • First step: there is no date before the first one. enable_previous(first_previous=None) takes it from the calendar rule (Calendar.previous_before); pass a date, or 'unbounded' to read everything up to the first date. enable_previous(dry_run=True) shows the view; disable_previous() drops it.
  • Each soft clone gets its own <clone>_PREVIOUS, and the rewrite of a process view to its clone covers both references. A manager created with TimeManager(..., previous=True) has it from the start.
  • Pruning: with DYNAMIC EXPLAIN (DynamicPlan=SYSTEMX) the plan reads only the partitions of the window (measured); a static EXPLAIN shows an all-rows read because the dates are parameters, exactly as with a single scalar today.
  • A process on the previous view, or a cumulative one (d <= clock), needs no SQL change when the frequency changes.

7c. Change the frequency (monthly to weekly)

The calendar is converted in place: same table and view names, so no process SQL is touched. In this order:

  1. Find the windows written by hand. tdfs4ds.sanity_check('windows.html') (info hand_written_window, warning window_mismatch_calendar, info fixed_window_bound) names each process and the replacement to read.
  2. Switch those processes once to the previous view (register the view again with upload_features: same process id, history kept), after clock.enable_previous(). A monthly window rewritten as (previous, current] gives the same values today.
  3. Plan the change. Nothing is written:
plan = tdfs4ds.change_domain_calendar('SALES', tdfs4ds.Calendar.weekly(weekday='FRI'), dry_run=True)
plan['review']      # processes with a hand-written window that assumes a frequency
plan['agnostic']    # processes on the previous view or cumulative: nothing to do
plan['refused']     # why it cannot run now: a literal view left by an interrupted run(at=), RUNNING runs,
                    # a hard copy of the calendar elsewhere

The report also gives the old and new number of dates, the position of every cursor view, a time estimate and the replay call. Read review and refused before going on. 4. Apply. tdfs4ds.change_domain_calendar('SALES', tdfs4ds.Calendar.weekly(weekday='FRI'), dry_run=False) (force=True only after reading refused). Every cursor view goes to the step at or before its date. If the call was interrupted, call it again with the same calendar: the pending record CALENDAR_CHANGE is resumed. 5. Replay the history, forward only (step 7). The change never runs it:

tdfs4ds.roll_out(data_domain='SALES', time_id_start=1, force_compute=True)
  1. Keep going weekly. tdfs4ds.refresh_domain('SALES', until='now') now adds the weekly dates.

TimeManager.change_calendar(calendar, until='now', dry_run=True, force=False) does the same for one manager. After a half-finished change the cheap check clock reports calendar_off_rule_dates.

8. Datasets follow the clock

A dataset built by upload_features(..., dataset_view_name=) on a process that reads a soft clone follows the domain clock (dataset_time_manager=False for a CURRENT VALIDTIME dataset). After a roll-out, moving the clock returns the values of that date with no re-run, and a date-less run(data_domain=...) sets the datasets' managers too. tdfs4ds.dataset_time_managers() lists what each dataset follows. See A dataset that follows the clock.

9. Keep the managers tidy

tdfs4ds.align_domain_time_managers('SALES', cursors=True, calendars=True, dry_run=True)   # the plan
tdfs4ds.write_time_manager_report('time_managers.html')

align_domain_time_managers gives processes registered before the clock a clone, re-points copies with the clock's dates, and never moves a process whose manager holds dates the clock does not have. Full table: Keep the TimeManagers under control.

10. Migrate a clock

plan_migration / migrate_objects carry the clock with the domain: the clones and their table of dates come with the processes, the clock view is recreated at its cursor over the placed table and declared for the target domain, and the calendar rule and recorded gaps are copied. After the migration, rebuild the production history (step 7) and schedule step 4. See From prototype to production.

11. Remove a clock

print(tdfs4ds.get_domain_time_manager('SALES'))                # note it first
tdfs4ds.detach_domain_time_manager('SALES', dry_run=True)
tdfs4ds.detach_domain_time_manager('SALES')

This removes the setting only. Processes keep their clones. The clock's own view and table of dates stay: drop them once nothing reads them. The soft clones of removed processes are dropped by plan_removal(..., drop_views=True) as drop_time_manager steps. Whole-domain recipe: Remove a data domain.

Common mistakes

See Common mistakes. The ones met most: dates in the calendar that are not yet current, FEATURE_STORE_TIME left set in the session, a manager the process view does not read, and scheduling run(pid) instead of refresh_domain.