Skip to main content

Module scripts

Module scripts 

Source
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:

  1. split_script cuts the text into statements with a small lexer that knows strings (raw, triple-quoted), comments, quoted names and nested BEGIN ... END, IF, LOOP, WHILE, FOR, REPEAT and CASE blocks. Semicolons inside any of those never split.
  2. 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.
  3. 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§

AnalysedStatement
What was decided about one statement. Carries no SQL text.
Procedure
A stored procedure with a SQL body.
ScriptAnalysis
The result of analyse_script.
ScriptNode
One statement or control structure of a script.
ScriptStatement
A statement of a script, in order, with where it sits.
ScriptWrite
A table a script writes and the real tables that feed it.
TableFunction
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.
NodeKind
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 IMMEDIATE followed.
MAX_PROCEDURE_DEPTH
Deepest chain of CALLs followed.

Functions§

analyse_script
Split text and follow what each statement reads and writes. Never fails.
analyse_script_with
analyse_script with procedures and table functions defined elsewhere in the project.
block_statements
The parsed statements a rewrite may change, in order, when text has blocks; None for 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 None when 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 text has BEGIN ... END, IF, a loop or a procedure around its statements.
insert_values_query
INSERT INTO t (a, b) VALUES (...), (...) as SELECT ... AS a, ... AS b UNION ALL ...; None without a column list or with DEFAULT.
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 text in 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; None when 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 text in 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 None for 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.