Expand description
BigQuery scripts: break them into statements and follow what flows between them.
Mirrors kumosql.scripts. A script is several statements run together, with variables,
temporary tables, control flow and stored procedures between them. Reading happens in steps:
split_scriptcuts the text into statements with a small lexer that knows strings (raw, triple-quoted), comments, quoted names and nestedBEGIN ... END,IF,LOOP,WHILE,FOR,REPEATandCASEblocks. Semicolons inside any of those never split.- Each statement is kept (it reads or writes tables), ignored (scalar
DECLARE/SET,ASSERT, transactions,LOAD DATA, definitions outside tables, control-flow shells) or unknown (it might read tables and could not be understood). Unknown is never guessed. - Kept statements are followed in order. A temporary table remembers what it was built from,
a script variable remembers the tables its value came from, a statement inside an
IF, loop or exception handler is a possible edge, and a temporary table one query defines is spliced into the final query as a CTE so column lineage reaches the real sources.
Nothing here echoes SQL text: ScriptAnalysis::report carries counts, kinds and line numbers.
Structs§
- Analysed
Statement - What was decided about one statement. Carries no SQL text.
- Procedure
- A stored procedure with a SQL body.
- Script
Analysis - The result of
analyse_script. - Script
Node - One statement or control structure of a script.
- Script
Statement - A statement of a script, in order, with where it sits.
- Script
Write - A table a script writes and the real tables that feed it.
- Table
Function CREATE TABLE FUNCTION name(params) AS query: kept so a call can be read as the query it stands for.- Tok
- A token of the script lexer (comments dropped, unterminated strings tolerated).
Enums§
- Disposition
- What was decided about a statement.
- Node
Kind - The kind of a script node.
- TokKind
- What a script token is.
Constants§
- MAX_
LOOP_ NESTING - Deepest nesting of loops followed.
- MAX_
LOOP_ PASSES - Trial runs of a loop body before its variables are called unsettled.
- MAX_
NESTED_ SQL_ DEPTH - Deepest nesting of
EXECUTE IMMEDIATEfollowed. - MAX_
PROCEDURE_ DEPTH - Deepest chain of
CALLs followed.
Functions§
- analyse_
script - Split
textand follow what each statement reads and writes. Never fails. - analyse_
script_ with analyse_scriptwith procedures and table functions defined elsewhere in the project.- block_
statements - The parsed statements a rewrite may change, in order, when
texthas blocks;Nonefor plain SQL. - collect_
procedures - Procedures defined in
texts, by lower-cased name and by bare name when unambiguous. - collect_
table_ functions - Table functions defined in
texts, by lower-cased name and by bare name when unambiguous. - column_
words - Every name the text could use as a column (lower-cased), or
Nonewhen it selects*. - expand_
script_ jobs - Job-history records with script jobs reduced to the real tables they read and wrote.
- final_
sql - The compact SQL of an analysis’s final query with its temporary tables spliced in.
- has_
blocks - Whether
texthasBEGIN ... END,IF, a loop or a procedure around its statements. - insert_
values_ query INSERT INTO t (a, b) VALUES (...), (...)asSELECT ... AS a, ... AS b UNION ALL ...;Nonewithout a column list or withDEFAULT.- is_
rewriteable - A statement whose query text a rewrite rule may change (not a declaration, assignment or call).
- is_
temporary_ reference - A table in a script’s hidden dataset (
_script...,_session, anonymous results). - leaf_
statements - The statement nodes of
textin order, with their offsets; blocks are opened. - lex
- Tokens of BigQuery SQL with comments dropped. Unterminated strings run to the end of their line.
- merge_
query - What flows into a MERGE target’s columns, as a query lineage can trace;
Nonewhen the MERGE is not understood. - script_
skeleton - What a rewrite must leave alone in a script: its tokens with each rewriteable statement
replaced by one
<statement>placeholder (words upper-cased, strings exact). - script_
tree - The statement tree of a script.
- split_
script - The statements of
textin order. Blocks and control flow are opened, not returned as statements. - split_
statements - Just the statement texts, in order.
- string_
value - The value of a string literal token, or
Nonefor bytes or an escape it does not decode. - token_
reads (tables read, tables written)found in the tokens of a statement that could not be parsed.