Skip to content

tdfs4ds.utils.learned_reads

tdfs4ds.utils.learned_reads

Learned reads: a page reads ahead what it read the last time (0.3.8.2).

A page of the admin app reads the same statements every time it is built: the same catalogs, the same SHOW VIEW of the same objects, one date per manager. At 0.3 to 0.5 s per round trip the wait is the NUMBER of statements. learned(key) records the reads a page issues; the next time the page is built it reads them all at the start in a few grouped requests (:mod:tdfs4ds.utils.grouped_read), and every reader that then sends one of those statements is answered from the rows. No reader changes: a statement that was not predicted exactly runs by itself, as before, so the page returns what it always returned (a changed store only costs the statements that changed).

What is replayed:

  • SELECT and SHOW VIEW / SHOW TABLE statements that SUCCEEDED the last time, alone in their request, and that do not depend on the session (no query band, no session table, no SELECT CURRENT_...). A statement that failed is not replayed (the reader sends it and sees its own error).
  • EXPLAIN SELECT * FROM v (the compile check of a view). Several EXPLAINs cannot share a request (measured live: Error 3706), but EXPLAIN SELECT * FROM v1; SELECT * FROM v2; ... is ONE request whose plan covers every statement, so its success proves every view compiles. It is bisected on an error, down to the view that fails alone, whose own EXPLAIN then runs and returns its own error (:func:compile_group, consulted by dependencies.explain_view only).

The record lives in memory and in ~/.tdfs4ds/cache/learned_reads (one JSON file per user and page, statement texts only, discarded when the package version changes). TDFS4DS_LEARNED_READS=off turns it off. Rows are read at the start of the page, seconds before the readers would have read them; a write sent through teradataml.execute_sql forgets them (:func:tdfs4ds.utils.grouped_read.forget).

_RECORDER module-attribute

_RECORDER = contextvars.ContextVar('tdfs4ds_read_recorder', default=None)

_COMPILE_OK module-attribute

_COMPILE_OK = contextvars.ContextVar('tdfs4ds_compile_ok', default=None)

_LEARNED module-attribute

_LEARNED = {}

_LOCK module-attribute

_LOCK = threading.Lock()

_MAX_RECORDED module-attribute

_MAX_RECORDED = 1500

_MAX_STATEMENT_CHARS module-attribute

_MAX_STATEMENT_CHARS = 20000

_SESSION_DEPENDENT module-attribute

_SESSION_DEPENDENT = re.compile('GETQUERYBAND|SESSIONINFO|\\bSESSION\\b|^SELECT\\s+(?:CURRENT_|USER\\b|DATABASE\\b|DATE\\b|TIME\\b)|\\bSAMPLE\\b|\\bRANDOM\\b', re.IGNORECASE)

_COMPILE_START module-attribute

_COMPILE_START = re.compile('^EXPLAIN\\s+SELECT\\s+\\*\\s+FROM\\s+(\\S+)$', re.IGNORECASE)

_ABSENT_ERROR module-attribute

_ABSENT_ERROR = re.compile("\\[Error 3807\\].*?Object '([^']+)' does not exist", re.IGNORECASE | re.DOTALL)

_WRONG_KIND_ERROR module-attribute

_WRONG_KIND_ERROR = re.compile('\\[Error (3854|3853)\\]', re.IGNORECASE)

_SHOW_OBJECT module-attribute

_SHOW_OBJECT = re.compile('^SHOW\\s+(?:VIEW|TABLE)\\s+(\\S+)$', re.IGNORECASE)

_MAX_FAILURES module-attribute

_MAX_FAILURES = 500

_enabled

_enabled() -> bool

_top_level_semicolon

_top_level_semicolon(sql) -> bool

replayable

replayable(sql) -> bool

True when sql can be read ahead: a single SELECT or SHOW that does not depend on the session.

recording

recording() -> bool

record

record(statement) -> None

Called by the execute_sql wrapper for a statement that succeeded (served from a prefetch or not).

record_failure

record_failure(statement, exc) -> None

Called by the execute_sql wrapper for a statement that raised. Two answers that stay true until an object is created or dropped are kept: "the object does not exist" (Error 3807) and "not a view" / "not a table" (3854 / 3853, a SHOW VIEW on a table: the code tries one then the other). The replay checks the dictionary once before it believes them (see :func:learned).

compile_ok

compile_ok(statement) -> bool

True when EXPLAIN SELECT * FROM v was shown to compile by a grouped check in this context.

compile_group

compile_group(views, *, execute=None) -> set

The statements EXPLAIN SELECT * FROM <view> that compile, in as few requests as possible (see the module docstring). Returns the normalised statements known to compile.

_scope

_scope(key) -> str

_path

_path(scope) -> str

_load

_load(scope)

_save

_save(scope, rec) -> None

_existence_sql

_existence_sql(failed)

One dictionary read that says which of the objects named by recorded failures exist now, and what.

_inject_absent

_inject_absent(failed, exist_sql) -> int

Answer a recorded failure by its error when the dictionary still says what made it fail: the object is not listed ("absent"), or it is listed and is not a view / not a table. Anything else (the object exists now, an unreadable dictionary) is left to its reader.

forget_learned

forget_learned(key=None) -> None

Drop the recorded reads of key (all of them when None), in memory (tests, a store switch).

learned

learned(key, *, execute=None, say=None)

Read ahead what the page key read last time, and record what it reads this time.

A nested learned block does nothing (the outer one records and replays). The replay never raises: when it fails, the readers run one by one. Yields {"replayed": n, "compiles_ok": n}.