Skip to content

tdfs4ds.utils.calendar_change

tdfs4ds.utils.calendar_change

Change the calendar of a TimeManager in place (0.3.7.31).

TimeManager.change_calendar(calendar) replaces the dates of a table of dates by those of another rule (monthly -> weekly) WITHOUT touching a process view, keeping the table, its name and its type. The planning is pure (:func:plan_steps, :func:map_views, :func:data_request); the reads are small fail-open functions of this module.

Write order (Teradata refuses DDL in a multi-statement request that holds DML, Error 3576; a DELETE plus several INSERT in one request is one transaction):

  1. the PENDING record (setting CALENDAR_CHANGE of the table: new rule, count, first and last date, and for each view its old date and new TIME_ID) is stored;
  2. ONE data request: DELETE FROM <table>; INSERT ... VALUES (1, d1); ... (all or nothing);
  3. each cursor view is repointed in its own statement (REPLACE VIEW ... WHERE TIME_ID = n);
  4. the new rule is stored (setting CALENDAR) and the pending record is deleted.

An interruption is recovered by calling change_calendar again with the same calendar: if the pending record is there and the table already holds the dates it announced, the data request is skipped and the views are repointed from the RECORDED old dates (they cannot be read from the new table); otherwise everything is recomputed from the stored dates. Until the end the stored rule is the old one, so calendar_off_rule_dates of the sanity check flags a half-finished change.

CHANGE_PENDING module-attribute

CHANGE_PENDING = 'CALENDAR_CHANGE'

qualified

qualified(database, object_name, force=False)

Build db.obj with each part quoted only as needed.

qualified('TDB', 'FS_X') == 'TDB.FS_X' (unchanged for ordinary names); qualified('SVC@CORP', 'FS_X') == '"SVC@CORP".FS_X'. An empty/None database yields the bare object name, so unqualified callers keep working.

force=True quotes both parts unconditionally — only for the sites that have always emitted quotes and whose exact text is already persisted (dataset view DDL, returned volatile-table names).

sql_literal

sql_literal(value)

Escape value for use inside a single-quoted SQL string literal.

For the DBC probes and catalog predicates that compare a name as data (WHERE DatabaseName = '…'). The caller supplies the surrounding quotes.

_norm

_norm(value, is_date: bool)

A comparable key: wall date for DATE, UTC naive instant for zoned values, naive as written.

plan_steps

plan_steps(calendar, first_date, now, until='now') -> List[pd.Timestamp]

The dates of calendar from first_date to until ('now' = the database clock now), limited to the steps already due. Pure; tz-aware in the rule's zone.

check_type

check_type(type_text: str, steps, offset=None) -> List[str]

Literals of steps in the table's type, or ValueError (a time of day on DATE, a type that cannot hold the dates). Pure.

data_request

data_request(table_sql: str, literals: List[str]) -> str

The ONE data request: DELETE then one INSERT ... VALUES per step, TIME_ID 1..N.

map_views

map_views(new_steps, type_text: str, old_rows, views: List[dict]) -> List[dict]

For each cursor view {view, database, time_id, literal}: its old date (the date of its TIME_ID in old_rows), the new TIME_ID (the largest new step whose date is <= the old date, else 1) and its new date. Pure.

pending_record

pending_record(calendar, rows_first, rows_last, count, views) -> str

data_already_written

data_already_written(pending: Optional[dict], calendar, first, last, count) -> bool

Whether a pending record says the data request of THIS change already ran: same rule and the table holds the announced first / last date and count. Pure.

_execute

_execute(sql)

cursor_views

cursor_views(tm, managers: List[Tuple[str, str]]) -> Tuple[List[dict], List[dict], List[str]]

(cursor views, literal views, notes) among the views reading the table of tm: the managers (managers, the registry's) plus the lineage predecessors of the table. A previous-date view is computed and skipped; a view holding a literal date (an interrupted run(at=)) is returned apart; another reader without cursor is noted.

processes_on

processes_on(managers: List[Tuple[str, str]]) -> List[dict]

[{process_id, view, time_view}] of the processes whose recorded TimeManager is one of managers. Fail-open ([]).

running_processes

running_processes(pids: List[str]) -> Optional[List[str]]

The processes of pids with a RUNNING follow-up row; None when unreadable.

hard_copies

hard_copies(tm) -> List[str]

Time managers elsewhere holding the same dates in ANOTHER table (they would not follow). Fail-open.

read_pending

read_pending(tm) -> Optional[dict]

process_windows

process_windows(processes: List[dict], managers: List[str], rule_step=None, rule_text=None) -> Tuple[List[dict], List[str]]

Classify the window of each process (window_shape.classify_chain over its live SQL chain). Returns (rows for window_findings, unverifiable notes). Fail-open per process.

change_calendar

change_calendar(tm, calendar, *, until='now', dry_run: bool = True, force: bool = False, gap_domains=()) -> Dict[str, Any]

See :meth:TimeManager.change_calendar. Returns {old_rule, new_rule, old_count, new_count, first_date, last_date, views, gaps_dropped, review, agnostic, refused, estimate_seconds, replay, statements, written, notes}; raises ValueError for a calendar the table cannot hold, and (when not a dry run) when a guard refuses and force is False. The replay is never run.

collect_calendar_states

collect_calendar_states(want_windows: bool = True, want_calendars: bool = True)

(states, window_rows, notes).

states: one dict per TABLE of dates of the store (managers of the registry, the process catalog and the domain clocks, soft clones grouped by table, so each table is read once): {view, database, table, views, state: calendar_state(...)}. window_rows: one per process with a recorded TimeManager, ready for :func:tdfs4ds.lineage.window_shape.window_findings (the process's window classified against the step of its manager's rule, stored or inferred). notes: what could not be read (unverifiable).