Skip to content

tdfs4ds.dataset.grain

tdfs4ds.dataset.grain

Datasets at the primary-index grain of an extended entity (0.3.7.35).

A process sometimes adds columns to entity_id only so a FilterManager can segment on them or so the FS_T_* table can be partitioned on them (both need the column in the entity), while its primary index stays the real grain, e.g. entity (CUSTOMER_ID, ENTITY_BUCKET) with primary_index=['CUSTOMER_ID']. A dataset keyed on exactly the primary-index columns can read such a feature: the dataset renderer only selects and joins the dataset's own entity columns, so the storage-only columns never appear in the view.

Rules, all here (selection, the app builder and the agent call them):

  • classify: exact (the feature's entity IS the dataset's), primary_index (its entity strictly contains the dataset's AND its recorded primary index equals the dataset's), near (strictly contains it, primary index differs: never used, only named in an error message), else None. An exact match always wins.
  • features_for_entity: the one pure listing of what a dataset on E can use.
  • check_grain: ONE statement, run once per build, that checks every selected primary-index-grain feature is unique at the dataset's grain. The result is kept on the specs (grain_checked), so the render, the registration split and the offsets rows never run it again.

Nothing here filters, fills or rewrites a stored value.

EXACT module-attribute

EXACT = 'exact'

PRIMARY_INDEX module-attribute

PRIMARY_INDEX = 'primary_index'

NEAR module-attribute

NEAR = 'near'

SUBSET module-attribute

SUBSET = 'subset'

OTHER module-attribute

OTHER = 'other'

DatasetGrainError

Bases: ValueError

A primary-index-grain feature is not unique at the dataset's grain, or the check could not be run. failing: [{'feature', 'n_keys', 'example'}]; tables: the storage tables read (named when the check failed to run).

failing instance-attribute

failing = list(failing)

tables instance-attribute

tables = list(tables)

__init__

__init__(message, failing=(), tables=())

quote_ident

quote_ident(name, force=False)

Return name as a Teradata identifier, quoting it only if required.

Idempotent: an already-quoted name is returned unchanged rather than being wrapped a second time. Embedded quotes are escaped as "".

Parameters

name : str The raw object/database name. force : bool, default False Quote even when the name does not need it. Use only at the few sites that have always emitted unconditional quotes and whose exact output is already baked into persisted DDL (see feature_store/feature_query_retrieval.py) — elsewhere forcing would change the SQL of existing stores for no reason.

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.

columns_of

columns_of(value) -> frozenset

Upper-cased column set of an entity key / primary index written as a list, a 'A,B' string or a dict (keys). Pure.

classify

classify(entity, storage_entity, primary_index) -> object

'exact' / 'primary_index' / 'near' / None (see the module). Pure.

classify_level

classify_level(entity, storage_entity, primary_index)

(kind, level) of a feature for a dataset keyed on entity, nested entities included. Pure.

level is the list of columns the feature joins on (sorted, upper-cased); None for no match.

  • exact: the feature's entity IS the dataset's, level = the entity;
  • subset: its entity is a strict subset (a coarser feature), level = its entity;
  • primary_index: it is stored with columns the dataset does not carry, and its primary index is a subset of the dataset's entity (strict or not) and strictly inside its own entity: it is read at the primary-index grain, level = the primary index (the Part B rule when that is the whole entity, a coarser level otherwise);
  • near: its entity strictly contains the dataset's, primary index differs (message only);
  • other: shares some column with the dataset's entity but none of the above (message only).

describe_coarser_column

describe_coarser_column(level, entity, left_over=None, base_description=None, french=False) -> str

Fixed sentence for a column stored at a coarser level than its dataset (no LLM).

level_text

level_text(level) -> str

'A, B' for a level list; '' for none.

left_out

left_out(entity, storage_entity) -> list

The storage entity's columns the dataset does not carry, sorted. Pure.

pi_by_process_from

pi_by_process_from(frame) -> dict

{PROCESS_ID: 'A,B'} (sorted, upper) from a frame with PROCESS_ID and FOR_PRIMARY_INDEX. A process with no recorded primary index is left out. Pure.

features_for_entity

features_for_entity(features_df, pi_by_process, entity, nested=False) -> list

What a dataset keyed on entity can use, one dict per (feature, producer).

