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/EXTRACTon 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;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.
_QN
module-attribute
¶
_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 = compile('\\(\\s*SEL(?:ECT)?\\b(?P<sel>(?:[^()]|\\([^()]*\\))*?)\\bFROM\\s+(?P<m>' + _QN + ')\\s*(?:(?:AS\\s+)?\\w+)?\\s*\\)', IGNORECASE | DOTALL)
_PAT
module-attribute
¶
_PAT = {'add_months': compile(f'ADD_MONTHS\s*\(\s*{_ARG}\s*,\s*-\s*(\d+)\s*\)', I), 'interval': compile(f'{_ARG}\s*-\s*INTERVAL\s*'\s*(\d+)\s*'\s*(DAY|MONTH|YEAR|HOUR|MINUTE)', I), 'minus_n': compile(f'{_ARG}\s*-\s*(\d+)(?![\d.])(?!\s*[*/])', I), 'trunc': compile(f'TRUNC\s*\(\s*{_ARG}\s*(?:,\s*'(\w+)')?\s*\)', I), 'month_begin': compile(f'(MONTH_BEGIN|MONTH_END|LAST_DAY|TD_MONTH_BEGIN|TD_YEAR_BEGIN|TD_WEEK_BEGIN)\s*\(\s*{_ARG}', I), 'extract': compile(f'EXTRACT\s*\(\s*(MONTH|YEAR|WEEK|DAY|QUARTER)\s+FROM\s+{_ARG}\s*\)', I), 'between': compile(f'BETWEEN\s+(?P<lo>[^;]{1,300}?)\s+AND\s+(?:CAST\s*\(\s*)?(?P<hi>{_TOK})', I)}
_LITERAL
module-attribute
¶
_LITERAL = compile("(?:DATE|TIMESTAMP)\\s*'[^']*'|'\\d{4}-\\d{2}-\\d{2}[^']*'|CAST\\s*\\(\\s*'[^']*'\\s+AS\\s+(?:DATE|TIMESTAMP)", I)
_UPPER
module-attribute
¶
_UPPER = compile(f'(?:<=|<)\s*(?:CAST\s*\(\s*)?{_TOK}|BETWEEN[^;]{1,300}?\s+AND\s+(?:CAST\s*\(\s*)?{_TOK}', 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)}
_RANK
module-attribute
¶
_RANK = {HAND_WRITTEN: 5, PREVIOUS_VIEW: 4, FIXED_LITERAL: 3, CUMULATIVE: 2, UNKNOWN: 1, NOT_READ: 0}
_classify_names
¶
{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.
classify_window
¶
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 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 > 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
¶
A step {'unit', 'n'} or (unit, n) in days (a month = 30.44 days), or None.
compare_step
¶
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.
replacement_snippet
¶
The predicate that reads the previous view in place of date arithmetic.
window_findings
¶
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). Pure.