Skip to content

tdfs4ds.utils.view_text

tdfs4ds.utils.view_text

The parts of a view definition: header, LOCKING modifier, query body.

One reader for CREATE|REPLACE [RECURSIVE] VIEW <name> [(<columns>)] AS [LOCKING ...] <query> text, used wherever a view's query is needed: the EXPLAIN of a process, the dataset sample, the query optimizer, its Approve step and sql_invariants.keep_locking. Before it there were three regular expressions, each wrong on a different shape (a AS inside a quoted name, a (a) UNION (b) body losing its first and last parenthesis, a doubled header on Approve).

Everything goes through :func:scan_sql, a small lexer that knows Teradata string literals ('' is the only escape: a backslash is an ordinary character), quoted identifiers and comments, so no rule ever looks inside a literal or a comment.

Pure, stdlib only, no database.

__all__ module-attribute

__all__ = ['Token', 'scan_sql', 'code_tokens', 'matching_close', 'ViewText', 'view_body', 'rebuild_view', 'explainable', 'approved_view_ddl']

_IDENT_RE module-attribute

_IDENT_RE = re.compile('(?:[^\\W\\d]|[$#])[\\w$#]*', re.UNICODE)

_NUM_RE module-attribute

_NUM_RE = re.compile('\\d+(?:\\.\\d*)?(?:[eE][+-]?\\d+)?|\\.\\d+(?:[eE][+-]?\\d+)?')

_WS_RE module-attribute

_WS_RE = re.compile('\\s+')

_LOCK_KINDS module-attribute

_LOCK_KINDS = {'ROW', 'TABLE', 'VIEW', 'DATABASE'}

_LOCK_MODES module-attribute

_LOCK_MODES = {'ACCESS', 'READ', 'WRITE', 'EXCLUSIVE', 'EXCL', 'SHARE', 'CHECKSUM', 'LOAD'}

Token dataclass

One lexical unit. kind is one of ws, line_comment, block_comment, str, qident, bq (a back-quoted name, not Teradata), ident, num, punct. depth is the parenthesis depth the token sits at (a ( and its ) share the outer depth).

kind instance-attribute

kind: str

value instance-attribute

value: str

start instance-attribute

start: int

end instance-attribute

end: int

depth class-attribute instance-attribute

depth: int = 0

upper property

upper: str

is_code property

is_code: bool

__init__

__init__(kind: str, value: str, start: int, end: int, depth: int = 0) -> None

__repr__

__repr__() -> str

ViewText dataclass

A view definition split in its parts.

  • header - CREATE|REPLACE [RECURSIVE] VIEW <name> [(cols)] AS as written (leading comments included), None when the text is a query.
  • name - the view name as written (quotes kept), None without header.
  • columns - the column list of the header as written, None when absent.
  • locking - the leading LOCKING ... FOR ACCESS / LOCK ROW FOR ACCESS modifier(s), verbatim, '' when none.
  • locks - the same modifiers one by one.
  • body - the query without header, locking, wrapping parentheses and trailing ;. CURRENT VALIDTIME and similar prefixes stay.

header instance-attribute

header: Optional[str]

name instance-attribute

name: Optional[str]

columns instance-attribute

columns: Optional[tuple]

locking instance-attribute

locking: str

body instance-attribute

body: str

locks class-attribute instance-attribute

locks: tuple = field(default=())

__init__

__init__(header: Optional[str], name: Optional[str], columns: Optional[tuple], locking: str, body: str, locks: tuple = ()) -> None

query

query() -> str

The runnable query: locking (when any) then the body.

_quoted_end

_quoted_end(sql: str, i: int, q: str) -> int

End (exclusive) of the quoted run opening at i; a doubled quote is an escaped one. An unterminated run ends at the end of the text.

scan_sql

scan_sql(sql: str) -> list

Every token of sql, whitespace and comments included, in order.

Concatenating t.value of all tokens gives back sql exactly.

code_tokens

code_tokens(sql_or_tokens) -> list

The tokens that are neither whitespace nor comments.

matching_close

matching_close(toks: list, open_idx: int) -> int

Index in toks of the ) closing the ( at open_idx, or -1.

_parse_lock

_parse_lock(t: list, k: int, end: int) -> int

Index after a LOCKING|LOCK ... modifier starting at k, or -1.

_parse_header

_parse_header(sql: str, t: list)

(index of the first body token, header text, name, columns) or None.

view_body

view_body(ddl: str) -> ViewText

Split ddl (a view definition or a plain query) into its parts.

Never raises: unreadable text gives header=None and the stripped text as body.

explainable

explainable(sql: str) -> str

The text to put after EXPLAIN: for a view definition its query (locking kept), for anything else sql unchanged.

rebuild_view

rebuild_view(view_name_sql: str, vt_or_body: Union[ViewText, str], locking: str = '') -> str

REPLACE VIEW <view_name_sql> [(cols)] AS + locking + body.

vt_or_body is a :class:ViewText (its columns and locking are used; locking overrides its locking when given) or a body text, which is read with :func:view_body so a text that still carries a header never gives a doubled header.

approved_view_ddl

approved_view_ddl(view_name_sql: str, best_sql: str, original_sql: str = '') -> str

The REPLACE VIEW statement that installs best_sql as view_name_sql.

best_sql may be a body or a whole definition. The LOCKING modifier(s) and the column list of original_sql (the view's current definition) are kept: locking through sql_invariants.keep_locking.