Skip to content

tdfs4ds.view_scorecard

tdfs4ds.view_scorecard

A scorecard for a view that reads the feature store (0.3.8.11).

A view a user built on a dataset (joined with a source table, filtered, renamed) can be attached to the feature store (Documentation > Attached objects). Before attaching or sampling it, the scorecard says what it is and how it reads its sources:

  1. identity: the datasets / process views it reads, whether it is attached, its sources and the views below it down to the base tables (SHOW VIEW, at most three levels);
  2. columns and types, each one passed through from a source column or computed, with the documentation already written for it;
  3. the view's SQL;
  4. a purpose written on demand by the instruct model (the panel's job, not this module's);
  5. its filter and join conditions (:func:tdfs4ds.agent.sql_context.view_predicates), each classified against the primary index and partitioning of the source it constrains: single_amp, partition_elimination, join_on_pi or none;
  6. sample filters: a view column that passes a source's primary index or partitioning column through unchanged gives a filter whose plan reads few rows; each one is checked with a DYNAMIC EXPLAIN (incremental planning forced) and says how many partitions it reads;
  7. statistics: the optimizer's own recommendations (DIAGNOSTIC HELPSTATS) for the full read and for the filtered reads, and the predicate / join / partitioning columns of a source that no statistic covers. Rendered as text for the table owner: never collected here;
  8. physical design suggestions, each with its reason (redistribution of the larger side of a join, a large table filtered on a date it is not partitioned on, skew, an all-rows scan of a large source with nothing to filter it on).

Three layers, as elsewhere in the package: :func:collect_view_scorecard_inputs (the only I/O, fail-open: each failed read is an unverified entry with the whole database message), :func:build_view_scorecard (pure) and :func:render_view_scorecard_html (scoped CSS, no script).

It never writes. Every statement goes through :func:check_read_only (EXPLAIN, DYNAMIC EXPLAIN, DIAGNOSTIC HELPSTATS ON / NOT ON FOR SESSION, SELECT, HELP, SHOW); the full read is an EXPLAIN only, never executed. A source guard test checks it.

ALLOWED_PREFIXES module-attribute

ALLOWED_PREFIXES = ('EXPLAIN ', 'DYNAMIC EXPLAIN ', 'DIAGNOSTIC HELPSTATS ON FOR SESSION', 'DIAGNOSTIC HELPSTATS NOT ON FOR SESSION', 'SELECT ', 'SEL ', 'HELP ', 'SHOW ', 'CURRENT VALIDTIME SELECT ', 'LOCKING ROW FOR ACCESS SELECT ')

ALIGN_MIN_MB module-attribute

ALIGN_MIN_MB = 1000

SKEW_PCT module-attribute

SKEW_PCT = 30.0

CHEAP_SECONDS module-attribute

CHEAP_SECONDS = 5.0

MAX_DEPTH module-attribute

MAX_DEPTH = 3

MAX_FILTERS module-attribute

MAX_FILTERS = 3

_DATE_CODES module-attribute

_DATE_CODES = {'DA'}

_TS_CODES module-attribute

_TS_CODES = {'TS', 'SZ', 'AT', 'TZ'}

_NUM_CODES module-attribute

_NUM_CODES = {'I', 'I1', 'I2', 'I8', 'D', 'N', 'F'}

_TEXT_CODES module-attribute

_TEXT_CODES = {'CV', 'CF', 'CO'}

_FILTER_WORDS module-attribute

_FILTER_WORDS = {'AND', 'OR', 'NOT', 'IN', 'BETWEEN', 'IS', 'NULL', 'DATE', 'TIMESTAMP'}

CSS module-attribute

