Skip to content

tdfs4ds.impact_features

tdfs4ds.impact_features

Feature-level impact of a change: which features of which readers, and the chain.

:func:tdfs4ds.impact.impact_of judges each downstream object as a whole. This module adds, to the same model, the reading at feature level:

  • features - one entry per feature (or column) of the edited object, with its state (unchanged / logic_changed / type_changed / removed / added / unknown), its severity (breaks > values_change > may_change > unaffected) and its readers: for each object that reads it, which of the reader's own features or columns are affected and which are not;
  • feature_edges - the feature-to-feature chain (a feature, then the dataset column or the reader feature computed from it, then the next reader...), each edge with its reason;
  • severity_counts - the distinct features and columns of the study, by severity;
  • schema_version = 2.

How a reader is narrowed:

  • a dataset reads features by FEATURE_ID: exactly the columns of the features concerned are affected, with the consequence of its join (INNER: rows lost, FULL OUTER: the column becomes NULL);
  • a process, a view or an attached view: its SQL is split into output items; only the items whose expression names a changed column (or pass it through, :func:sql_context.column_lineage) are affected. SELECT *, a changed column named in its FROM / WHERE / JOIN, or a select list that cannot be split is CONSERVATIVE (narrowed=False, severity at least may_change);
  • an affected feature of a reader process seeds the next round, up to max_depth (default 6), with a cycle guard.

PURE: no database read, no write, no language model. Everything comes from the inputs that :func:tdfs4ds.impact.collect_impact_inputs already read (lineage graph, view texts, catalogs).

SCHEMA_VERSION module-attribute

SCHEMA_VERSION = 2

DEFAULT_MAX_DEPTH module-attribute

DEFAULT_MAX_DEPTH = 6

BREAKS module-attribute

BREAKS = 'breaks'

VALUES_CHANGE module-attribute

VALUES_CHANGE = 'values_change'

MAY_CHANGE module-attribute

MAY_CHANGE = 'may_change'

UNAFFECTED module-attribute

UNAFFECTED = 'unaffected'

SEVERITIES module-attribute

SEVERITIES = (BREAKS, VALUES_CHANGE, MAY_CHANGE, UNAFFECTED)

_RANK module-attribute

_RANK = {UNAFFECTED: 0, MAY_CHANGE: 1, VALUES_CHANGE: 2, BREAKS: 3}

STATES module-attribute

STATES = ('unchanged', 'logic_changed', 'type_changed', 'removed', 'added', 'unknown')

TYPE_ONLY module-attribute

TYPE_ONLY = 'type only (values identical if the type widens)'

TYPE_NOTE module-attribute

TYPE_NOTE = 'type changed: values identical, readers that cast or compare this column should be checked'

PARSE_LIMIT_NOTE module-attribute

PARSE_LIMIT_NOTE = 'the parser cannot follow this: have an expert review'

_BUILTINS module-attribute

_BUILTINS = frozenset({'cast', 'trim', 'extract', 'substr', 'substring', 'coalesce', 'nullif', 'case', 'count', 'sum', 'min', 'max', 'avg', 'over', 'in', 'exists', 'values', 'using', 'on', 'as'})

_KEYWORD_BEFORE_NAME module-attribute

_KEYWORD_BEFORE_NAME = re.compile('(?is)\\b(?:FROM|JOIN|INTO|TABLE|UPDATE|VIEW|TO|REFERENCES|ON)\\s*$')

_NAME_CALL module-attribute

_NAME_CALL = re.compile('(?<![\\w$#.])([A-Za-z_][\\w$#]*)\\.([A-Za-z_][\\w$#]*)\\s*\\(')

_SIMPLE_CONSTRUCTS module-attribute

