tdfs4ds.utils.grouped_read¶
tdfs4ds.utils.grouped_read
¶
Read several statements in ONE round trip (0.3.8.2).
A report issues dozens of small SELECTs; at 0.3 to 0.5 s per round trip the wait is the number of
statements, not their cost. Teradata runs a multi-statement request (S1; S2; S3) in one trip and
returns one result set per statement (cursor.nextset(), the pattern of server_side_batch).
Two pieces:
- :func:
run_groupedsends a list of SELECT statements as grouped requests and returns their rows. - :func:
prefetchruns a list of statements grouped and answers the readers that issue exactly those statements later in the block (teradataml.execute_sqlis already wrapped by :mod:tdfs4ds.utils.read_cache; the wrapper asks :func:lookupfirst). No reader changes: the SQL a reader sends is what is prefetched, so a statement that was not predicted exactly just runs by itself, as before. Rows are the driver's own, so a reader gets the same values either way.
Safeguards:
- Only reads. A statement that is not a SELECT (
SELECT/SEL/ temporal qualifier + SELECT /LOCKING/WITH) is refused withValueError; noEXPLAIN/SHOW/HELPgoes in a group (not tested live in a group). - Size. A request is cut before about 300 KB (
MAX_REQUEST_BYTES), well under Teradata's 1 MB request limit (Error 5639), and holds at mostMAX_STATEMENTSstatements. - Fallback. One failing statement aborts its whole request. The group is then split in halves, recursively; a statement that fails alone is simply not prefetched (its reader runs it again and sees its own error, with its own handling).
- Cleared by any write. A statement that is not a read, sent through
teradataml.execute_sql, empties the prefetched rows (the wrapper in :mod:read_cache).
_PREFETCHED
module-attribute
¶
_READ_START
module-attribute
¶
_READ_START = re.compile("^(?:(?:CURRENT\\s+VALIDTIME|NONSEQUENCED\\s+VALIDTIME|SEQUENCED\\s+VALIDTIME|VALIDTIME\\s+AS\\s+OF\\s+(?:TIMESTAMP\\s*)?'[^']*'|VALIDTIME\\s+PERIOD\\s*'[^']*'|LOCKING\\s+[^;]*?\\s+FOR\\s+ACCESS)\\s+)*(?:SELECT|SEL|WITH)\\b", re.IGNORECASE | re.DOTALL)
_SHOW_START
module-attribute
¶
PrefetchedCursor
¶
What a reader gets for a prefetched statement: the part of a cursor the package uses.
normalise
¶
The key of a statement: whitespace collapsed, a closing ; removed.
is_read
¶
True for a SELECT (with or without a temporal / locking qualifier). EXPLAIN, SHOW and HELP are not.
is_show
¶
SHOW VIEW x / SHOW TABLE x. Measured live (Vantage 20): several SHOW statements go in one
request and each comes back as its own result set (0.3.8.2); EXPLAIN does not (see :func:compile_group).
lookup
¶
A :class:PrefetchedCursor for statement when it was prefetched in this context, else None.
on_forget
¶
Call fn() whenever a write forgets the prefetched rows (a cache built on them forgets too).
covers
¶
True when every statement is already prefetched in this context.
_chunks
¶
Consecutive groups of statements, each below the byte and statement limits.
_fetch_sets
¶
The n result sets of a multi-statement request: [(description, rows)].
run_grouped
¶
{normalised statement: (description, rows)} for the statements that could be read.
Statements are sent in as few requests as the limits allow. When a request fails, its statements
run one by one and the ones that fail alone are left out (their exception goes to errors, a dict,
when given). execute defaults to teradataml.execute_sql looked up at call time (tests
replace it).
_read_group
¶
Read group in one request; when it fails, split it in two and read each half, down to the
single statement that fails alone (left out). A group with k bad statements costs about
2 k log2(n) requests, where reading them one by one costs n.
prefetch
¶
Run statements grouped now; inside the block a reader that sends one of them is answered from the result (see the module docstring). Nested blocks add to the same store, and the rows stay available until the outermost block ends (or a write forgets them). Never raises for a read that failed: that statement is simply not prefetched. Yields the number of statements prefetched.
absent: statements known to fail because the object they read is not in the data dictionary (the caller checked). They are not sent; a reader that issues one gets the error at once, as it would have from the database, without the round trip.