Skip to content

tdfs4ds.lineage.window_shape

tdfs4ds.lineage.window_shape

What window a process view reads from its TimeManager (0.3.7.31). Pure: no database, no LLM.

A process reading a TimeManager usually keeps the rows of the current step: a window whose lower bound is derived from the manager's value, ADD_MONTHS(<date>, -1) for a monthly clock. Such a window silently assumes the frequency of the calendar. This module reads the SQL text of a process view chain and says which of these it is:

  • previous_view: the lower bound is read from <manager>_PREVIOUS (the opt-in previous-date view); it follows a change of calendar;
  • cumulative: only an upper bound from the manager (d <= <date>): not a window;
  • hand_written: the lower bound is arithmetic on the manager value (ADD_MONTHS(<date>, -n), <date> - INTERVAL 'n' DAY|MONTH|YEAR, <date> - n, TRUNC(<date>, 'MM'), MONTH_BEGIN / LAST_DAY / EXTRACT on it, BETWEEN <expr of date> AND <date>), with the step it implies ({'unit': 'months', 'n': 1}) and the snippet;
  • fixed_literal: a literal lower bound (d >= DATE '2020-01-01') beside the manager's upper bound;
  • point_in_time (0.3.7.37): the manager value, shifted or not, pins ONE instant, so there is no window whose length could disagree with the calendar: an equality (snapshot_date = <date> - 1), a VALIDTIME AS OF <date> - 1 / AS OF read, or a validity interval containing the instant (the SAME expression bounds one column from above with <= / < and another from below with > / >=: valid_from <= <date> - 1 AND valid_to > <date> - 1). A real window (a lower bound d >= <date> - n and the upper bound d <= <date>) has that expression on one side only and stays hand_written. An ambiguous shape is left as it was (hand_written);
  • unknown: the manager is read, but no date predicate on it is recognised.

The manager's value is recognised as a scalar sub-query (SELECT BUSINESS_DATE FROM <manager>) (a CAST in the select list is fine), as a column of a manager joined under an alias (JOIN <manager> m ... m.BUSINESS_DATE), and through a CTE / derived-table alias that selects it.

Limits (by design, a text reader): dynamic SQL, UDFs, a window written as ROWS n PRECEDING over source rows, a bound computed in an intermediate view that the chain does not contain, and a manager value reached through a bare column name are not classified (unknown). Arithmetic with a positive shift (an upper bound in the future) is not a lower bound and is ignored.

POINT_IN_TIME module-attribute

POINT_IN_TIME = 'point_in_time'

NOT_READ module-attribute

NOT_READ = 'not_read'

_QN module-attribute

_QN = '(?:"[^"]+"|[A-Za-z_][\\w$#@]*)(?:\\s*\\.\\s*(?:"[^"]+"|[A-Za-z_][\\w$#@]*))?'

_KEYWORDS module-attribute

