tdfs4ds.utils.phases¶
tdfs4ds.utils.phases
¶
Phase of a statement: what a round trip to the database was for (0.3.8.0).
A tdfs4ds call (run, roll_out, upload_features, build_dataset...) sends
dozens of statements. This module names why each one is sent, so a cost can be put on
the framework (catalog reads, safety checks, follow-up rows, statistics...) apart from
the work itself (the ingestion CTAS, the MERGE).
Contract (for profile() / run_profile() and everything built on them)¶
Tag. Inside a call, every statement sent through teradataml.execute_sql or through
teradataml's SQLAlchemy engine (DataFrame SQL) starts with the comment::
/* TDFS4DS_PHASE=<name> */ <the statement>
<name> is the innermost with phase(name): block around the call site, else
other. The comment is added at the wire (below read_cache, count_statements and
every text check), so builders return the same strings, cache keys and EXPLAIN-prefix
checks see the bare SQL, and the statement counter classifies the bare SQL.
Measured on Vantage 20 (spike): DBQL QryLogV.QueryText keeps the leading comment;
EXPLAIN, SHOW, HELP, SET QUERY_BAND, MERGE, CREATE VOLATILE TABLE AS,
multi-statement requests and COLLECT STATISTICS accept it.
Never tagged: statements whose text the database stores (CREATE|REPLACE VIEW|MACRO|
PROCEDURE|FUNCTION|TRIGGER: SHOW VIEW would return the comment), driver escapes
({fn ...}), and anything outside a call (no phase, no call band): the user's own SQL is
never touched. Such statements read other in DBQL.
Switch. tdfs4ds.PHASE_TAGS (env TDFS4DS_PHASE_TAGS, default True). Off, or
tdfs4ds.QUERY_BAND False: the original bytes are sent.
Call band. utils.query_band.call_query_band(kind) sets, once per outermost public call,
the session band pairs TDFS4DS_CALL_ID=<uuid hex> and TDFS4DS_CALL_KIND=<kind>
(run | roll_out | upload | dataset). Read them back in SQL with
query_band._band_value('CALL_ID') (:func:call_id_sql_expr).
Reading the phase in SQL (DBQL): :func:phase_sql_expr is a Teradata expression over
QueryText that yields the name ('other' without a tag). :func:phase_of_sql is the
pure Python twin, :func:strip_tag removes the comment.
Phase names and groups¶
PHASES lists every name; WORK is what a user pays for (compute, merge, stats,
close_absent, dataset), SAFETY the checks that guard correctness (a cost to know, not to
switch off), FRAMEWORK everything else (other included). group_of(name) returns
'work' | 'safety' | 'framework'. Statements from a view's persisted DDL (REPLACE VIEW)
are not tagged, so a client-side count by phase can exceed the DBQL count of that phase by those.
SAFETY
module-attribute
¶
FRAMEWORK
module-attribute
¶
FRAMEWORK = ('catalog', 'options', 'clock', 'skip_check', 'filter', 'followup', 'entity_refresh', 'lineage', 'sql_capture', 'registration', 'documentation', 'optimize', 'query_band', 'history', OTHER)
PHASE_DESCRIPTIONS
module-attribute
¶
PHASE_DESCRIPTIONS = {'compute': {'label': 'Compute', 'kind': 'work', 'what': "Your source query, run once per execution into a staging table (CREATE VOLATILE TABLE AS over the process view), plus the UAF ART statements. Every run. This is the only phase counted as the user's workflow.", 'reduce': 'Optimise the process view (primary index, partitioning, FilterManager); this is the work itself.'}, 'merge': {'label': 'Merge', 'kind': 'work', 'what': 'The MERGE (or UPDATE+INSERT) of the staged rows into the FS_T_ feature tables. Every run that has rows.', 'reduce': 'Nothing to cut: fewer changed values means fewer rows written.'}, 'stats': {'label': 'Statistics', 'kind': 'work', 'what': 'COLLECT STATISTICS on the feature tables after the ingestion (apply_collect_stats). Every run that computed something.', 'reduce': 'Skipped when the run computes nothing. STATS_BASE_USING sets the sampling of base statistics.'}, 'close_absent': {'label': 'Close absent entities', 'kind': 'work', 'what': 'Ends the validity of entities the process no longer produces (volatile key table, sequenced DELETE). Only when close_absent_entities is on.', 'reduce': 'close_absent_entities=False if the source never drops entities.'}, 'dataset': {'label': 'Dataset build', 'kind': 'work', 'what': 'build_dataset / build_historical_dataset, and the dataset a run builds when dataset_view_name is given: the view, its catalog rows and optional documentation.', 'reduce': 'Build the dataset once, not per run (leave dataset_view_name unset on routine runs).'}, 'upstream': {'label': 'Upstream check', 'kind': 'safety', 'what': 'check_upstream=True: loads the lineage graph and the follow-up of the upstream processes to refuse a run whose input FAILED. Only when check_upstream is on.', 'reduce': 'check_upstream=False when the order is guaranteed elsewhere. A safety check: its cost is the price of the refusal.'}, 'history_guard': {'label': 'History guard', 'kind': 'safety', 'what': "roll_out only: before the first date, reads where each process's stored history starts so a replay is not begun after it (a history is built forward only). Once per roll_out call; skipped for valid_time_column processes.", 'reduce': 'Nothing to cut: it prevents a wrong history.'}, 'uniqueness': {'label': 'Staging checks', 'kind': 'safety', 'what': 'One statement on the staged table: rows staged, duplicate entity keys and NULL key columns (it also reads a few duplicated keys when it finds some). Every run.', 'reduce': 'Nothing to cut: it protects the primary index. A very large staging table makes it longer.'}, 'null_key': {'label': 'NULL-key check', 'kind': 'safety', 'what': 'The NULL entity-key check. Since 0.3.8.0 it is part of the one staging statement counted under uniqueness, so this label stays empty on current versions (old history only).', 'reduce': ''}, 'row_count': {'label': 'Row count', 'kind': 'safety', 'what': 'A separate COUNT of the staged rows, used only when the staging check did not report them. Normally empty.', 'reduce': ''}, 'truncation': {'label': 'VARCHAR truncation check', 'kind': 'safety', 'what': 'One aggregate on the staged rows: is a text value longer than the stored VARCHAR? Only for registered VARCHAR features whose source is longer than the stored type.', 'reduce': 'Widen the stored type with plan_type_change, or CAST in the view.'}, 'catalog': {'label': 'Catalog reads', 'kind': 'framework', 'what': 'Reads of the process, feature and dataset catalogs, the feature ids and types, and the metadata teradataml reads when it opens the process view as a DataFrame. Every run.', 'reduce': 'Fewer processes per roll_out call (roll_out reads catalog rows once per call).'}, 'options': {'label': 'Process settings', 'kind': 'framework', 'what': "Reads of the process options (FS_PROCESS_OPTIONS, FS_PROCESS_SETTINGS: close_absent_entities, track_entity_refresh, valid_time_column, force_varchar_length...) and the domain's process list. Once per process per call.", 'reduce': 'roll_out already prefetches them once per call; nothing else to set.'}, 'clock': {'label': 'TimeManager / clock', 'kind': 'framework', 'what': 'Resolving the valid time of the run (TimeManager date, domain clock, database clock), moving TimeManager cursors, soft clones, and the pairing written at registration.', 'reduce': 'One clock per data domain (set_domain_time_manager) and roll_out(data_domain=...).'}, 'skip_check': {'label': 'Skip check', 'kind': 'framework', 'what': 'Decides whether a (process, date) is already COMPLETED in FS_FOLLOW_UP (one COUNT, or one grid read per roll_out). A date already done costs only this.', 'reduce': 'Nothing to cut; roll_out reads the whole grid once per call.'}, 'filter': {'label': 'FilterManager', 'kind': 'framework', 'what': "FilterManager work: building the manager, moving it to each segment (update) and reading the segment's row. Once per segment. Only for processes with a FilterManager.", 'reduce': 'Fewer, larger segments (each segment is a full ingestion), or no FilterManager.'}, 'followup': {'label': 'Follow-up rows', 'kind': 'framework', 'what': 'The execution log: one row opened and one closed in FS_FOLLOW_UP per execution (or per segment). Every run.', 'reduce': 'Fewer executions: fewer segments or dates.'}, 'entity_refresh': {'label': 'Entity refresh', 'kind': 'framework', 'what': 'Records that each entity was confirmed in this run (FS_R_ table). Only when track_entity_refresh is on.', 'reduce': 'track_entity_refresh=False when the last-seen date per entity is not needed.'}, 'lineage': {'label': 'Lineage', 'kind': 'framework', 'what': 'Records the lineage edges of the process view (SHOW VIEW of each object in the chain) at registration. Registration only.', 'reduce': 'Register once, not per run (an unchanged re-registration skips the writes).'}, 'sql_capture': {'label': 'SQL versions', 'kind': 'framework', 'what': 'Captures the SQL beneath the process view (SHOW VIEW chain, hash, FS_PROCESS_SQL) to version it: at registration and once per run call.', 'reduce': 'PROCESS_SQL_VERSIONING=False turns it off (versions are then not kept).'}, 'registration': {'label': 'Registration', 'kind': 'framework', 'what': 'upload_features: checking the source view, the storage definition probe, the process catalog rows, entity and feature registration, storage tables. Registration only.', 'reduce': 'Register once, then run(); an unchanged re-registration is cheaper (SKIP_UNCHANGED_REGISTRATION).'}, 'documentation': {'label': 'Documentation', 'kind': 'framework', 'what': 'The documentation upload_features writes when document is on: an LLM pass over the process and its lineage chain (many SHOW VIEW, EXPLAIN and dictionary reads and writes), the dataset documentation and the vector index refresh. Registration only.', 'reduce': 'document=False (UPLOAD_DOCUMENT) and vector_indexing=False for fast registrations; document later with rebuild_documentation.'}, 'optimize': {'label': 'Query optimiser', 'kind': 'framework', 'what': 'The optional query optimiser at registration (EXPLAIN of candidate rewrites). Registration only, when optimize_query is on.', 'reduce': 'optimize_query=False (the default, UPLOAD_OPTIMIZE_QUERY).'}, 'query_band': {'label': 'Query band', 'kind': 'framework', 'what': 'The statements that read, set and restore the session query band (TDFS4DS_CALL_ID, execution tags): 3 per call plus the ones per execution, so statements can be joined to DBQL.', 'reduce': 'QUERY_BAND=False removes them (and the profile with them).'}, 'history': {'label': 'Cost history harvest', 'kind': 'framework', 'what': 'The copy of DBQL into FS_EXECUTION_RESOURCES and FS_CALL_PHASES, started at the top of run() and roll_out(), at most every 10 minutes per Python process. Few statements, but each reads DBQL (DBC.QryLogV, a large dictionary view): this is why it can dominate database seconds.', 'reduce': 'EXECUTION_RESOURCES=False and PHASE_TAGS=False stop the copies; run less often.'}, 'other': {'label': 'Other', 'kind': 'framework', 'what': 'Statements sent inside a call but outside any phase block, plus the statements never tagged (CREATE/REPLACE VIEW, such as a TimeManager or FilterManager cursor move). A big share here means untagged framework work.', 'reduce': ''}}
_PERSISTS_RE
module-attribute
¶
_PERSISTS_RE = re.compile('\\b(?:CREATE|REPLACE)\\s+(?:OR\\s+REPLACE\\s+)?(?:RECURSIVE\\s+)?(?:VIEW|MACRO|PROCEDURE|FUNCTION|TRIGGER|METHOD|TYPE)\\b', re.IGNORECASE)
_PHASE
module-attribute
¶
_FRAME
module-attribute
¶
EXPECTED_ERRORS
module-attribute
¶
EXPECTED_ERRORS = ((3807, '%DROP %', 'DROP of a temporary object that does not exist (idempotent clean-up)'), (3624, '%COLLECT STAT%', 'first COLLECT STATISTICS on a table with no statistics yet'), (3854, '%SHOW VIEW %', 'SHOW VIEW on a name that is a table (the kind probe)'), (3823, '%HELP INDEX %', 'HELP INDEX on a view (teradataml reflection)'), (3514, 'ABORT', 'user-generated ABORT (teradataml transaction handling)'))
role_of
¶
'workflow' (the user's SQL), 'work' (tdfs4ds' own work: MERGE, statistics...), 'safety' or
'framework'. Pure.
describe
¶
Description of a phase (a neutral entry for a name that is not in PHASE_DESCRIPTIONS). Pure.
default_phase
¶
phase(name) only when no phase block is open: a statement of a shared helper (settings, options)
takes the phase of its caller when it has one, else name instead of other (0.3.8.0).
tag_sql
¶
The statement with its phase comment, or sql itself when it must not be tagged.
name defaults to the current phase, else other when a call is in progress; with
neither, nothing is added. Idempotent.
strip_tag
¶
sql without a leading phase comment (anything else is returned unchanged).
phase_of_sql
¶
The phase named by a statement's leading comment (other when there is none).
phase_sql_expr
¶
Teradata expression: the phase of a statement, from its text (DBQL QueryText).
expected_error_sql_expr
¶
Teradata predicate: this DBQL row is a statement that fails by design (see EXPECTED_ERRORS).
is_expected_error
¶
Python twin of :func:expected_error_sql_expr (the tests compare them). Pure.
phase
¶
Name what the statements sent inside the block are for (nesting: the innermost wins).
Nothing is sent by this; the name travels in each statement (see the module docstring)
and is recorded by tdfs4ds.profile() with the block's own wall time.
recording
¶
Feed callback(name, self_wall_seconds, call_id) for each phase block that ends.
_before_cursor_execute
¶
_ensure_installed
¶
Install the two wire hooks once each (fail-open): the execute_sql wrapper and the
engine listener. Returns True when at least one is in place.