CSS = '\n.td-vsc{--navy:var(--td-navy,#00233C);--orange:var(--td-orange,#FF5F00);--slate:var(--td-slate,#5A6B7B);\n--surface:var(--td-surface,#FFFFFF);--soft:var(--td-soft,#F5F7FA);--border:var(--td-border,#E2E8F0);\ncolor:var(--navy);font-size:13px;line-height:1.45}\n.td-vsc h3{font-size:15px;margin:18px 0 6px;border-left:4px solid var(--orange);padding-left:8px}\n.td-vsc .kpis{display:flex;flex-wrap:wrap;gap:8px;margin:6px 0 10px}\n.td-vsc .kpi{flex:1 1 150px;background:var(--surface);border:1px solid var(--border);border-radius:10px;padding:8px 12px}\n.td-vsc .kpi b{display:block;font-size:16px}.td-vsc .kpi span{font-size:12px;color:var(--slate)}\n.td-vsc table{border-collapse:collapse;width:100%;font-size:12.5px;background:var(--surface)}\n.td-vsc th{background:var(--soft);text-align:left;padding:5px 8px;border-bottom:2px solid var(--border)}\n.td-vsc td{padding:4px 8px;border-bottom:1px solid var(--border);vertical-align:top}\n.td-vsc .note{color:var(--slate);font-size:12.5px;margin:4px 0}\n.td-vsc .chip{display:inline-block;border-radius:10px;padding:0 8px;font-size:11.5px;border:1px solid var(--border);background:var(--soft)}\n.td-vsc .chip.good{border-color:#15803d;color:#15803d}.td-vsc .chip.warn{border-color:#b45309;color:#b45309}\n.td-vsc .warning{color:#b45309;font-weight:600}\n.td-vsc pre{white-space:pre-wrap;background:var(--soft);border:1px solid var(--border);border-radius:6px;padding:8px;font-size:12px;max-height:420px;overflow:auto}\n.td-vsc code{font-size:12px}\n.td-vsc .scroll{overflow-x:auto}\n'.strip()

_CLASS_LABEL module-attribute

_CLASS_LABEL = {'single_amp': 'one AMP', 'partition_elimination': 'partition elimination', 'join_on_pi': 'join on the primary index', 'none': 'no access path'}

_u

_u(x) -> str

_esc

_esc(x) -> str

_strip_literals

_strip_literals(sql: str) -> str

check_read_only

check_read_only(sql: str) -> str

sql when it is a statement this module may send, else ValueError. Pure.

One statement only (no ; outside a literal) starting with one of ALLOWED_PREFIXES.

_default_run_sql

_default_run_sql(sql: str) -> Tuple[List[str], list]

(column names, rows) of sql on the app's teradataml connection.

_rows_of

_rows_of(run, sql) -> list

_plan_text

_plan_text(run, sql) -> str

_err

_err(exc) -> str

parse_helpstats

parse_helpstats(plan: str) -> List[dict]

The statistics DIAGNOSTIC HELPSTATS appends to an EXPLAIN. Pure.

The plan ends with (lines wrapped anywhere, seen live on Vantage 20)::

BEGIN RECOMMENDED STATS FOR FINAL PLAN->
-- "COLLECT STATISTICS COLUMN (REGION ,CUST_ID) ON
demo_user.TMP_SC_CUST" (Medium Confidence)
<- END RECOMMENDED STATS FOR FINAL PLAN

and sometimes a ... FOR OTHER PLANS block. Returns [{block, statement, columns, table, confidence}]; block is final or other.

partitions_read

partitions_read(plan: str, source: str, aliases=()) -> Optional[int]

The largest number of partitions a step of plan reads from source ('DB.TABLE', or one of its correlation names), 1 for "a single partition"; None when no step says so. Pure.

plan_moves

plan_moves(plan: str, source: str, aliases=()) -> Optional[str]

How the steps of plan reading source move its rows: redistributed (by the hash code of a join column), duplicated (on all AMPs), or None (left in place, or not read). Pure.

literal_for

literal_for(value, code: str) -> Optional[str]

A SQL literal for value of HELP COLUMN type code, None when it cannot be written. Pure.

trace_origins

trace_origins(view_key: str, infos: Dict[str, dict], view_columns=None) -> Dict[str, tuple]

Where each view column comes from, followed down the views of infos ({"DB.VIEW": parse_view_sources(...)}). Pure.

{VIEW COLUMN: ("base", (DB, TABLE, COLUMN))} for a column passed through unchanged down to the relation that holds it (a table, or a view not read), ("computed", "DB.VIEW", expression) for a column computed in this view or one below it; a column whose origin is not known is left out (several * sources, an unreadable view).

trace_pass_through

trace_pass_through(view_key: str, infos: Dict[str, dict], view_columns=None) -> Dict[str, Tuple[str, str, str]]

