sqltables

package
v1.169.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqltables extracts the physical tables a SQL statement reads.

It is one extractor, not one per caller: semantic enrichment names the tables it must fetch context for, and the call catalog names the datasets a recorded query addressed (#1321). Both are answering the same question, and a second implementation of it would mean an enriched table and a cataloged target disagreeing about what a query touched.

The extraction is lexical (regular expressions over FROM and JOIN clauses, plus Trino's Elasticsearch raw_query table function), not a parser: it names what a statement plainly reads, drops common table expressions, and makes no claim about a statement it cannot parse.

Index

Constants

View Source
const (
	// ShapeStar is SELECT * FROM t: its result names every column of t.
	ShapeStar = "star"
	// ShapeCount is SELECT count(*) FROM t: it names no column and its result
	// names none either.
	ShapeCount = "count"
)

Column-free statement shapes (#2810): a statement that names no column of the table it reads, so there is no column name in it to guess.

View Source
const KindOther = "other"

KindOther is the statement kind of anything StatementKind does not recognize, including an empty statement.

Variables

This section is empty.

Functions

func ColumnFree added in v1.169.0

func ColumnFree(sql string) (string, bool)

ColumnFree reports whether a statement reads one table without naming any of its columns, and which shape it is: ShapeStar for SELECT * FROM t, ShapeCount for SELECT count(*) FROM t, each with at most an alias and LIMIT, OFFSET or FETCH. Any other statement, including either shape with a WHERE, a join, a grouping or an ordering, is not column-free.

func HasTopLevelOrderBy added in v1.142.0

func HasTopLevelOrderBy(sql string) bool

HasTopLevelOrderBy reports whether a statement orders its own result: an ORDER BY outside every parenthesis, so not a subquery's, a CTE's or a window function's, none of which fix the order of the rows the statement returns. String literals, quoted identifiers and comments are skipped. A statement without one returns its rows in whatever order the engine produced them, which is what makes a limited subset of it arbitrary (#2057).

func IsRead added in v1.166.3

func IsRead(sql string) bool

IsRead reports whether sql is a statement that only reads. It is an allow-list: a statement whose kind is not a read kind is not a read, whatever it is, so a statement kind added to the engine later is refused by a read-only gate until it is listed here. EXPLAIN is a read when what it explains is not run: EXPLAIN ANALYZE runs it, so EXPLAIN ANALYZE, and an EXPLAIN whose explained statement is EXPLAIN ANALYZE or ANALYZE, is not.

func StatementKind added in v1.141.0

func StatementKind(sql string) string

StatementKind is the lowercased leading keyword of a SQL statement, after any leading comments and opening parentheses, when it is one of the keywords this package knows; KindOther otherwise. It reads no further than that word, so it never carries a literal, an identifier or the text.

func Summary added in v1.141.0

func Summary(sql string) string

Summary is a low-cardinality description of a statement without its literals, in the shape the OpenTelemetry database conventions give db.query.summary: the statement's keyword in upper case followed by the tables it reads, at most three of them ("SELECT hive.sales.orders"). A statement this package does not recognize summarizes as its kind alone.

Types

type Ref

type Ref struct {
	Catalog  string
	Schema   string
	Table    string
	FullPath string
	Source   string // "FROM", "JOIN", "TABLE_FUNCTION"
}

Ref is one table a statement reads.

func Extract

func Extract(sql string) []Ref

Extract returns every physical table a statement reads. Uses regex for Trino-specific functions and standard table patterns. Combines ES raw_query indices with regular table references (e.g., JOINs). Filters out CTE references to only return physical tables.

func WriteTarget added in v1.146.0

func WriteTarget(sql string) (Ref, bool)

WriteTarget returns the table a statement writes from what it reads, and false for any statement that is not such a write. The lineage of a statement is an edge from each table Extract names to this one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL