Skip to content

tdfs4ds.dataset.joinable

tdfs4ds.dataset.joinable

Tables and views a dataset can be joined with (0.3.8.8).

A dataset only holds features. The non-feature objects that carry the dataset's entity key (or a part of it) are the ones it can be joined with: a reference table, a customer master, a label table. The search is bounded by what the store knows: every object documented in the business dictionary or attached to the feature store, never a scan of the databases.

Three layers, like the other reports:

  • :func:collect_joinable_inputs - the only I/O, fail-open (a failed read goes to errors): the business dictionary, the attached markers, the catalogs, then the real columns, primary indexes, partitioning and size of the candidates;
  • :func:match_joinable - pure: which candidates carry the entity, how well, and what the join costs;
  • :func:joinable_objects_model / :func:find_joinable_objects - the public readers.

A name match is not a meaning match: the output carries the documented meaning of the join columns next to the match so a person (or the agent) can judge it.

Nothing here writes. :func:check_join_uniqueness reads rows, only after its EXPLAIN shows the read stays within the sample limits.

__all__ module-attribute

__all__ = ['collect_joinable_inputs', 'match_joinable', 'joinable_objects_model', 'find_joinable_objects', 'check_join_uniqueness', 'resolve_dataset', 'sample_refusal', 'apply_purpose', 'rank_by_purpose', 'select_by_purpose', 'FULL', 'SUBSET']

FULL module-attribute

FULL = 'full'

SUBSET module-attribute

SUBSET = 'subset'

RESULT_COLUMNS module-attribute

RESULT_COLUMNS = ['DATABASE_NAME', 'OBJECT_NAME', 'KIND', 'DOCUMENTED', 'ATTACHED', 'MATCH', 'JOIN_COLUMNS', 'UNIQUENESS', 'SIZE_MB', 'ACCESS', 'TYPE_WARNINGS', 'DESCRIPTION', 'COLUMN_MEANINGS', 'RELEVANT_COLUMNS', 'REASON']

NAME_NOT_MEANING module-attribute

NAME_NOT_MEANING = 'A name match is not a meaning match: check that the join columns mean the same thing in both objects before joining.'

_PURPOSE_BATCH module-attribute

_PURPOSE_BATCH = 150

_TEMPORAL_COLUMNS module-attribute

_TEMPORAL_COLUMNS = {'VALIDSTART', 'VALIDEND', 'VALIDPERIOD'}

_NUMBER module-attribute

_NUMBER = re.compile('\\b(?:BYTEINT|SMALLINT|INTEGER|INT|BIGINT|DECIMAL|DEC|NUMERIC|NUMBER|FLOAT|REAL|DOUBLE)\\b', re.I)

_CODE_FAMILY module-attribute

_CODE_FAMILY = {'CV': 'text', 'CF': 'text', 'CO': 'text', 'I': 'number', 'I1': 'number', 'I2': 'number', 'I8': 'number', 'D': 'number', 'F': 'number', 'N': 'number', 'DA': 'date', 'TS': 'timestamp', 'SZ': 'timestamp'}

_WORD module-attribute

_WORD = re.compile('[A-Za-zÀ-ÿ0-9]+')

_STOP module-attribute

_STOP = {'the', 'a', 'an', 'of', 'for', 'to', 'and', 'or', 'in', 'on', 'with', 'le', 'la', 'les', 'un', 'une', 'des', 'de', 'du', 'et', 'ou', 'pour', 'sur', 'dans', 'avec', 'study', 'studying', 'etudier', 'étudier', 'analyse', 'analyze', 'analysis', 'analyser', 'about'}

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).

split_qualified

split_qualified(name)

Split db.obj into (database, object), both de-quoted.

Quote-aware, so a dot inside a quoted part does not split the name: split_qualified('"DEINF.CARLOSN"."FS_X"') == ('DEINF.CARLOSN', 'FS_X'). The naive rsplit('.', 1) this replaces returns ('"DEINF.CARLOSN"', '"FS_X"') only by luck and ('"DEINF', 'CARLOSN"."FS_X"') in the split('.') variant.

An unqualified name returns ('', name).

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.

_s

_s(v) -> str

_u

_u(v) -> str

_split

_split(name, db=None)

_cols

_cols(value) -> list

Column names of a key written as a list, 'A,B' or a dict, in order, upper-cased.

type_family

type_family(value) -> str

'text' | 'number' | 'date' | 'timestamp' | '' of a SQL type text or a DBC ColumnType code.

_access_text

_access_text(cand: dict, join_cols: list) -> str

What joining this object costs, from the dictionary alone (pure).

_uniqueness

_uniqueness(cand: dict, join_cols: list) -> str

'proven' when a unique primary or secondary index lies inside the join columns, else 'unknown'.

match_joinable

match_joinable(entity, inputs: dict, *, include_subset: bool = True, entity_types: Optional[dict] = None) -> list

The candidates of inputs that carry entity (a list of key columns), ranked. Pure.

match is full (every entity column is in the object) or subset (some are). Ranked full first, then by the number of join columns, documented before attached before the rest. A type difference between the key columns (text against number, a date against a text) is a warning in type_warnings (a CAST is needed), never a reason to drop the object.

sample_refusal

sample_refusal(row: dict, max_gb: float = 10) -> Optional[str]

Why a sample of the join must not run before its EXPLAIN: the object is larger than max_gb and its primary index is not on the join columns, so the join would redistribute it. None when nothing in the dictionary says so. Unproven uniqueness is a note, not a refusal (the check is opt-in). Pure.

_stem

_stem(w: str) -> str

_tokens

_tokens(text: str) -> set

rank_by_purpose

rank_by_purpose(rows: list, purpose: str, hits=None) -> list

Order rows by how well their documentation matches purpose. Pure.

score = stemmed word overlap with the object's description and its columns' descriptions, plus a bonus for each vector-search hit on the object (hits: a frame with OBJECT_NAME or an iterable of names). Rows with no overlap keep their order after the others; every row is kept.

_candidate_line

_candidate_line(r: dict) -> str

select_by_purpose

select_by_purpose(rows: list, purpose: str, llm_json, *, batch: int = _PURPOSE_BATCH) -> list

Keep the rows relevant to purpose, over ALL of them (batches of batch lines), by one model call per batch (llm_json: (system, user, schema=...) -> dict | None). Each retained row gains relevant_columns (the columns of the object the model found useful) and reason. A name the model returns that is not a candidate (or a column that is not in it) is dropped. A failed call keeps the batch unfiltered, said by purpose_unfiltered on the rows. Pure apart from llm_json.

apply_purpose

apply_purpose(rows: list, purpose: str, *, llm_json=None, hits=None) -> dict

{"rows", "mode", "note"}: rows narrowed to purpose. mode is 'model' (the model selected over every candidate, after a documentation pre-rank), 'ranked' (no instruct model: the pre-rank alone, every row kept) or 'none' (no purpose).

_run_group

_run_group(statements, errors: dict) -> dict

{statement: DataFrame} of SELECTs read in one grouped request (fail-open).

_frame_of

_frame_of(frames: dict, sql: str) -> pd.DataFrame

_read_physical

_read_physical(candidates: set, errors: dict) -> dict

Types, primary index, unique indexes, partitioning, size and skew of the tables among candidates.

collect_joinable_inputs

collect_joinable_inputs(*, data_domain: Optional[str] = None) -> dict

Read what the search needs, fail-open. Returns {"candidates", "datasets", "entities", "errors"}.

Candidates = the business dictionary's objects and column owners plus the attached objects, minus everything the feature store owns (process views, datasets, managers, FS_*, *_HIDDEN). Attached objects are kept, tagged attached. One grouped request reads the dictionary, the markers and the catalogs; a second layer reads the real columns of the candidates (DBC) and, for tables, their primary and unique indexes, partitioning and size. A documented object absent from DBC is missing.

_attached_sources_from

_attached_sources_from(df: DataFrame) -> dict

{(DB, OBJ): ['DB.SOURCE', ...]}: what each attached view read at its last pass (upper-cased).

_attached_from

_attached_from(df: DataFrame) -> dict

{(DB, OBJ): 'V' | 'T'} from the ATTACHED section rows (the marker is JSON).

resolve_dataset

resolve_dataset(dataset: str, inputs: dict) -> Optional[dict]

{"db", "name", "id", "entity": [cols], "types": {col: type}} of a registered dataset (case-insensitive, with or without its database), else None. Pure over inputs.

joinable_objects_model

joinable_objects_model(dataset=None, *, entity=None, data_domain: Optional[str] = None, include_subset: bool = True, purpose: Optional[str] = None, inputs: Optional[dict] = None, llm_json=None, hits=None) -> dict

The joinable objects of a dataset (its entity from the catalog) or of an entity (key columns).

Returns {"dataset", "entity", "rows", "errors", "purpose", "note"}; rows are the dicts of :func:match_joinable (full match first). With purpose, the rows are narrowed on their documentation (:func:apply_purpose; llm_json None = no model, ranked only). Reads nothing but the dictionary, the catalogs and DBC. Raises ValueError for an unknown dataset or when neither (or both) of dataset / entity is given.

_default_llm_json

_default_llm_json()

The model call of the public API: the plugin seam, only when an instruct model is configured.

find_joinable_objects

find_joinable_objects(dataset=None, *, entity=None, data_domain: Optional[str] = None, include_subset: bool = True, purpose: Optional[str] = None) -> pd.DataFrame

The tables and views, documented or attached to the feature store, that carry the entity of dataset (or the key columns entity) and so can be joined with it. One row per object, full matches first. Reads only; the scope is the business dictionary and the attached objects, never a scan of the databases. With purpose (e.g. 'fiber loss') the list is narrowed on the objects' documentation by the instruct model when one is configured, else only ranked (RELEVANT_COLUMNS and REASON are filled by the model). A name match is not a meaning match: COLUMN_MEANINGS shows what each join column is documented to mean.

uniqueness_sql

uniqueness_sql(obj: str, columns, *, example: bool = False) -> str

The statement that counts the rows and the distinct keys of obj on columns; with example, one key that repeats. Pure.

_ident

_ident(c) -> str

check_join_uniqueness

check_join_uniqueness(obj: str, columns, *, force: bool = False, conn=None, candidate: Optional[dict] = None) -> dict

Is obj unique on columns? The dictionary is read first (a unique primary or secondary index inside the columns proves it, nothing is read). Otherwise the count runs only after its EXPLAIN stays within the sample limits (SAMPLE_MAX_SECONDS / SAMPLE_MAX_SPOOL_GB); above them the answer is not_verified with the plan summary, unless force. Returns {"state": 'proven' | 'unique' | 'duplicates' | 'not_verified' | 'error', "unique": bool | None, "duplicates": int, "example": dict | None, "plan": str, "reasons": [...], "sql": str}. conn is a raw teradatasql connection (the app's cancellable job); None = the active teradataml context. candidate is a :func:collect_joinable_inputs candidate (saves the dictionary read).