scope

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachTempColumnLookup

func AttachTempColumnLookup(tableRef *TableRef, declared []string)

AttachTempColumnLookup lets the scope resolver treat a CTE or derived table as a relation whose columns are known, so an unqualified name it owns resolves through its own lineage instead of being guessed from name order. declared is the CTE's column list when the query wrote one, or the names the query's output exposes; a nil list falls back to the targets the lineage carries.

func ExposedColumnNames

func ExposedColumnNames(declared []string, cols []OutputColumn) []string

ExposedColumnNames lists the names a temporary relation exposes to whatever reads it, in the order it offers them. It is OutputColumnNames — a declared list renames the body's outputs from the left, the outputs it does not reach keep their own names — with one exception: a declared list longer than the modelled body is kept when that body carries a wildcard output column, because the count is then a lower bound rather than the body's arity. `WITH c (a, b) AS (SELECT * FROM undescribed) SELECT * FROM c` is engine-valid whenever `undescribed` really has two columns — all five engines were asked, and each exposes `(a, b)` for an equal-arity declaration — while discarding it left the relation undescribed, so the two edges the names carry became one wildcard edge, an edge a declared target list expected went missing, and a name the relation does not have resolved silently. The statement's own list is the only account of those columns there is.

The result may be longer than cols, so a caller indexes outputs with it only up to len(cols) and uses it in full for the lookup a relation's references resolve against.

func NewLineageEdge

func NewLineageEdge(fromQualifier, fromTable, fromField, toQualifier, toTable, toField string, transform []model.Transformation, isTemp bool) model.ColumnRelation

NewLineageEdge creates a ColumnRelation from field-edge parameters, recording the qualifier of each endpoint as a database name. That is what a MySQL-family or StarRocks reference names; PostgreSQL names a schema and uses NewSchemaLineageEdge.

func NewSchemaLineageEdge

func NewSchemaLineageEdge(fromQualifier, fromTable, fromField, toQualifier, toTable, toField string, transform []model.Transformation, isTemp bool) model.ColumnRelation

NewSchemaLineageEdge is NewLineageEdge with the qualifier recorded as a schema name. The distinction is not cosmetic: database and schema are separate fields of an object identifier, so an edge built with the wrong one names a different object than the analyzer resolved.

func OutputColumnNames added in v0.1.2

func OutputColumnNames(declared []string, cols []OutputColumn) []string

OutputColumnNames lists the names the analyzer's modelled outputs carry, in the order it modelled them. A declared list — a CTE's `WITH c (a, b)` or a derived table's `AS d (a, b)` — renames them from the left, so the relation's own lineage edges are named the same way. The outputs the list does not reach keep their own names, because that is what the engines do: PostgreSQL 16 and Doris 4.1.4 accept a list shorter than the body and expose `(a, name)` for `WITH c (a) AS (SELECT k, name FROM t) SELECT …`, while MySQL 8.3, TiDB, MariaDB, StarRocks 4.1 and MSSQL 2019 accept a declared list only when its arity matches the body, which makes this rule and an equality rule agree on every statement they execute. A list longer than the body is a statement every one of them rejects, so the declaration is discarded and the body names the columns.

The rule is about the body's outputs, never about the names its lineage happens to carry: an output column with no source — a literal, an aggregate — is a column of the relation all the same, and leaving it out let a scope holding another relation deny the name and shifted a declared target list behind a wildcard over the relation.

func TempColumnNames

func TempColumnNames(declared []string, lineage []model.ColumnRelation) []string

TempColumnNames reports the columns a temporary relation exposes, or nil when one of them cannot be named. A wildcard the catalog did not expand, and an output without a name, both leave the list incomplete; an incomplete list is reported as unknown so the resolver keeps its fallback for the relation.

Types

type CTEDefinition

type CTEDefinition struct {
	Name    string
	Columns []string
	// Lineage edges within the CTE
	Lineage []model.ColumnRelation
}

CTEDefinition represents a Common Table Expression.

type ColumnLookup

type ColumnLookup func() []string

ColumnLookup reports the column names a relation exposes. A nil result means the metadata is unavailable, and the resolver keeps its deterministic ordering rule for that relation.

type ColumnRef

type ColumnRef struct {
	Schema string
	Table  string
	Column string
	// Resolved marks a reference that was already resolved against the scope it
	// originated in, so a later lookup does not bind it to a different relation
	// that happens to share the name. Every analyzer sets it where the resolved
	// table lives in a scope a later lookup cannot see: a set-operation arm, a
	// flattened expression subquery, an expanded wildcard.
	Resolved bool
}

ColumnRef represents a reference to a column in the query.

func QueryLocalColumnRef added in v0.1.2

func QueryLocalColumnRef(tableRef *TableRef, column string) ColumnRef

QueryLocalColumnRef is the source one column of a query-local relation contributes. The relation's name is an alias the statement invented, so the reference stays unresolved for the scope resolver to bind by the relation's scope key — which is what attaches the relation and lets the temporary-relation tracing flatten it. Marking it resolved instead made COUNT(*) over a CTE emit an edge naming the CTE, which is not a stored object at all.