features_df: rows with FEATURE_NAME, the entity key (ENTITY_NAME or ENTITY) and optionally PROCESS_ID / DATA_DOMAIN. pi_by_process: {process id: primary index} (:func:pi_by_process_from). Each result: {'feature_name', 'process_id', 'data_domain', 'match', 'storage_entity', 'left_out', 'primary_index'} with match 'exact' or 'primary_index'; with nested=True also the features at a coarser entity (match 'subset' or a 'primary_index' at a lower level) and every row carries level. A row with no process cannot be primary-index-grain (its index is unknown). A feature name found both exactly and at PI grain yields both rows (the caller picks; the exact one is the default). Pure.

entities_reached_through_pi

entities_reached_through_pi(features_df, pi_by_process) -> dict

{domain: {entity key: n features}} of the entities that only primary-index-grain features reach: the PI of an extended-entity process, as an entity of its own. Pure.

entities_through_pi_of_processes

entities_through_pi_of_processes(processes) -> dict

{domain: {PI key: n features}} from a process catalog frame (DATA_DOMAIN, ENTITY_ID, FOR_PRIMARY_INDEX, FEATURE_NAMES): the entity a process's primary index makes available when it is a strict subset of the process's entity. Pure.

grain_specs

grain_specs(specs) -> list

The specs read at PI grain (they carry a storage_entity). Pure.

_concat

_concat(cols, alias, width=255) -> str

_window_predicate

_window_predicate(time, has_offsets) -> str

'' for a whole-history check, else the valid-instant predicate. Pure.

spec_grain_columns

spec_grain_columns(spec) -> list

The columns a primary-index-grain spec is read at: its storage entity minus the left-out ones, sorted. Pure.

grain_check_sql

grain_check_sql(specs, entity=None, time=None, has_offsets=False) -> str

The single statement checking every primary-index-grain spec. Pure.

One row per feature that is NOT unique at its grain: FEATURE_ID, N_KEYS (grain values that break), EXAMPLE (the smallest). No row = every feature unique.

  • inner: one derived-table leg per storage table (temporal qualifier inside the leg, a UNION branch refuses a leading one), reading only the selected (FEATURE_ID, FEATURE_VERSION) pairs and projecting FEATURE_ID, the grain as one text (E_TXT: grain columns differ from a table to another, a UNION needs the same columns) and the left-out columns as one text (X);
  • snapshot (current / as of): at that valid time HAVING COUNT(*) > 1 per (FEATURE_ID, E_TXT); history / TimeManager / offsets: the whole history, HAVING COUNT(DISTINCT X) > 1 (conservative);
  • outer: grouped again by FEATURE_ID.

entity is only informative: each spec carries its own grain.

sanity_specs

sanity_specs(reads, entity_of_feature, pi_by_process, entity_of_dataset)

(specs, datasets_of) for the sanity re-check: the feature reads of the registered datasets that are at primary-index grain. reads: dependencies.dataset_feature_reads; entity_of_feature: {FEATURE_ID: entity key}; pi_by_process: {PROCESS_ID: 'A,B'}; entity_of_dataset: {DATASET_ID: entity key}. A read is at PI grain when the dataset's entity IS the primary index of the producing process and is strictly inside the feature's entity. datasets_of: {feature_id: [dataset labels]}. Pure.

failing_rows

failing_rows(rows, specs, datasets_of, pids_of=None) -> list

The sanity result rows: {'feature', 'feature_id', 'n_keys', 'example', 'datasets', 'pids'} from the statement's rows (FEATURE_ID, N_KEYS, EXAMPLE). Pure.

statistics_hint

statistics_hint(tables) -> str

The call that defines the statistics the check reads through (primary index + FEATURE_ID).

refusal_message

refusal_message(failing, entity, names=None, window=False, tables=()) -> str

The text of a refused build: every failing feature with its count and an example.

check_grain

check_grain(specs, entity, time=None, run=None, has_offsets=None)

Check every primary-index-grain spec is unique at grain entity: ONE statement.

Runs only when at least one spec is primary-index-grain (nothing is read for an exact-match dataset), and only once: the result is stamped on the specs (grain_checked), so a second call returns at once. Raises :class:DatasetGrainError naming each failing feature (count + example), or the tables when the statement cannot run: it never lets a build through unchecked. run: sql -> rows (default teradataml.execute_sql). Returns the failing list (empty).