_SIMPLE_CONSTRUCTS = (('table operator call (EXECUTE FUNCTION)', re.compile('(?i)\\bEXECUTE\\s+FUNCTION\\b')), ('table operator TABLE(...)', re.compile('(?i)(?<![\\w$#.])TABLE\\s*\\(')), ('table operator input (HASH BY / DIMENSION)', re.compile('(?is)\\bON\\b.*\\b(?:HASH\\s+BY|DIMENSION)\\b')), ('SCRIPT(...)', re.compile('(?i)(?<![\\w$#.])SCRIPT\\s*\\(')), ('APPLY(...)', re.compile('(?i)(?<![\\w$#.])APPLY\\s*\\(')), ('PIVOT / UNPIVOT', re.compile('(?i)\\b(?:UN)?PIVOT\\s*\\(')), ('JSON_TABLE', re.compile('(?i)\\bJSON_TABLE\\b')), ('JSON function (shredding)', re.compile('(?i)(?<![\\w$#.])JSON_(?!TABLE\\b)[A-Z_]+\\s*\\(')), ('XML function', re.compile('(?i)\\b(?:XMLTABLE|XMLQUERY|XMLEXTRACT|XMLPUBLISH|XMLAGG|XMLELEMENT|EXTRACTVALUE)\\b')), ('WITH RECURSIVE', re.compile('(?i)\\bWITH\\s+RECURSIVE\\b')), ('dynamic SQL', re.compile('(?i)\\bEXECUTE\\s+IMMEDIATE\\b|\\bSYSEXECSQL\\b|\\bDBC\\.SYSEXECSQL\\b')), ('macro or procedure call', re.compile('(?i)^\\s*(?:CALL|EXEC|EXECUTE)\\s+(?!FUNCTION\\b|IMMEDIATE\\b)[A-Za-z_\\"]|(?<=;)\\s*(?:CALL|EXEC)\\s+[A-Za-z_\\"]')), ('TD_ analytic function', re.compile('(?i)(?<![\\w$#.])TD_[A-Z0-9_]+\\s*\\(')))

_Ctx

im instance-attribute

im = im

inputs instance-attribute

inputs = inputs

H instance-attribute

H = H

G instance-attribute

G = inputs.get('graph')

ddl instance-attribute

ddl = inputs.get('ddl') or {}

ddl_err instance-attribute

ddl_err = inputs.get('ddl_errors') or {}

attached instance-attribute

attached = inputs.get('attached') or {}

max_depth instance-attribute

max_depth = max_depth

pid_view instance-attribute

pid_view = {p: n for n, p in self.view_pid.items()}

targets instance-attribute

targets = {t.get('node') for t in model.get('targets') or [] if t.get('node')}

effect instance-attribute

effect: Dict[str, Dict[str, Dict[int, dict]]] = {}

inputs_of instance-attribute

inputs_of: Dict[str, set] = {}

depth instance-attribute

depth: Dict[str, int] = {}

records instance-attribute

records: Dict[Tuple[str, int], dict] = {}

edges instance-attribute

edges: Dict[tuple, dict] = {}

edge_origins instance-attribute

edge_origins: Dict[str, set] = {}

unverified instance-attribute

unverified: List[dict] = []

truncated instance-attribute

truncated: set = set()

proc_features instance-attribute

proc_features = {}

__init__

__init__(model, inputs, H, max_depth)

kind_of

kind_of(n) -> Optional[str]

label_of

label_of(n) -> str

readers_of

readers_of(u) -> set

add_edge

add_edge(origin, src, kind, obj, item, is_feature, via, sev, reason) -> str

merge

merge(node, col, origin, info) -> bool

worst

worst(*sev) -> str

The most severe of the given severities (unaffected for none).

_u

_u(v) -> str

_names

_names(csv) -> List[str]

_feature_nodes_of

_feature_nodes_of(H, pid) -> Dict[str, dict]

{NAME: attrs} of the features process pid produces (dependency graph).

_declared

_declared(props, name: str, domain=None) -> List[str]

The declared checks of a feature, as short texts (empty when none).

_seeds

_seeds(model: dict, inputs: dict, H) -> List[dict]

The feature entries of every target, with the node and column they start from.

_join_of

_join_of(H, dsid) -> Optional[str]

_dataset_consequence

_dataset_consequence(sev, state_word, cols, join) -> str

_sql_consequence

_sql_consequence(kind, sev, affected, narrowed) -> str

_judge

_judge(ctx: _Ctx, r: str, origin: int, seed: dict) -> Optional[Tuple[dict, Dict[str, dict]]]

(record, {item: info}) for reader r and the feature origin, or None when r does not read anything coming from that feature.

_propagate

_propagate(ctx: _Ctx, seeds: List[dict]) -> None

add_feature_impact