_KEYWORDS = {'WHERE', 'ON', 'AND', 'OR', 'GROUP', 'ORDER', 'HAVING', 'QUALIFY', 'UNION', 'JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'CROSS', 'SELECT', 'SEL', 'FROM', 'WHEN', 'THEN', 'ELSE', 'END', 'USING', 'AS', 'BETWEEN', 'NOT', 'IN', 'LOCKING', 'SAMPLE', 'WITH', 'LIMIT'}

_SUBQ module-attribute

_SUBQ = re.compile('\\(\\s*SEL(?:ECT)?\\b(?P<sel>(?:[^()]|\\([^()]*\\))*?)\\bFROM\\s+(?P<m>' + _QN + ')\\s*(?:(?:AS\\s+)?\\w+)?\\s*\\)', re.IGNORECASE | re.DOTALL)

_TOK module-attribute

_TOK = '__[MP]\\d+__'

_ARG module-attribute

_ARG = f'(?:CAST\\s*\\(\\s*{_TOK}\\s+AS\\s+[\\w(), ]+?\\)|{_TOK})'

_PAT module-attribute

_PAT = {'add_months': re.compile(f'ADD_MONTHS\\s*\\(\\s*{_ARG}\\s*,\\s*-\\s*(\\d+)\\s*\\)', re.I), 'interval': re.compile(f"{_ARG}\\s*-\\s*INTERVAL\\s*'\\s*(\\d+)\\s*'\\s*(DAY|MONTH|YEAR|HOUR|MINUTE)", re.I), 'minus_n': re.compile(f'{_ARG}\\s*-\\s*(\\d+)(?![\\d.])(?!\\s*[*/])', re.I), 'trunc': re.compile(f"TRUNC\\s*\\(\\s*{_ARG}\\s*(?:,\\s*'(\\w+)')?\\s*\\)", re.I), 'month_begin': re.compile(f'(MONTH_BEGIN|MONTH_END|LAST_DAY|TD_MONTH_BEGIN|TD_YEAR_BEGIN|TD_WEEK_BEGIN)\\s*\\(\\s*{_ARG}', re.I), 'extract': re.compile(f'EXTRACT\\s*\\(\\s*(MONTH|YEAR|WEEK|DAY|QUARTER)\\s+FROM\\s+{_ARG}\\s*\\)', re.I), 'between': re.compile(f'BETWEEN\\s+(?P<lo>[^;]{{1,300}}?)\\s+AND\\s+(?:CAST\\s*\\(\\s*)?(?P<hi>{_TOK})', re.I)}

_LITERAL module-attribute

_LITERAL = re.compile("(?:DATE|TIMESTAMP)\\s*'[^']*'|'\\d{4}-\\d{2}-\\d{2}[^']*'|CAST\\s*\\(\\s*'[^']*'\\s+AS\\s+(?:DATE|TIMESTAMP)", re.I)

_LOWER_LITERAL module-attribute

_LOWER_LITERAL = re.compile('(>=|>)\\s*(?:' + _LITERAL.pattern + ')', re.I)

_UPPER module-attribute

_UPPER = re.compile(f'(?:<=|<)\\s*(?:CAST\\s*\\(\\s*)?{_TOK}|BETWEEN[^;]{{1,300}}?\\s+AND\\s+(?:CAST\\s*\\(\\s*)?{_TOK}', re.I)

_TRUNC_STEP module-attribute

_TRUNC_STEP = {'MM': ('months', 1), 'MON': ('months', 1), 'MONTH': ('months', 1), 'Q': ('months', 3), 'CC': ('months', 12), 'Y': ('months', 12), 'YY': ('months', 12), 'YYYY': ('months', 12), 'YEAR': ('months', 12), 'IW': ('days', 7), 'WW': ('days', 7), 'W': ('days', 7), 'D': ('days', 1), 'DD': ('days', 1), 'DDD': ('days', 1), 'DAY': ('days', 1), 'RM': ('months', 1)}

_EXTRACT_STEP module-attribute

_EXTRACT_STEP = {'MONTH': ('months', 1), 'YEAR': ('months', 12), 'WEEK': ('days', 7), 'DAY': ('days', 1), 'QUARTER': ('months', 3)}

_FUNC_STEP module-attribute

_FUNC_STEP = {'MONTH_BEGIN': ('months', 1), 'MONTH_END': ('months', 1), 'LAST_DAY': ('months', 1), 'TD_MONTH_BEGIN': ('months', 1), 'TD_YEAR_BEGIN': ('months', 12), 'TD_WEEK_BEGIN': ('days', 7)}

_CMP_TAIL module-attribute

_CMP_TAIL = re.compile('(<=|>=|<>|!=|<|>|=)$')

_AS_OF_TAIL module-attribute

_AS_OF_TAIL = re.compile('\\bAS\\s+OF$', re.I)

_WRAP_TAIL module-attribute

_WRAP_TAIL = re.compile('(?:[\\s(]|\\bCAST\\b|\\bTIMESTAMP\\b|\\bDATE\\b)+$', re.I)

_PIT_KINDS module-attribute

_PIT_KINDS = ('add_months', 'interval', 'minus_n', 'trunc', 'month_begin')

_RANK module-attribute

_RANK = {HAND_WRITTEN: 5, PREVIOUS_VIEW: 4, FIXED_LITERAL: 3, POINT_IN_TIME: 2.5, CUMULATIVE: 2, UNKNOWN: 1, NOT_READ: 0}

_DAYS module-attribute

_DAYS = {'days': 1.0, 'months': 30.4375, 'seconds': 1.0 / 86400.0}

_obj

_obj(name) -> str

_strip_comments

_strip_comments(text: str) -> str

_classify_names

_classify_names(managers: Iterable[str], previous: Iterable[str] = ()) -> Dict[str, str]

{OBJECT NAME: 'M' | 'P'}: manager views (clock, clones) and previous views, by object name. A name ending in _PREVIOUS is a previous view when its base is a manager or it is listed.

tokenise

tokenise(text: str, managers: Iterable[str], previous: Iterable[str] = ()) -> Tuple[str, Dict[str, dict]]

(text with the manager values replaced by __M<n>__ / __P<n>__, {token: {kind, name}}). Pure.

_readable

_readable(snippet: str, meta: Dict[str, dict]) -> str

_step

_step(kind, m) -> Optional[Tuple[str, int]]

_expression_key

_expression_key(text: str, meta: Dict[str, dict]) -> str

The expression without spaces, each manager token by the manager's name (two scalar reads of the same manager carry two tokens but are the same value).

_comparator_before

_comparator_before(t: str, pos: int) -> Optional[str]

How the expression starting at pos is used: 'asof' (AS OF <expr>), 'eq', 'upper' (the expression follows <= / <: the column is bounded above by it), 'lower' (> / >=) or None (anything else).

_point_in_time_hits

_point_in_time_hits(t: str, meta: Dict[str, dict], hits: List[dict]) -> set

Indexes (in hits) of the shifted-date expressions that pin ONE instant. Pure.

eq / asof pin it by themselves. An upper and a lower use of the SAME expression pin it together (a validity interval containing the instant); a window has its shifted expression on one side only, the manager value itself bounding the other.

classify_window

classify_window(text: str, managers: Iterable[str], previous: Iterable[str] = ()) -> dict

Classify ONE SQL text. Returns {kind, step: {unit, n} | None, snippet, found: [...]}; kind is not_read when none of the managers is read. Pure.

classify_chain

classify_chain(steps: List[dict], managers: Iterable[str], previous: Iterable[str] = ()) -> dict

Classify a process: steps = [{'name', 'text'}] (the process view first, then the views it reads). One result per process: the most telling kind (hand_written > previous_view > fixed_literal > point_in_time > cumulative > unknown) with the view where it was found, and views = every view with its own kind. A chain reading no manager is not_read. Pure.

step_days

step_days(step) -> Optional[float]

A step {'unit', 'n'} or (unit, n) in days (a month = 30.44 days), or None.

compare_step

compare_step(implied, rule_step) -> str

How a window of implied step reads a calendar whose step is rule_step: equal, overlap (the window is longer than the step: an event falls in several windows), gap (shorter: events fall in none) or incomparable. A month against 28-31 days is equal only for the same unit; other differences under 5% (a year against twelve months) are equal. Pure.

step_text

step_text(step) -> str

replacement_snippet

replacement_snippet(manager_view: str, previous_view: Optional[str] = None) -> str

The predicate that reads the previous view in place of date arithmetic.

window_findings

window_findings(rows: List[dict]) -> List[dict]

Findings from classified processes. rows: [{process_id, view, name, manager, previous_view, rule_step: (unit, n) | None, rule_text, result: classify_chain(...)}]. Returns plain dicts {kind, severity, object, detail, fix, pids} (hand_written_window info, window_mismatch_calendar warning, fixed_window_bound info). A point_in_time read (one instant, 0.3.7.37) has no window, so no length to compare with the step: it gives no finding. Pure.