{VIEW COLUMN: (DB, TABLE, COLUMN)} for the columns the view passes through unchanged, followed down to the relation that holds them (:func:trace_origins). Pure.

classify_predicates

classify_predicates(preds: List[dict], physical: Dict[str, dict]) -> List[dict]

Each predicate with access (list) and classification (the best of single_amp, partition_elimination, join_on_pi, else none) and why. Pure.

physical: {"DB.TABLE": {"pi", "part_cols", "size_bytes", "skew", ...}} of the base tables.

sample_filter_candidates

sample_filter_candidates(trace: Dict[str, tuple], physical: Dict[str, dict], view_types: Dict[str, str]) -> List[dict]

View columns that pass a source's primary index or partitioning column straight through. Pure.

[{column, source, source_column, role: 'pi' | 'partition', type_code}], partitioning first (a range over the latest partition is a sample with a known cost), then a one-column primary index.

render_sample_filter

render_sample_filter(cand: dict, example) -> dict

{filter, runnable, note} for a candidate and an example value (None = unknown). Pure.

validate_sample_filter

validate_sample_filter(text: str, columns) -> Tuple[bool, str]

Whether text may follow WHERE in a sample of the view. Pure.

Accepted: the view's own columns, = <> < <= > >= BETWEEN IN IS [NOT] NULL AND OR NOT, parentheses, numbers, quoted strings ('' escaping), DATE '...' and TIMESTAMP '...'. Refused: ;, --, /*, any other word (functions, sub-queries), unbalanced quotes or parentheses. Returns (ok, reason).

statistics_needed

statistics_needed(preds: List[dict], physical: Dict[str, dict]) -> List[dict]

The predicate / join / partitioning / primary-index columns of each base source that no statistic in DBC.StatsV covers (sources whose statistics were not readable are skipped). Pure.

design_suggestions

design_suggestions(preds: List[dict], physical: Dict[str, dict], full_plan: str, *, aliases: Optional[Dict[str, list]] = None, filters: Optional[List[dict]] = None, align_min_mb: float = ALIGN_MIN_MB) -> List[dict]

Physical design changes worth discussing with the table owner, each with its reason. Pure.

_help_columns

_help_columns(run, db, name) -> List[dict]

_dbc

_dbc(run, view, columns, keys, where='', unverified=None, what='')

_dynamic_plan

_dynamic_plan(sql: str, run, default: bool) -> Tuple[str, bool]

(plan, dynamic): DYNAMIC EXPLAIN with incremental planning forced when possible.

_helpstats

_helpstats(sqls: List[str], run, default: bool) -> Tuple[Dict[str, str], Optional[str], str]

({sql: plan with recommendations}, error, where): DIAGNOSTIC HELPSTATS for the session, the EXPLAINs, then NOT ON (always, in finally). On its own connection when the TDFS4DS_TD_* settings allow it, so the app's session is never left with the diagnostic on.

_helpstats_on

_helpstats_on(sqls, run, plans)

collect_view_scorecard_inputs

collect_view_scorecard_inputs(view: str, *, run_sql: Optional[Callable] = None, helpstats: bool = True, fs_objects: Optional[Callable] = None, attached: Optional[Callable] = None, progress: Optional[Callable] = None) -> dict

Everything the scorecard of view ('DB.VIEW') needs. The only I/O; fail-open.

run_sql(sql) -> (column names, rows) sends one statement (default: the app's teradataml connection); every statement passes :func:check_read_only first. fs_objects() / attached() return {(DB, OBJ): 'process' | 'dataset'} / {(DB, OBJ): kind} (default: the package's readers). A read that fails is an unverified entry with the whole message.

build_view_scorecard

build_view_scorecard(inp: dict) -> dict

The scorecard model from :func:collect_view_scorecard_inputs. Pure.

purpose_prompt

purpose_prompt(model: dict, language: str = 'en') -> Tuple[str, str]

(system, user) for the instruct model: what the view is, from its SQL, columns and sources. Pure.

_table

_table(head, rows) -> str

render_view_scorecard_html

render_view_scorecard_html(model: dict) -> str

The scorecard as an HTML fragment for the admin app (scoped style, no script). Pure.