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