func Refs

func Refs(sources []ColumnSource) []ColumnRef

Refs returns the source references without their transformations, for the callers that only need to resolve or count them.

func WildcardSourceRef

func WildcardSourceRef(tableRef *TableRef) ColumnRef

WildcardSourceRef is the source a `*` contributes for one relation: a wildcard reference to that relation, marked resolved because the relation it names cannot be rebound by name later. StarRocks keeps its own rule for a query-local relation, where the reference has to stay rebindable; see its queryLocalWildcardSourceRef.

type ColumnSource

type ColumnSource struct {
	Ref       ColumnRef
	Transform []model.Transformation
}

ColumnSource is a source column of an output column together with the transformations that produced the output from it. The transformation travels with the source rather than with the output column because a set operation merges arms that compute the same output column in different ways: one transformation for the whole column would attribute the first arm's expression to every other arm's sources, which is lineage that does not exist.

func NewColumnSources

func NewColumnSources(refs []ColumnRef, transform []model.Transformation) []ColumnSource

NewColumnSources pairs every reference with the same transformation, the shape an output column built from a single expression has.

type OutputColumn

type OutputColumn struct {
	Alias string // The alias given to this column (AS clause)
	// Sources are the columns this output depends on, each carrying the
	// transformation that produced it.
	Sources []ColumnSource
	// Whether this is a derived/transformed column
	IsDerived bool
}

OutputColumn represents a column in the SELECT output.

func (*OutputColumn) SetTransform

func (c *OutputColumn) SetTransform(transform []model.Transformation)

SetTransform gives every source the same transformation list, the shape an output column built from a single expression has. The list is shared rather than copied, so callers must treat it as read-only afterwards.

type RelationKey

type RelationKey struct {
	Qualifier string
	Name      string
}

RelationKey identifies a relation inside a scope. Qualifier is the database (MySQL family, StarRocks) or schema (PostgreSQL) the relation was named with, and is empty when the query does not name one. Name is the alias when the relation has one, otherwise its table name: the name a SQL reference uses.

func RelationKeyOf

func RelationKeyOf(id model.ObjectIdentifier) RelationKey

RelationKeyOf builds the key of a relation endpoint recorded on an edge. The qualifier is whichever of the identifier's database or schema is set: the MySQL family and StarRocks record a SQL qualifier as a database, PostgreSQL as a schema.

type ResolvedColumn

type ResolvedColumn struct {
	Ref      ColumnRef
	Relation *TableRef
}

ResolvedColumn is a column reference together with the relation that owns it. An unqualified name yields one per relation in scope that exposes it, which is more than one when several relations share the name — the shape a coalesced USING or NATURAL JOIN column has, where every owner really is a source.

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope represents a lexical scope in SQL, tracking available tables and columns. Scopes are nested (e.g., subquery inside a main query).

func NewScope

func NewScope(parent *Scope) *Scope

NewScope creates a new scope with an optional parent.

func NewScopeWithDefinitions

func NewScopeWithDefinitions(parent, definitions *Scope) *Scope

NewScopeWithDefinitions creates a scope that resolves the CTE definitions of definitions while resolving the relations of parent. The two namespaces are usually reached through the same link, which is why NewScope is the ordinary constructor; a clause that may read a query's CTEs but not the relations that query reads from needs them separated.

func (*Scope) AddCTE

func (s *Scope) AddCTE(cte *CTEDefinition)

AddCTE adds a CTE definition to the current scope.

func (*Scope) AddOutputColumn

func (s *Scope) AddOutputColumn(col OutputColumn)

AddOutputColumn adds an output column to the current scope.

func (*Scope) AddTable

func (s *Scope) AddTable(ref *TableRef)

AddTable adds a table reference to the current scope. Registering the same address twice replaces the earlier reference, which keeps a repeated registration idempotent; two same-named relations from different databases or schemas have different keys and coexist.

func (*Scope) FindCTE

func (s *Scope) FindCTE(name string) (*CTEDefinition, bool)

FindCTE looks up a CTE definition a FROM clause names: this scope's own definitions, the definitions this scope reads, and then their parent chain. A CTE is named from a FROM clause and nowhere else, which is why the query resolution path uses findCTEQualifier instead.

func (*Scope) FindRelation

func (s *Scope) FindRelation(key RelationKey) (*TableRef, bool)

FindRelation returns the relation a reference with this key addresses, searching parent scopes when the current one has none. An exactly matching key wins; otherwise a relation that accepts the reference (see TableRef.addresses) is returned.

func (*Scope) FindTable

func (s *Scope) FindTable(name string) (*TableRef, bool)

FindTable returns the relation addressed by name alone, which is how a reference that names no qualifier addresses it.

func (*Scope) GetOutputColumns

func (s *Scope) GetOutputColumns() []OutputColumn

GetOutputColumns returns the output columns of this scope.

func (*Scope) Parent

func (s *Scope) Parent() *Scope