add_feature_impact(model: dict, inputs: dict, *, H=None, max_depth: int = DEFAULT_MAX_DEPTH) -> dict

Add schema_version, features, feature_edges and severity_counts to an :func:tdfs4ds.impact.build_impact_model model (in place; the model is returned). No existing key is changed, except that unverified gains the entries of the feature chain (where it stops, and why). Pure.

_severity_counts

_severity_counts(features: List[dict], edges: List[dict]) -> Dict[str, int]

The distinct items of the study by severity: the edited features and every reader feature or column reached. Pure.

_edited_phrase

_edited_phrase(f: dict) -> str

How an edited feature changes, as the end of 'X reads E, which ...'.

_edited_short

_edited_short(f: dict) -> str

_consequence_phrase

_consequence_phrase(sev: str, measured: List[str]) -> str

_names_list

_names_list(names: List[str], limit: int = 5) -> str

explain_dependent_feature

explain_dependent_feature(name: str, sev: str, because: List[dict], unaffected: List[str], narrowed: bool = True, kind: Optional[str] = None) -> str

One plain-English sentence, written by code: why the feature name of a dependent is affected, and which of the dependent's features are not. because: [{edited_feature, phrase, measured}]. Pure.

by_dependent

by_dependent(model: dict) -> List[dict]

One entry per object depending on the edited object: which of its features may be affected, by which edited feature, and which are not. Built from features / feature_edges. Pure.

Each entry: {object, kind, label, process_id | dataset_id, severity, narrowed, features: [{name, severity, evidence, because: [{edited_feature, process_id, via, why}], explanation}], unaffected_features}, the most severe first.

apply_measurement

apply_measurement(model: dict, measurement: Optional[dict]) -> dict

Apply a :func:tdfs4ds.impact_measure.measure result to an impact model (in place; the model is returned). The measurement is kept as measure. When it ran (state done), every edited feature it compared gets evidence: 'measured': a feature whose values differ keeps its severity (raised to values_change when the SQL reading said less); a feature whose values are identical goes from values_change / may_change to unaffected, and so do its readers and the edges of its chain (the chain stops there). A feature measured identical whose state is type_changed keeps that verdict for its values but gets type_note (:data:TYPE_NOTE): a reader that casts or compares the column may still depend on its type; the dependents reading it list it in type_notes. severity_counts and dependents are recomputed. The other features keep evidence: 'semantic'. Pure.

feature_readers

feature_readers(model: dict, feature: str, process_id: Optional[str] = None) -> List[dict]

The readers of one feature of a model built with features (pure).

feature_chain

feature_chain(model: dict, feature: str, process_id: Optional[str] = None) -> List[dict]

The edges of the chain that starts at one feature (pure).

_mask

_mask(sql: str) -> str

sql with comments and the content of string literals blanked (same length).

_enclosing_open

_enclosing_open(text: str, pos: int) -> int

Index of the unmatched ( before pos (-1 when at depth 0).

complex_constructs

complex_constructs(sql: Optional[str]) -> List[str]

The constructs of sql that resist the parser, as short labels (empty when none). Pure.

Table operators and EXECUTE FUNCTION, TD_* analytic functions, table-operator inputs (ON ... PARTITION BY / HASH BY / DIMENSION, TABLE(), SCRIPT( / APPLY(, PIVOT / UNPIVOT, JSON and XML shredding, WITH RECURSIVE, dynamic SQL, macro and procedure calls, QUALIFY over a derived column, and user-defined functions (a qualified schema.name( call). Keywords inside string literals and comments are ignored.

study_texts

study_texts(model: dict, inputs: Optional[dict]) -> List[Tuple[str, str, str, Optional[str]]]

Every SQL text of the study as (name, sql, kind, node): the current SQL of the edited objects (kind target), the proposed SQL (proposed) and the SQL of every reader reached (reader). Pure.

parse_limits

parse_limits(model: dict, inputs: Optional[dict]) -> List[dict]

[{object, constructs, note}] for every SQL text of the study that holds constructs the parser cannot follow: the proposed SQL, the edited objects and every reader reached. Pure.

add_parse_limits

add_parse_limits(model: dict, inputs: Optional[dict]) -> dict

Set model["parse_limits"] (in place, never raises). Pure.