Skip to content

tdfs4ds.utils.read_cache

tdfs4ds.utils.read_cache

A cache of object definitions (SHOW VIEW / SHOW TABLE) for one call (0.3.7.20).

upload_features builds the lineage of the view it registers several times (TimeManager detection, the check, the soft clone, the lineage record, the process-SQL capture), and each build reads every object's definition again: four SHOW statements per object, measured. Inside :func:ddl_cache a definition is read once.

Scope and safety:

  • The cache lives in a context variable, so other threads (the admin app) never see it.
  • Errors are cached too: a SHOW VIEW on a table fails every time, so the second attempt is answered from the cache as well.
  • Any statement that is not a read, sent through teradataml.execute_sql by the context holding the cache, clears it (a CREATE / REPLACE / DROP may change a definition already read; seen live: a soft clone created after a failed SHOW VIEW of its name). Since 0.3.7.25 a data write (INSERT / UPDATE / DELETE / MERGE, with or without a temporal qualifier, every statement of the request) clears only the cached rows (("rows", ...) keys): it cannot change an object's definition, and one upload_features writes a dozen catalog rows between its lineage builds (measured: 8.7 s of repeated SHOW). tdfs4ds also clears it explicitly after the soft clone and the optimizer's rewrite (:func:invalidate). A view changed by another session during the call is not seen until the call ends.

_CACHE module-attribute

_CACHE = contextvars.ContextVar('tdfs4ds_ddl_cache', default=None)

_READ_HEADS module-attribute

_READ_HEADS = ('SELECT', 'SEL ', 'SEL\n', 'SHOW', 'HELP', 'EXPLAIN', 'LOCKING', 'WITH', 'CURRENT VALIDTIME SEL', 'NONSEQUENCED VALIDTIME SEL', 'VALIDTIME AS OF', 'DYNAMIC EXPLAIN')

_SESSION_HEADS module-attribute

_SESSION_HEADS = ('SET QUERY_BAND',)

_DML_HEADS module-attribute

_DML_HEADS = ('INSERT ', 'INS ', 'UPDATE ', 'UPD ', 'DELETE ', 'DEL ', 'MERGE ', 'UPSERT ')

_QUALIFIER module-attribute

_QUALIFIER = re.compile("^(?:(?:CURRENT|NONSEQUENCED|SEQUENCED)\\s+VALIDTIME|VALIDTIME\\s+PERIOD\\s*'[^']*'|VALIDTIME\\s+AS\\s+OF\\s+(?:TIMESTAMP\\s*)?'[^']*')\\s+", re.IGNORECASE)

_OBJECT_DDL module-attribute

_OBJECT_DDL = re.compile('^(?:CREATE|REPLACE|DROP)\\b[^;]*?\\b(?:VIEW|TABLE)\\s+([^\\s(,;]+)', re.IGNORECASE)

_is_read

_is_read(statement) -> bool

_is_session_setting

_is_session_setting(statement) -> bool

A statement that changes the session only (SET QUERY_BAND around an EXPLAIN, 0.3.7.35): it cannot change a definition or a row, so it does not forget anything. The distribution check sets the band before each dynamic EXPLAIN, and each one used to empty the cache of a report.

_is_data_write

_is_data_write(statement) -> bool

True when every statement of the request is a data write (no definition can change).

_clear_rows

_clear_rows(cache) -> None

_norm

_norm(name) -> str

_ddl_objects

_ddl_objects(statement)

The objects a request creates, replaces or drops (CREATE / REPLACE / DROP VIEW|TABLE, mixed with data writes), or None when one statement is anything else (then everything is forgotten). 0.3.7.25: a REPLACE VIEW forgets that view's definition only, so the lineage rebuilt after it reads the changed view again and the others from the cache.

_forget_objects

_forget_objects(cache, names) -> None

Drop every cached row (an object created or dropped changes what DBC and the registries answer) and the cached definitions of an object with one of names' bare names (any database: forgetting too much is safe, forgetting too little is not).

_install_invalidation

_install_invalidation()

Clear the cache of the calling context on any statement that is not a read sent through teradataml.execute_sql (a CREATE / REPLACE / DROP may change a definition already read: seen live, a soft clone created after a failed SHOW VIEW of its name). Installed once; a context without a cache (another thread) passes straight through.

ddl_cache

ddl_cache()

Cache object definitions for the block. Nested blocks share the outer cache.

active

active() -> bool

show_key

show_key(sql) -> tuple

The cache key of a SHOW statement: Teradata object names are not case sensitive, and the same object was read under three spellings in one report (demo_user.v, DEMO_USER.V;, quoted), once each (0.3.7.35).

cached

cached(key, fn)

fn() once per key inside :func:ddl_cache; a direct call outside it. A ("show", sql) key is read as :func:show_key reads it (case, quotes and a closing ; do not make another key).

cached_rows

cached_rows(sql, run=None)

run(sql).fetchall() once per statement inside :func:ddl_cache (a ("rows", sql) key: any data write, CREATE or DROP forgets it); a direct read outside it. run defaults to teradataml.execute_sql looked up at call time (tests replace it).

invalidate

invalidate() -> None

Forget every cached definition (a view was just replaced).

suspended

suspended()

No cache inside the block (a run moves manager views, so definitions may change).

without_ddl_cache

without_ddl_cache(fn)

Run fn with the cache suspended (functools.wraps).

with_ddl_cache

with_ddl_cache(fn)

Run fn inside :func:ddl_cache (signature and source kept: functools.wraps).