Skip to content

tdfs4ds.dataset.selection

tdfs4ds.dataset.selection

Feature selection of a dataset: names alone when a feature has one version.

build_dataset / build_historical_dataset accept selected_features as

  • a list of feature names (['amount', 'nb_tx']) or one name;
  • a dict {feature: version} whose value may be None ("the only version"), {'alias': ...} (the only version under a name of its own), or any of the explicit forms (a process id, {'process_id', 'alias'}, a list of versions side by side).

A version has to be written only for a feature that several processes compute. Everything is resolved with ONE read of the catalogs, whatever the number of features: :func:read_feature_producers (the only I/O) then the pure :func:resolve_selection, which returns the full {feature: version} dict and the storage locations in the shape get_feature_location returns.

FeatureSelectionError

Bases: ValueError

The selection names a feature that cannot be resolved unambiguously.

missing: names not registered for this entity and data domain. no_producer: registered names that no current process computes. ambiguous: {feature: [{'process_id', 'process_view_name'}]} for the features computed by several processes, with no version given.

missing instance-attribute

missing = list(missing)

no_producer instance-attribute

no_producer = list(no_producer)

ambiguous instance-attribute

ambiguous = dict(ambiguous or {})

__init__

__init__(message, missing=(), no_producer=(), ambiguous=None)

Locations

Bases: dict

{qualified table: [(feature_id, version, feature_name)]} as get_feature_location returns it, plus grain: {(table, feature_id): {'storage_entity', 'left_out', 'pi'}} for the features read at their primary-index grain (empty for an ordinary selection).

grain instance-attribute

grain = {}

__init__

__init__(*args, **kw)

qualified

qualified(database, object_name, force=False)

Build db.obj with each part quoted only as needed.

qualified('TDB', 'FS_X') == 'TDB.FS_X' (unchanged for ordinary names); qualified('SVC@CORP', 'FS_X') == '"SVC@CORP".FS_X'. An empty/None database yields the bare object name, so unqualified callers keep working.

force=True quotes both parts unconditionally — only for the sites that have always emitted quotes and whose exact text is already persisted (dataset view DDL, returned volatile-table names).

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.

_entity_key

_entity_key(entity_id)

normalise_selection

normalise_selection(selected_features)

{name: value} from any accepted form (insertion order kept).

producers_query

producers_query(entity_id, names, domain=None, pi_grain=False, nested=False)

The one catalog read: each requested feature with its location and producers.

pi_grain=True (what :func:read_feature_producers asks for) also returns the features whose entity key contains the requested one: it adds A.ENTITY_NAME and the producer's recorded B.FOR_PRIMARY_INDEX (a column of the same feature-split view, so still ONE statement) and replaces the exact entity test by a coarse one on the requested columns; :func:partition_grain_rows makes the exact decision in Python. nested=True (a multi-column entity) widens the coarse entity test to "names any of the requested columns", so features stored at a subset of the entity (coarser) come back too; Python decides. Without these flags the text is what it always was.

partition_grain_rows

partition_grain_rows(entity_id, raw_rows, store_present=False, nested=False)

Legacy-shaped rows from the rows of producers_query(pi_grain=True). Pure.

A row whose entity IS the requested one is kept as it always was. A row whose entity strictly contains it and whose primary index equals it is kept with a trailing dict {'grain': 'primary_index', 'storage_entity', 'left_out', 'pi'}; one whose primary index differs gets 'grain': 'near' (only used to word an error). Anything else is dropped.

nested=True (the dataset may read features at a coarser entity): a row stored at a strict subset of the entity (or at the primary-index grain of a subset) is kept with 'grain': 'subset' / 'primary_index' and a level; every dict also carries full (the feature joins on the whole entity); a row of an unrelated entity that shares a column gets 'grain': 'other' (the message names it).

read_feature_producers

read_feature_producers(entity_id, names, domain=None)

Rows (FEATURE_NAME, FEATURE_ID, FEATURE_DATABASE, FEATURE_TABLE, PROCESS_ID, VIEW_NAME) for the requested names; PROCESS_ID is None when no process computes the feature. One query. A feature stored with a wider entity key whose primary index is the requested key comes with a trailing grain dict (see :func:partition_grain_rows).

_core

_core(row)

(six or seven columns, grain dict or None) of a producer row.

_given_pids

_given_pids(value)

Process ids a selection value names (None, alias-only dict: none).

_first_store_rows

_first_store_rows(requested, rows)

Rows of one store per feature (rows read across stores carry the store last).

The store holding a process id the selection gives wins; otherwise the first store in the winning order. Rows without a store column are returned as they are.

_clean

_clean(value)

_readable

_readable(view_name)

DB.VIEW without the identifier quotes the catalog stores.

_snippet

_snippet(selection, ambiguous)

The selected_features to paste: one version chosen, or all side by side.

resolve_selection

resolve_selection(selected_features, rows, entity=None)

(selection, locations) from selected_features and the catalog rows.

selection is the full {feature: version} dict, keyed by the catalog's spelling; locations is {qualified table: [(feature_id, version, feature_name)]}, the shape get_feature_location returns. Raises :class:FeatureSelectionError listing every problem at once. Pure.

resolve_for_build

resolve_for_build(entity_id, selected_features, domain=None)

Read the catalogs once and resolve selected_features (see the module).