Skip to content

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_grouped sends a list of SELECT statements as grouped requests and returns their rows.
  • :func:prefetch runs a list of statements grouped and answers the readers that issue exactly those statements later in the block (teradataml.execute_sql is already wrapped by :mod:tdfs4ds.utils.read_cache; the wrapper asks :func:lookup first). 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 with ValueError; no EXPLAIN / SHOW / HELP goes 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 most MAX_STATEMENTS statements.
  • 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).

MAX_REQUEST_BYTES module-attribute

MAX_REQUEST_BYTES = 300000

MAX_STATEMENTS module-attribute

MAX_STATEMENTS = 200

_PREFETCHED module-attribute

_PREFETCHED = contextvars.ContextVar('tdfs4ds_prefetched_rows', default=None)

_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

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

_FORGET_HOOKS module-attribute

_FORGET_HOOKS = []

PrefetchedCursor

What a reader gets for a prefetched statement: the part of a cursor the package uses.

description instance-attribute

description = description

rowcount instance-attribute

rowcount = len(rows)

__init__

__init__(description, rows)

fetchall

fetchall()

fetchone

fetchone()

fetchmany

fetchmany(size=1)

nextset

nextset()

close

close()

__iter__

__iter__()

normalise

normalise(sql) -> str

The key of a statement: whitespace collapsed, a closing ; removed.

is_read

is_read(sql) -> bool

True for a SELECT (with or without a temporal / locking qualifier). EXPLAIN, SHOW and HELP are not.

is_show

is_show(sql) -> bool

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

lookup(statement)

A :class:PrefetchedCursor for statement when it was prefetched in this context, else None.

on_forget

on_forget(fn) -> None

Call fn() whenever a write forgets the prefetched rows (a cache built on them forgets too).

covers

covers(statements) -> bool

True when every statement is already prefetched in this context.

forget

forget() -> None

Empty the prefetched rows (a write may have changed what they say).

_chunks

_chunks(statements)

Consecutive groups of statements, each below the byte and statement limits.

_fetch_sets

_fetch_sets(cur, n)

The n result sets of a multi-statement request: [(description, rows)].

run_grouped

run_grouped(statements, *, execute=None, errors=None)

{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(execute, group, out, errors=None)

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.

_alone

_alone(execute, key, errors=None)

frame

frame(result)

A pandas frame from one (description, rows) entry of :func:run_grouped.

prefetch

prefetch(statements, *, execute=None, absent=())

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.