Skip to content

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.

TAG_PREFIX module-attribute

TAG_PREFIX = '/* TDFS4DS_PHASE='

TAG_SUFFIX module-attribute

TAG_SUFFIX = ' */ '

OTHER module-attribute

OTHER = 'other'

WORK module-attribute

WORK = ('compute', 'merge', 'stats', 'close_absent', 'dataset')

SAFETY module-attribute

SAFETY = ('upstream', 'history_guard', 'uniqueness', 'null_key', 'row_count', 'truncation')

FRAMEWORK module-attribute

FRAMEWORK = ('catalog', 'options', 'clock', 'skip_check', 'filter', 'followup', 'entity_refresh', 'lineage', 'sql_capture', 'registration', 'documentation', 'optimize', 'query_band', 'history', OTHER)

PHASES module-attribute

PHASES = WORK + SAFETY + FRAMEWORK

_NAME_RE module-attribute

_NAME_RE = re.compile('[a-z][a-z0-9_]{0,39}$')

_TAG_RE module-attribute

_TAG_RE = re.compile('^\\s*/\\* TDFS4DS_PHASE=([a-z][a-z0-9_]*) \\*/ ?')

_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

_PHASE: ContextVar = contextvars.ContextVar('tdfs4ds_phase', default=None)

_CALL module-attribute

_CALL: ContextVar = contextvars.ContextVar('tdfs4ds_call', default=None)

_FRAME module-attribute

_FRAME: ContextVar = contextvars.ContextVar('tdfs4ds_phase_frame', default=None)

_RECORDERS module-attribute

_RECORDERS: list = []

_INSTALL_LOCK module-attribute

_INSTALL_LOCK = threading.Lock()

_INSTALLED module-attribute

_INSTALLED = {'v': False, 'execute': False, 'engine': False}

group_of

group_of(name) -> str

clean_name

clean_name(name) -> str

default_phase

default_phase(name)

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

current_phase

current_phase()

The innermost phase name, or None outside any phase block.

current_call

current_call()

(call_id, kind) of the outermost call in progress, or None.

current_call_id

current_call_id()

enabled

enabled() -> bool

tag_sql

tag_sql(sql, name=None)

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

strip_tag(sql)

sql without a leading phase comment (anything else is returned unchanged).

phase_of_sql

phase_of_sql(text) -> str

The phase named by a statement's leading comment (other when there is none).

phase_sql_expr

phase_sql_expr(column='QueryText') -> str

Teradata expression: the phase of a statement, from its text (DBQL QueryText).

call_id_sql_expr

call_id_sql_expr(column='QueryBand', width=64) -> str

phase

phase(name)

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

recording(callback)

Feed callback(name, self_wall_seconds, call_id) for each phase block that ends.

new_call_id

new_call_id() -> str

_before_cursor_execute

_before_cursor_execute(conn, cursor, statement, parameters, context, executemany)

_ensure_installed

_ensure_installed() -> bool

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.