Parent returns the parent scope.

func (*Scope) ResolveColumn

func (s *Scope) ResolveColumn(colRef ColumnRef) (*ColumnRef, error)

ResolveColumn resolves a column reference to its source table. It handles both qualified (table.column) and unqualified (column) references.

func (*Scope) ResolveColumnRef

func (s *Scope) ResolveColumnRef(colRef ColumnRef) (*ColumnRef, *TableRef, error)

ResolveColumnRef is ResolveColumn plus the relation the reference resolved to, so a caller can tell a base table from a query-local one without looking the name up again and guessing. It reports the first of ResolveColumnRefs' resolutions, which is the only one for every reference except an unqualified name that several relations in scope own. A CTE that is in scope only as a definition is presented as the relation it describes. The relation is nil when this scope cannot identify it, which happens for a reference that was already resolved against a scope this one does not share.

func (*Scope) ResolveColumnRefs

func (s *Scope) ResolveColumnRefs(colRef ColumnRef) ([]ResolvedColumn, error)

ResolveColumnRefs resolves a column reference to every relation in scope that owns it, in the scope's name order. A qualified reference and one that was already resolved elsewhere have exactly one resolution; an unqualified name has one per owning relation, which is how a coalesced USING or NATURAL JOIN column reports both sides.

func (*Scope) SetFoldNames added in v0.1.2

func (s *Scope) SetFoldNames(fold bool)

SetFoldNames declares that this scope's engine binds identifiers case-insensitively, so a reference that differs only in case from the name a relation or a CTE was written with resolves to it. It is set on a root scope, and every child scope created afterwards inherits it; the default is the byte-for-byte comparison every other dialect keeps.

The dialect that sets it is the one whose engine is case-insensitive: a T-SQL identifier is bound by the database's collation, which is case-insensitive by default, so `SELECT T.a FROM t` is valid and has lineage. A case-sensitive engine must not set it — MySQL on a case-sensitive filesystem and a PostgreSQL quoted alias both name different objects with `T` and `t` — and a missing reference is reported as a gap by the caller rather than silently bound to the wrong relation.

func (*Scope) SetOutputColumns

func (s *Scope) SetOutputColumns(cols []OutputColumn)

SetOutputColumns replaces every output column of this scope. A merge that produces the whole list uses it so a shorter list cannot silently leave a column behind.

func (*Scope) SetVerifyQualifiedColumns added in v0.1.2

func (s *Scope) SetVerifyQualifiedColumns(verify bool)

SetVerifyQualifiedColumns declares that this scope's engine resolves a qualified reference against the catalog as well as against the relation it names: a reference whose qualifier addresses a relation the catalog describes and which does not own the column resolves to nothing, the way `column not found` already answers an unqualified name no relation in scope owns (resolveInScope). It is set on a root scope, and every child scope created afterwards inherits it.

The dialect that sets it is the one whose engine answers that question from the catalog: Doris reports `Unknown column 'nope' in 'ev2'` for `SELECT ev2.nope FROM ev2`. A dialect that leaves it off keeps the reading it always had — the qualifier is bound to a relation and the column is taken on faith — which is what a dialect has to do when the relations it registers are not described by the catalog.

Only what the catalog describes is asked, and a relation it cannot describe keeps the lenient reading even when this is on: ColumnNames reports nil for one whose lookup is absent, and a relation whose lookup answers with no names at all is unknown rather than empty. A *query-local* relation — a CTE, a derived table, a table function — is not asked either, even when it carries a lookup, because that list is the analyzer's own account of what the query exposes (the names its lineage happens to carry, the placeholder a generator is named after) rather than what the engine accepts. A reference already resolved against the scope it came from is the other scope's to answer. A column name is compared the way resolveInScope compares it, case-insensitively, because that is the rule a column name is bound by wherever this scope matches one.

func (*Scope) Tables

func (s *Scope) Tables() []*TableRef

Tables returns the scope's relations in registration order, without the parent scope's. Every relation is returned, so a wildcard expands against both db1.t and db2.t.

type TableRef

type TableRef struct {
	Schema string
	Table  string
	Alias  string
	// For subqueries and CTEs
	IsSubquery bool
	IsCTE      bool
	// Lineage edges for subqueries (how output columns relate to source tables)
	Lineage []model.ColumnRelation
	// contains filtered or unexported fields
}

TableRef represents a table or table-like source (subquery, CTE).

func (*TableRef) ColumnNames

func (r *TableRef) ColumnNames() []string

ColumnNames returns the relation's column names, or nil when they are unknown.

func (*TableRef) Key

func (r *TableRef) Key() RelationKey

Key returns the key a SQL reference addresses the relation by.

func (*TableRef) SetColumnLookup

func (r *TableRef) SetColumnLookup(lookup ColumnLookup)

SetColumnLookup attaches the catalog column lookup for this relation. Analyzers set it when they register a base table, so every scope — including the CTE, derived-table and subquery scopes the analyzers resolve in — disambiguates an unqualified column the same way.

Jump to

Keyboard shortcuts

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