core

package
v0.0.0-...-e6c4605 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package core defines the shared language of the LeapSQL system.

This package contains:

  • Domain entities (Model, DialectConfig, Run, etc.)
  • Service interfaces (Adapter, Store)
  • Configuration types (ProjectConfig, TargetConfig)
  • Lint types (Severity, RuleInfo)
  • Base AST interface (Node)

The Golden Rule: pkg/core imports ONLY pkg/token and stdlib. All other packages depend on core, not the reverse.

Index

Constants

View Source
const (
	JoinInner = "INNER"
	JoinLeft  = "LEFT"
	JoinRight = "RIGHT"
	JoinFull  = "FULL"
	JoinCross = "CROSS"
)

Standard ANSI SQL join type values.

View Source
const (
	MaterializationTable       = "table"
	MaterializationView        = "view"
	MaterializationIncremental = "incremental"
)

Materialization constants for model types.

View Source
const (
	PrecedenceNone       = 0
	PrecedenceOr         = 1
	PrecedenceAnd        = 2
	PrecedenceNot        = 3
	PrecedenceComparison = 4 // =, <>, <, >, <=, >=, LIKE, ILIKE, IN, BETWEEN
	PrecedenceAddition   = 5 // +, -, ||
	PrecedenceMultiply   = 6 // *, /, %
	PrecedenceUnary      = 7 // -, +, NOT
	PrecedencePostfix    = 8 // ::, [], ()
)

Precedence constants for operator precedence parsing.

Variables

View Source
var ErrDialectRequired = errors.New("dialect is required")

ErrDialectRequired is returned when a dialect is required but not provided.

Functions

func AllKnownClauses

func AllKnownClauses() map[token.TokenType]string

AllKnownClauses returns all registered clause tokens. Useful for debugging and testing.

func IsKnownClause

func IsKnownClause(t token.TokenType) (string, bool)

IsKnownClause returns true if ANY registered dialect uses this token as a clause. Returns the clause name for error messages.

func RecordClause

func RecordClause(t token.TokenType, name string)

RecordClause registers a token as a clause keyword. Called automatically by dialect.Builder.ClauseHandler().

Types

type AcceptedValuesConfig

type AcceptedValuesConfig struct {
	Column string
	Values []string
}

AcceptedValuesConfig represents accepted values test configuration.

type Adapter

type Adapter interface {
	// Connect establishes a connection to the database.
	Connect(ctx context.Context, cfg AdapterConfig) error

	// Close closes the database connection.
	Close() error

	// Exec executes a SQL statement that doesn't return rows.
	Exec(ctx context.Context, sql string) error

	// Query executes a SQL statement that returns rows.
	Query(ctx context.Context, sql string) (*Rows, error)

	// GetTableMetadata retrieves metadata for a table.
	GetTableMetadata(ctx context.Context, table string) (*TableMetadata, error)

	// LoadCSV loads data from a CSV file into a table.
	LoadCSV(ctx context.Context, tableName, filePath string) error

	// DialectConfig returns the static dialect configuration.
	DialectConfig() *DialectConfig
}

Adapter defines the interface that all database adapters must implement.

type AdapterConfig

type AdapterConfig struct {
	Type     string
	Path     string
	Host     string
	Port     int
	Database string
	Username string
	Password string
	Schema   string
	Options  map[string]string
	Params   map[string]any
}

AdapterConfig holds configuration for connecting to a database.

type BetweenExpr

type BetweenExpr struct {
	Expr Expr
	Not  bool
	Low  Expr
	High Expr
}

BetweenExpr represents a BETWEEN expression.

func (*BetweenExpr) End

func (b *BetweenExpr) End() token.Position

End implements Node.

func (*BetweenExpr) Pos

func (b *BetweenExpr) Pos() token.Position

Pos implements Node.

type BinaryExpr

type BinaryExpr struct {
	Left  Expr
	Op    token.TokenType
	Right Expr
}

BinaryExpr represents a binary expression.

func (*BinaryExpr) End

func (b *BinaryExpr) End() token.Position

End implements Node.

func (*BinaryExpr) GetLeft

func (b *BinaryExpr) GetLeft() Expr

GetLeft returns the left operand.

func (*BinaryExpr) GetOp

func (b *BinaryExpr) GetOp() token.TokenType

GetOp returns the operator.

func (*BinaryExpr) GetRight

func (b *BinaryExpr) GetRight() Expr

GetRight returns the right operand.

func (*BinaryExpr) Pos

func (b *BinaryExpr) Pos() token.Position

Pos implements Node.

type CTE

type CTE struct {
	NodeInfo
	Name   string
	Select *SelectStmt
}

CTE represents a Common Table Expression.

type CaseExpr

type CaseExpr struct {
	Operand Expr // CASE operand WHEN... (optional)
	Whens   []WhenClause
	Else    Expr
}

CaseExpr represents a CASE expression.

func (*CaseExpr) End

func (c *CaseExpr) End() token.Position

End implements Node.

func (*CaseExpr) Pos

func (c *CaseExpr) Pos() token.Position

Pos implements Node.

type CastExpr

type CastExpr struct {
	Expr     Expr
	TypeName string
}

CastExpr represents a CAST expression.

func (*CastExpr) End

func (c *CastExpr) End() token.Position

End implements Node.

func (*CastExpr) Pos

func (c *CastExpr) Pos() token.Position

Pos implements Node.

type ClauseDef

type ClauseDef struct {
	Token    token.TokenType
	Handler  any // spi.ClauseHandler - cast at call site
	Slot     ClauseSlot
	Keywords []string
	Inline   bool
}

ClauseDef bundles clause parsing logic with storage destination. Handler is stored as 'any' to avoid import cycles with pkg/spi. Consumers cast to spi.ClauseHandler when invoking.

type ClauseSlot

type ClauseSlot int

ClauseSlot specifies where a parsed clause result should be stored in SelectCore. This enables dialects to declaratively specify storage locations for their clauses.

const (
	SlotWhere ClauseSlot = iota
	SlotGroupBy
	SlotHaving
	SlotWindow
	SlotOrderBy
	SlotLimit
	SlotOffset
	SlotQualify
	SlotFetch      // FETCH FIRST/NEXT clause
	SlotExtensions // Default for custom/dialect-specific clauses
)

ClauseSlot constants define where parsed clause results are stored in SelectCore.

func (ClauseSlot) String

func (s ClauseSlot) String() string

String returns the slot name for debugging.

type Column

type Column struct {
	Name       string
	Type       string
	Nullable   bool
	PrimaryKey bool
	Position   int
}

Column represents a column in a database table.

type ColumnInfo

type ColumnInfo struct {
	Name          string
	Index         int
	TransformType TransformType // "" (direct) or "EXPR"
	Function      string        // "sum", "count", etc.
	Sources       []SourceRef   // where this column comes from
}

ColumnInfo represents column lineage information.

type ColumnRef

type ColumnRef struct {
	Table  string // optional table/alias qualifier
	Column string
}

ColumnRef represents a column reference (possibly qualified).

func (*ColumnRef) End

func (c *ColumnRef) End() token.Position

End implements Node.

func (*ColumnRef) GetColumn

func (c *ColumnRef) GetColumn() string

GetColumn returns the column name.

func (*ColumnRef) GetTable

func (c *ColumnRef) GetTable() string

GetTable returns the table qualifier.

func (*ColumnRef) Pos

func (c *ColumnRef) Pos() token.Position

Pos implements Node.

type Conditional

type Conditional struct {
	Condition string
	Content   string
}

Conditional represents an #if directive block.

type Dependency

type Dependency struct {
	ModelID  string
	ParentID string
}

Dependency represents an edge in the model dependency graph.

type DerivedTable

type DerivedTable struct {
	NodeInfo
	Select *SelectStmt
	Alias  string
}

DerivedTable represents a subquery in FROM clause.

func (*DerivedTable) End

func (d *DerivedTable) End() token.Position

End implements Node.

func (*DerivedTable) Pos

func (d *DerivedTable) Pos() token.Position

Pos implements Node.

type Dialect

type Dialect struct {
	Name        string
	Identifiers IdentifierConfig

	// Database-specific settings
	DefaultSchema string
	Placeholder   PlaceholderStyle

	// Function classifications (normalized names)
	Aggregates     map[string]struct{}
	Generators     map[string]struct{}
	Windows        map[string]struct{}
	TableFunctions map[string]struct{}

	// Documentation for LSP
	Docs map[string]FunctionDoc

	// Keywords and types for autocomplete
	Keywords      map[string]struct{}
	ReservedWords map[string]struct{}
	DataTypes     []string

	// Parsing behavior - handlers stored as 'any'
	ClauseSeq      []token.TokenType
	ClauseDefs     map[token.TokenType]ClauseDef
	Symbols        map[string]token.TokenType
	DynamicKw      map[string]token.TokenType
	Precedences    map[token.TokenType]int
	InfixHandlers  map[token.TokenType]any // spi.InfixHandler
	PrefixHandlers map[token.TokenType]any // spi.PrefixHandler
	JoinTypes      map[token.TokenType]JoinTypeDef
	StarModifiers  map[token.TokenType]any // core.StarModifierHandler
	FromItems      map[token.TokenType]any // spi.FromItemHandler
}

Dialect represents a SQL dialect configuration with runtime behavior. Handler fields use 'any' to keep core free of spi imports. The pkg/dialect Builder provides type-safe construction.

func (*Dialect) ClauseDefFor

func (d *Dialect) ClauseDefFor(t token.TokenType) (ClauseDef, bool)

ClauseDefFor returns the definition for a clause token type.

func (*Dialect) ClauseSequence

func (d *Dialect) ClauseSequence() []token.TokenType

ClauseSequence returns the ordered list of clause token types.

func (*Dialect) Config

func (d *Dialect) Config() *DialectConfig

Config returns the pure data configuration for this dialect.

func (*Dialect) FromItemHandler

func (d *Dialect) FromItemHandler(t token.TokenType) any

FromItemHandler returns the handler for a FROM item extension (as any).

func (*Dialect) FunctionLineageTypeOf

func (d *Dialect) FunctionLineageTypeOf(name string) FunctionLineageType

FunctionLineageTypeOf returns the lineage classification for a function.

func (*Dialect) GetDoc

func (d *Dialect) GetDoc(name string) (FunctionDoc, bool)

GetDoc returns documentation for a function.

func (*Dialect) GetName

func (d *Dialect) GetName() string

GetName returns the dialect name.

func (*Dialect) InfixHandler

func (d *Dialect) InfixHandler(t token.TokenType) any

InfixHandler returns the custom infix handler (as any). Caller casts to spi.InfixHandler.

func (*Dialect) IsAggregate

func (d *Dialect) IsAggregate(name string) bool

IsAggregate returns true if the function is an aggregate function.

func (*Dialect) IsClauseToken

func (d *Dialect) IsClauseToken(t token.TokenType) bool

IsClauseToken returns true if this dialect supports the given clause.

func (*Dialect) IsFromItemToken

func (d *Dialect) IsFromItemToken(t token.TokenType) bool

IsFromItemToken returns true if the token is a FROM item extension.

func (*Dialect) IsGenerator

func (d *Dialect) IsGenerator(name string) bool

IsGenerator returns true if the function generates values.

func (*Dialect) IsJoinTypeToken

func (d *Dialect) IsJoinTypeToken(t token.TokenType) bool

IsJoinTypeToken returns true if the token is a dialect-specific join type.

func (*Dialect) IsReservedWord

func (d *Dialect) IsReservedWord(word string) bool

IsReservedWord returns true if the word needs quoting.

func (*Dialect) IsStarModifierToken

func (d *Dialect) IsStarModifierToken(t token.TokenType) bool

IsStarModifierToken returns true if the token is a star modifier.

func (*Dialect) IsTableFunction

func (d *Dialect) IsTableFunction(name string) bool

IsTableFunction returns true if the function acts as a table source.

func (*Dialect) IsWindow

func (d *Dialect) IsWindow(name string) bool

IsWindow returns true if the function is a window function.

func (*Dialect) JoinTypeDefFor

func (d *Dialect) JoinTypeDefFor(t token.TokenType) (JoinTypeDef, bool)

JoinTypeDefFor returns the definition for a join type token.

func (*Dialect) LookupKeyword

func (d *Dialect) LookupKeyword(name string) (token.TokenType, bool)

LookupKeyword returns the token type for a dynamic keyword.

func (*Dialect) NormalizeName

func (d *Dialect) NormalizeName(name string) string

NormalizeName normalizes an identifier according to dialect rules.

func (*Dialect) Precedence

func (d *Dialect) Precedence(t token.TokenType) int

Precedence returns the precedence level for an operator token.

func (*Dialect) PrefixHandler

func (d *Dialect) PrefixHandler(t token.TokenType) any

PrefixHandler returns the custom prefix handler (as any).

func (*Dialect) QuoteIdentifier

func (d *Dialect) QuoteIdentifier(name string) string

QuoteIdentifier quotes an identifier using the dialect's quote characters.

func (*Dialect) QuoteIdentifierIfNeeded

func (d *Dialect) QuoteIdentifierIfNeeded(name string) string

QuoteIdentifierIfNeeded quotes an identifier only if it's a reserved word.

func (*Dialect) StarModifierHandler

func (d *Dialect) StarModifierHandler(t token.TokenType) any

StarModifierHandler returns the handler for a star modifier (as any).

func (*Dialect) SymbolsMap

func (d *Dialect) SymbolsMap() map[string]token.TokenType

SymbolsMap returns the custom operators map for lexer symbol matching.

type DialectConfig

type DialectConfig struct {
	// Name is the dialect identifier (e.g., "duckdb", "postgres")
	Name string

	// Identifiers defines quoting and normalization rules
	Identifiers IdentifierConfig

	// DefaultSchema is the default schema name ("main" for DuckDB, "public" for Postgres)
	DefaultSchema string

	// Placeholder defines how query parameters are formatted
	Placeholder PlaceholderStyle

	// Function classifications (normalized names)
	Aggregates     []string // SUM, COUNT, AVG, etc.
	Generators     []string // NOW, UUID, RANDOM, etc.
	Windows        []string // ROW_NUMBER, LAG, LEAD, etc.
	TableFunctions []string // read_csv, generate_series, etc.

	// Keywords for autocomplete/highlighting
	Keywords  []string
	DataTypes []string

	// Feature Flags - Builder auto-wires based on these
	//
	// Clause extensions
	SupportsQualify    bool // QUALIFY clause for window filtering
	SupportsReturning  bool // RETURNING clause for DML
	SupportsGroupByAll bool // GROUP BY ALL
	SupportsOrderByAll bool // ORDER BY ALL

	// Operator extensions
	SupportsIlike        bool // ILIKE case-insensitive LIKE
	SupportsCastOperator bool // :: cast operator

	// Join extensions
	SupportsSemiAntiJoins bool // SEMI/ANTI join types
}

DialectConfig holds the static configuration for a SQL dialect. This is pure data — no handler functions.

The runtime behavior (clause handlers, infix handlers, etc.) lives in pkg/core.Dialect, which embeds this config.

type Environment

type Environment struct {
	Name      string
	CommitRef string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Environment represents a virtual environment pointer.

type ExcludeModifier

type ExcludeModifier struct {
	Columns []string // Column names to exclude
}

ExcludeModifier represents * EXCLUDE (col1, col2, ...).

type ExistsExpr

type ExistsExpr struct {
	Not    bool
	Select *SelectStmt
}

ExistsExpr represents an EXISTS expression.

func (*ExistsExpr) End

func (e *ExistsExpr) End() token.Position

End implements Node.

func (*ExistsExpr) Pos

func (e *ExistsExpr) Pos() token.Position

Pos implements Node.

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr is a marker interface for expression nodes.

type FetchClause

type FetchClause struct {
	First    bool // true = FIRST, false = NEXT (semantically identical)
	Count    Expr // Number of rows (nil = 1 row implied)
	Percent  bool // FETCH FIRST n PERCENT ROWS
	WithTies bool // true = WITH TIES, false = ONLY
}

FetchClause represents FETCH FIRST/NEXT n ROWS ONLY/WITH TIES (SQL:2008).

type FrameBound

type FrameBound struct {
	Type   FrameBoundType
	Offset Expr // for N PRECEDING/FOLLOWING
}

FrameBound represents a window frame bound.

type FrameBoundType

type FrameBoundType string

FrameBoundType represents the type of frame bound.

const (
	FrameUnboundedPreceding FrameBoundType = "UNBOUNDED PRECEDING"
	FrameUnboundedFollowing FrameBoundType = "UNBOUNDED FOLLOWING"
	FrameCurrentRow         FrameBoundType = "CURRENT ROW"
	FrameExprPreceding      FrameBoundType = "EXPR PRECEDING"
	FrameExprFollowing      FrameBoundType = "EXPR FOLLOWING"
)

FrameBoundType constants for window frame bound types.

type FrameSpec

type FrameSpec struct {
	Type  FrameType
	Start *FrameBound
	End   *FrameBound
}

FrameSpec represents a window frame specification.

type FrameType

type FrameType string

FrameType represents the type of window frame.

const (
	FrameRows   FrameType = "ROWS"
	FrameRange  FrameType = "RANGE"
	FrameGroups FrameType = "GROUPS"
)

FrameType constants for window frame specification types.

type FromClause

type FromClause struct {
	NodeInfo
	Source TableRef
	Joins  []*Join
}

FromClause represents the FROM clause.

type FuncCall

type FuncCall struct {
	Name     string
	Distinct bool
	Args     []Expr
	Star     bool        // COUNT(*)
	Window   *WindowSpec // OVER clause
	Filter   Expr        // FILTER (WHERE ...) clause
}

FuncCall represents a function call.

func (*FuncCall) End

func (f *FuncCall) End() token.Position

End implements Node.

func (*FuncCall) Pos

func (f *FuncCall) Pos() token.Position

Pos implements Node.

type FunctionDoc

type FunctionDoc struct {
	Description string
	Signatures  []string
	ReturnType  string
	Example     string
}

FunctionDoc contains documentation metadata for LSP features.

type FunctionLineageType

type FunctionLineageType int

FunctionLineageType classifies how a function affects lineage.

const (
	// LineagePassthrough indicates the function passes through columns unchanged.
	LineagePassthrough FunctionLineageType = iota
	// LineageAggregate indicates an aggregate function.
	LineageAggregate
	// LineageGenerator indicates a function that generates values without input columns.
	LineageGenerator
	// LineageWindow indicates a window function.
	LineageWindow
	// LineageTable indicates a table-valued function.
	LineageTable
)

FunctionLineageType constants classify function behavior for lineage analysis.

func (FunctionLineageType) String

func (t FunctionLineageType) String() string

String returns the string representation of FunctionLineageType.

type IdentifierConfig

type IdentifierConfig struct {
	Quote         string                // Quote character: ", `, [
	QuoteEnd      string                // End quote character (usually same as Quote, ] for [)
	Escape        string                // Escape sequence: "", “, ]]
	Normalization NormalizationStrategy // How to normalize unquoted identifiers
}

IdentifierConfig defines how identifiers are quoted and normalized.

type InExpr

type InExpr struct {
	Expr   Expr
	Not    bool
	Values []Expr      // IN (1, 2, 3)
	Query  *SelectStmt // IN (SELECT ...)
}

InExpr represents an IN expression.

func (*InExpr) End

func (i *InExpr) End() token.Position

End implements Node.

func (*InExpr) Pos

func (i *InExpr) Pos() token.Position

Pos implements Node.

type IndexExpr

type IndexExpr struct {
	NodeInfo
	Expr  Expr // The expression being indexed
	Index Expr // Simple index (non-nil if not a slice)
	// For slicing (arr[start:stop])
	IsSlice bool
	Start   Expr // nil means from beginning
	Stop    Expr // nil means to end (named Stop to avoid shadowing End() method)
}

IndexExpr represents array indexing or slicing: arr[i] or arr[start:stop]. Supports DuckDB array subscript and slice syntax.

type IsBoolExpr

type IsBoolExpr struct {
	Expr  Expr
	Not   bool
	Value bool // true for IS TRUE, false for IS FALSE
}

IsBoolExpr represents an IS [NOT] TRUE/FALSE expression.

func (*IsBoolExpr) End

func (i *IsBoolExpr) End() token.Position

End implements Node.

func (*IsBoolExpr) Pos

func (i *IsBoolExpr) Pos() token.Position

Pos implements Node.

type IsNullExpr

type IsNullExpr struct {
	Expr Expr
	Not  bool
}

IsNullExpr represents an IS NULL expression.

func (*IsNullExpr) End

func (i *IsNullExpr) End() token.Position

End implements Node.

func (*IsNullExpr) Pos

func (i *IsNullExpr) Pos() token.Position

Pos implements Node.

type Join

type Join struct {
	NodeInfo
	Type      JoinType
	Natural   bool // NATURAL JOIN modifier
	Right     TableRef
	Condition Expr     // ON clause (mutually exclusive with Using)
	Using     []string // USING (col1, col2) columns
}

Join represents a JOIN clause.

type JoinType

type JoinType string

JoinType represents the type of join. The value is the SQL keyword (e.g., "LEFT", "INNER", "SEMI"). Join type constants are defined in their respective dialect packages:

  • Standard joins (INNER, LEFT, RIGHT, FULL, CROSS): pkg/dialect/joins.go
  • DuckDB joins (SEMI, ANTI, ASOF, POSITIONAL): pkg/dialects/duckdb/join_types.go
const JoinComma JoinType = ","

JoinComma represents an implicit cross join using comma syntax. This is kept in the core package because it's syntactically unique (not a TYPE JOIN keyword pattern) and universal across all SQL dialects.

type JoinTypeDef

type JoinTypeDef struct {
	Token         token.TokenType // The trigger token for this join type
	Type          string          // JoinType value (e.g., "LEFT", "SEMI")
	OptionalToken token.TokenType // Optional modifier token (OUTER) - 0 means none
	RequiresOn    bool            // true if ON clause is required
	AllowsUsing   bool            // true if USING clause is allowed
}

JoinTypeDef defines a dialect-specific join type.

type LambdaExpr

type LambdaExpr struct {
	NodeInfo
	Params []string // Parameter names
	Body   Expr     // Lambda body expression
}

LambdaExpr represents a lambda expression: x -> expr or (x, y) -> expr. Used with list functions like list_transform, list_filter, list_reduce.

func (*LambdaExpr) GetBody

func (l *LambdaExpr) GetBody() Expr

GetBody returns the lambda body.

func (*LambdaExpr) GetParams

func (l *LambdaExpr) GetParams() []string

GetParams returns the parameter names.

type LateralTable

type LateralTable struct {
	NodeInfo
	Select *SelectStmt
	Alias  string
}

LateralTable represents a LATERAL subquery.

func (*LateralTable) End

func (l *LateralTable) End() token.Position

End implements Node.

func (*LateralTable) Pos

func (l *LateralTable) Pos() token.Position

Pos implements Node.

type LikeExpr

type LikeExpr struct {
	Expr    Expr
	Not     bool
	Pattern Expr
	Op      token.TokenType // token.LIKE or dialect-registered ILIKE
}

LikeExpr represents a LIKE expression.

func (*LikeExpr) End

func (l *LikeExpr) End() token.Position

End implements Node.

func (*LikeExpr) Pos

func (l *LikeExpr) Pos() token.Position

Pos implements Node.

type LintConfig

type LintConfig struct {
	// Disabled contains rule IDs to disable
	Disabled []string `koanf:"disabled"`

	// Severity maps rule ID to severity override (error, warning, info, hint)
	Severity map[string]string `koanf:"severity"`

	// Rules contains rule-specific options
	Rules map[string]RuleOptions `koanf:"rules"`

	// ProjectHealth holds project-level linting configuration
	ProjectHealth *ProjectHealthConfig `koanf:"project_health"`
}

LintConfig holds lint rule configuration.

type ListLiteral

type ListLiteral struct {
	NodeInfo
	Elements []Expr // Expression elements
}

ListLiteral represents a list/array literal: [expr, expr, ...]. DuckDB syntax for creating array values.

type Literal

type Literal struct {
	Type  LiteralType
	Value string
}

Literal represents a literal value.

func (*Literal) End

func (l *Literal) End() token.Position

End implements Node.

func (*Literal) Pos

func (l *Literal) Pos() token.Position

Pos implements Node.

type LiteralType

type LiteralType int

LiteralType represents the type of a literal.

const (
	LiteralNumber LiteralType = iota
	LiteralString
	LiteralBool
	LiteralNull
)

LiteralType constants for SQL literal value types.

type MacroExpr

type MacroExpr struct {
	NodeInfo
	Content string // raw {{ ... }} content including delimiters
}

MacroExpr represents a template macro expression (e.g., {{ ref('table') }}).

type MacroFunction

type MacroFunction struct {
	Namespace string
	Name      string
	Args      []string
	Docstring string
	Line      int
}

MacroFunction represents a function exported from a macro namespace.

type MacroNamespace

type MacroNamespace struct {
	Name      string
	FilePath  string
	Package   string
	UpdatedAt string
}

MacroNamespace represents a macro namespace from a .star file.

type MacroTable

type MacroTable struct {
	NodeInfo
	Content string // raw {{ ... }} content including delimiters
	Alias   string
}

MacroTable represents a macro used as a table reference (e.g., {{ ref('table') }}).

func (*MacroTable) End

func (m *MacroTable) End() token.Position

End implements Node.

func (*MacroTable) Pos

func (m *MacroTable) Pos() token.Position

Pos implements Node.

type Model

type Model struct {
	// Path is the model path (e.g., "staging.customers")
	Path string
	// Name is the model name (filename without extension)
	Name string
	// FilePath is the absolute path to the SQL file
	FilePath string
	// Materialized defines how the model is stored: table, view, incremental
	Materialized string
	// UniqueKey for incremental models
	UniqueKey string
	// Owner is the team/person responsible for this model
	Owner string
	// Schema is the database schema for this model
	Schema string
	// Description is a human-readable description of the model
	Description string
	// Tags are metadata labels for filtering/organizing models
	Tags []string
	// Meta contains custom extension fields
	Meta map[string]any
	// Tests contains test configurations
	Tests []TestConfig
	// Imports are explicit model dependencies from @import pragmas (legacy)
	Imports []string
	// Sources are all table names referenced in the SQL
	Sources []string
	// Columns contains column-level lineage information
	Columns []ColumnInfo
	// UsesSelectStar is true if model uses SELECT * or t.*
	UsesSelectStar bool
	// SQL is the raw SQL content (excluding frontmatter)
	SQL string
	// RawContent is the full file content including frontmatter
	RawContent string
	// Conditionals are #if directives for environment-specific SQL
	Conditionals []Conditional
	// HasFrontmatter indicates if YAML frontmatter was found
	HasFrontmatter bool
}

Model represents a SQL model (transformation unit). This contains the core identity fields only. Persistence-specific fields (ID, ContentHash, timestamps) belong in state.PersistedModel.

type ModelRun

type ModelRun struct {
	ID           string
	RunID        string
	ModelID      string
	Status       ModelRunStatus
	RowsAffected int64
	StartedAt    time.Time
	CompletedAt  *time.Time
	Error        string
	RenderMS     int64 // Time spent rendering template
	ExecutionMS  int64 // Time spent executing SQL
}

ModelRun represents a single execution of a model within a run.

type ModelRunStatus

type ModelRunStatus string

ModelRunStatus represents the status of an individual model execution.

const (
	ModelRunStatusPending ModelRunStatus = "pending"
	ModelRunStatusRunning ModelRunStatus = "running"
	ModelRunStatusSuccess ModelRunStatus = "success"
	ModelRunStatusFailed  ModelRunStatus = "failed"
	ModelRunStatusSkipped ModelRunStatus = "skipped"
)

Model run status constants.

type ModelRunWithInfo

type ModelRunWithInfo struct {
	ModelRun
	ModelPath string
	ModelName string
}

ModelRunWithInfo represents a model run with additional model info. Used for UI display where we need model path and name alongside run data.

type ModelType

type ModelType string

ModelType represents the semantic type of a model.

const (
	ModelTypeStaging      ModelType = "staging"
	ModelTypeIntermediate ModelType = "intermediate"
	ModelTypeMarts        ModelType = "marts"
	ModelTypeOther        ModelType = "other"
)

Model type constants.

type Node

type Node interface {
	// Pos returns the position of the first character of the node.
	Pos() token.Position
	// End returns the position of the character immediately after the node.
	End() token.Position
}

Node is the base interface for all AST nodes. This provides type safety for parser extension points (spi.ClauseHandler, etc.) without requiring pkg/core to import pkg/spi.

type NodeInfo

type NodeInfo struct {
	Span             token.Span
	LeadingComments  []*token.Comment
	TrailingComments []*token.Comment
}

NodeInfo provides common fields for all AST nodes. Embed this in node types that need position/comment tracking.

func (*NodeInfo) AddLeadingComment

func (n *NodeInfo) AddLeadingComment(c *token.Comment)

AddLeadingComment adds a leading comment to the node.

func (*NodeInfo) AddTrailingComment

func (n *NodeInfo) AddTrailingComment(c *token.Comment)

AddTrailingComment adds a trailing comment to the node.

func (*NodeInfo) End

func (n *NodeInfo) End() token.Position

End returns the position of the character immediately after the node.

func (*NodeInfo) GetSpan

func (n *NodeInfo) GetSpan() token.Span

GetSpan returns the node's source span.

func (*NodeInfo) Pos

func (n *NodeInfo) Pos() token.Position

Pos returns the position of the first character of the node.

type NormalizationStrategy

type NormalizationStrategy int

NormalizationStrategy defines how unquoted identifiers are normalized.

const (
	// NormLowercase normalizes unquoted identifiers to lowercase (default SQL behavior).
	NormLowercase NormalizationStrategy = iota
	// NormUppercase normalizes unquoted identifiers to uppercase (Snowflake, Oracle).
	NormUppercase
	// NormCaseSensitive preserves identifier case exactly (MySQL, ClickHouse).
	NormCaseSensitive
	// NormCaseInsensitive normalizes to lowercase for comparison (BigQuery, Hive, DuckDB).
	NormCaseInsensitive
)

type OperatorDef

type OperatorDef struct {
	Token      token.TokenType
	Symbol     string
	Precedence int
	Handler    any // spi.InfixHandler - cast at call site
}

OperatorDef defines an infix operator with precedence. Handler is stored as 'any' to avoid import cycles.

type OrderByItem

type OrderByItem struct {
	Expr       Expr
	Desc       bool
	NullsFirst *bool // nil means default, true = NULLS FIRST, false = NULLS LAST
}

OrderByItem represents an item in ORDER BY clause.

type ParenExpr

type ParenExpr struct {
	Expr Expr
}

ParenExpr represents a parenthesized expression.

func (*ParenExpr) End

func (p *ParenExpr) End() token.Position

End implements Node.

func (*ParenExpr) GetExpr

func (p *ParenExpr) GetExpr() Expr

GetExpr returns the inner expression.

func (*ParenExpr) Pos

func (p *ParenExpr) Pos() token.Position

Pos implements Node.

type PersistedModel

type PersistedModel struct {
	*Model             // Embed core identity
	ID          string // Database primary key
	ContentHash string // For change detection
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

PersistedModel represents a model stored in the state database. It wraps core.Model with persistence-specific fields.

type PivotAggregate

type PivotAggregate struct {
	Func  *FuncCall // The aggregate function
	Alias string    // Optional alias for the result columns
}

PivotAggregate represents an aggregate in PIVOT.

type PivotInValue

type PivotInValue struct {
	Value Expr   // The value (literal, identifier, or expression)
	Alias string // Optional column name alias
}

PivotInValue represents a value in PIVOT ... IN (...).

type PivotTable

type PivotTable struct {
	NodeInfo
	Source     TableRef         // The source table/subquery
	Aggregates []PivotAggregate // Aggregate functions to compute
	ForColumn  string           // Column to pivot on
	InValues   []PivotInValue   // Values to pivot (or InStar=true for *)
	InStar     bool             // true if IN *
	Alias      string           // Optional alias
}

PivotTable represents a PIVOT operation in FROM clause. SELECT * FROM table PIVOT (agg FOR col IN (values))

func (*PivotTable) End

func (p *PivotTable) End() token.Position

End implements Node.

func (*PivotTable) Pos

func (p *PivotTable) Pos() token.Position

Pos implements Node.

type PlaceholderStyle

type PlaceholderStyle int

PlaceholderStyle defines how query parameters are formatted.

const (
	// PlaceholderQuestion uses ? for all parameters (DuckDB, MySQL, SQLite).
	PlaceholderQuestion PlaceholderStyle = iota
	// PlaceholderDollar uses $1, $2, etc. for parameters (PostgreSQL).
	PlaceholderDollar
)

func (PlaceholderStyle) FormatPlaceholder

func (p PlaceholderStyle) FormatPlaceholder(index int) string

FormatPlaceholder returns a placeholder for the given parameter index (1-based). Returns "?" for PlaceholderQuestion style, "$1", "$2" etc. for PlaceholderDollar style.

type ProjectConfig

type ProjectConfig struct {
	ModelsDir string        `koanf:"models_dir"`
	SeedsDir  string        `koanf:"seeds_dir"`
	MacrosDir string        `koanf:"macros_dir"`
	Target    *TargetConfig `koanf:"target"`
	Lint      *LintConfig   `koanf:"lint"`
}

ProjectConfig holds project-level configuration.

type ProjectHealthConfig

type ProjectHealthConfig struct {
	// Enabled controls whether project health linting is enabled (default: true)
	Enabled *bool `koanf:"enabled"`

	// Thresholds for various rules
	Thresholds ProjectHealthThresholds `koanf:"thresholds"`

	// Rules maps rule IDs to severity overrides (off, info, warning, error)
	Rules map[string]string `koanf:"rules"`
}

ProjectHealthConfig holds configuration for project-level linting.

func (*ProjectHealthConfig) IsEnabled

func (c *ProjectHealthConfig) IsEnabled() bool

IsEnabled returns whether project health linting is enabled.

type ProjectHealthThresholds

type ProjectHealthThresholds struct {
	ModelFanout        int `koanf:"model_fanout"`        // PM04: default 3
	TooManyJoins       int `koanf:"too_many_joins"`      // PM05: default 7
	PassthroughColumns int `koanf:"passthrough_columns"` // PL01: default 20
	StarlarkComplexity int `koanf:"starlark_complexity"` // PT01: default 10
}

ProjectHealthThresholds holds configurable thresholds for project health rules.

type QueryableStore

type QueryableStore interface {
	Store
	// DB returns the underlying database connection for direct queries.
	DB() *sql.DB
}

QueryableStore is an optional interface that stores can implement to allow direct database queries (used by dev server for efficiency).

type RenameItem

type RenameItem struct {
	OldName string
	NewName string
}

RenameItem represents a single rename in RENAME modifier.

type RenameModifier

type RenameModifier struct {
	Items []RenameItem
}

RenameModifier represents * RENAME (old AS new, ...).

type ReplaceItem

type ReplaceItem struct {
	Expr  Expr   // Expression to use
	Alias string // Column name to replace
}

ReplaceItem represents a single replacement in REPLACE modifier.

type ReplaceModifier

type ReplaceModifier struct {
	Items []ReplaceItem
}

ReplaceModifier represents * REPLACE (expr AS col, ...).

type Rows

type Rows struct {
	*sql.Rows
}

Rows wraps sql.Rows to provide a consistent interface.

type RuleInfo

type RuleInfo struct {
	ID              string   `json:"id"`
	Name            string   `json:"name"`
	Group           string   `json:"group"`
	Description     string   `json:"description"`
	DefaultSeverity Severity `json:"default_severity"`
	ConfigKeys      []string `json:"config_keys,omitempty"`
	Dialects        []string `json:"dialects,omitempty"` // Only for SQL rules
	Type            string   `json:"type"`               // "sql" or "project"

	// Documentation fields
	Rationale   string `json:"rationale,omitempty"`
	BadExample  string `json:"bad_example,omitempty"`
	GoodExample string `json:"good_example,omitempty"`
	Fix         string `json:"fix,omitempty"`
}

RuleInfo provides metadata about a lint rule for documentation/tooling. This is a DTO (Data Transfer Object) - it carries data without behavior.

type RuleOptions

type RuleOptions map[string]any

RuleOptions holds rule-specific configuration options.

type Run

type Run struct {
	ID          string
	Environment string
	Status      RunStatus
	StartedAt   time.Time
	CompletedAt *time.Time
	Error       string
}

Run represents a pipeline execution session.

type RunStatus

type RunStatus string

RunStatus represents the status of a pipeline run.

const (
	RunStatusRunning   RunStatus = "running"
	RunStatusCompleted RunStatus = "completed"
	RunStatusFailed    RunStatus = "failed"
	RunStatusCancelled RunStatus = "cancelled"
)

Run status constants.

type SelectBody

type SelectBody struct {
	NodeInfo
	Left   *SelectCore
	Op     SetOpType   // UNION, INTERSECT, EXCEPT, or empty
	All    bool        // UNION ALL
	ByName bool        // DuckDB: BY NAME (match columns by name, not position)
	Right  *SelectBody // For chained set operations
}

SelectBody represents the body of a SELECT with possible set operations.

type SelectCore

type SelectCore struct {
	NodeInfo
	Distinct       bool
	Columns        []SelectItem
	From           *FromClause
	Where          Expr
	GroupBy        []Expr
	GroupByAll     bool // DuckDB: GROUP BY ALL (auto-group by non-aggregate columns)
	Having         Expr
	Windows        []WindowDef // Named window definitions (WINDOW clause)
	Qualify        Expr        // DuckDB/Snowflake window function filter
	OrderBy        []OrderByItem
	OrderByAll     bool // DuckDB: ORDER BY ALL (order by all select columns)
	OrderByAllDesc bool // DuckDB: Direction for ORDER BY ALL (true = DESC)
	Limit          Expr
	Offset         Expr
	Fetch          *FetchClause // FETCH FIRST/NEXT support (SQL:2008)

	// Extensions holds rare/custom dialect-specific nodes (e.g., CONNECT BY, SAMPLE).
	// Use this for dialect features that are too specialized to warrant typed fields.
	Extensions []Node
}

SelectCore represents the core SELECT clause.

type SelectItem

type SelectItem struct {
	Star      bool           // SELECT *
	TableStar string         // SELECT t.*
	Expr      Expr           // Expression
	Alias     string         // AS alias
	Modifiers []StarModifier // DuckDB: EXCLUDE, REPLACE, RENAME modifiers
}

SelectItem represents an item in the SELECT list.

type SelectStmt

type SelectStmt struct {
	NodeInfo
	With *WithClause
	Body *SelectBody
}

SelectStmt represents a complete SELECT statement with optional WITH clause.

func (*SelectStmt) End

func (s *SelectStmt) End() token.Position

End implements Node.

func (*SelectStmt) Pos

func (s *SelectStmt) Pos() token.Position

Pos implements Node.

type SetOpType

type SetOpType string

SetOpType represents the type of set operation.

const (
	SetOpNone      SetOpType = ""
	SetOpUnion     SetOpType = "UNION"
	SetOpUnionAll  SetOpType = "UNION ALL"
	SetOpIntersect SetOpType = "INTERSECT"
	SetOpExcept    SetOpType = "EXCEPT"
)

SetOpType constants for set operations in queries.

type Severity

type Severity int

Severity indicates the importance of a lint diagnostic.

const (
	// SeverityError indicates a critical issue that should be fixed.
	SeverityError Severity = iota
	// SeverityWarning indicates a potential issue that should be reviewed.
	SeverityWarning
	// SeverityInfo indicates informational feedback.
	SeverityInfo
	// SeverityHint indicates a suggestion for improvement.
	SeverityHint
)

Severity levels for diagnostics.

func ParseSeverity

func ParseSeverity(s string) (Severity, bool)

ParseSeverity converts a string to a Severity value. Returns the severity and true if valid, or SeverityWarning and false if invalid.

func (Severity) String

func (s Severity) String() string

String returns the string representation of the severity.

type SourceRef

type SourceRef struct {
	Table  string
	Column string
}

SourceRef represents a source column reference in lineage.

type StarExpr

type StarExpr struct {
	Table string // optional table qualifier for t.*
}

StarExpr represents a * expression (for SELECT *).

func (*StarExpr) End

func (s *StarExpr) End() token.Position

End implements Node.

func (*StarExpr) Pos

func (s *StarExpr) Pos() token.Position

Pos implements Node.

type StarModifier

type StarModifier interface {
	// contains filtered or unexported methods
}

StarModifier is the interface for star expression modifiers (DuckDB). Implemented by ExcludeModifier, ReplaceModifier, and RenameModifier.

type Stmt

type Stmt interface {
	Node
	// contains filtered or unexported methods
}

Stmt is a marker interface for statement nodes.

type Store

type Store interface {
	Open(path string) error
	Close() error
	InitSchema() error

	// Run operations
	CreateRun(env string) (*Run, error)
	GetRun(id string) (*Run, error)
	CompleteRun(id string, status RunStatus, errMsg string) error
	GetLatestRun(env string) (*Run, error)
	ListRuns(limit int) ([]*Run, error)

	// Model operations (uses PersistedModel for storage)
	RegisterModel(model *PersistedModel) error
	GetModelByID(id string) (*PersistedModel, error)
	GetModelByPath(path string) (*PersistedModel, error)
	GetModelByFilePath(filePath string) (*PersistedModel, error)
	UpdateModelHash(id string, contentHash string) error
	ListModels() ([]*PersistedModel, error)
	DeleteModelByFilePath(filePath string) error

	// Model run operations
	RecordModelRun(modelRun *ModelRun) error
	UpdateModelRun(id string, status ModelRunStatus, rowsAffected int64, errMsg string, renderMS int64, executionMS int64) error
	GetModelRunsForRun(runID string) ([]*ModelRun, error)
	GetModelRunsWithModelInfo(runID string) ([]*ModelRunWithInfo, error)
	GetLatestModelRun(modelID string) (*ModelRun, error)

	// Dependency operations
	SetDependencies(modelID string, parentIDs []string) error
	GetDependencies(modelID string) ([]string, error)
	GetDependents(modelID string) ([]string, error)

	// Environment operations
	CreateEnvironment(name string) (*Environment, error)
	GetEnvironment(name string) (*Environment, error)
	UpdateEnvironmentRef(name string, commitRef string) error

	// Column lineage operations
	SaveModelColumns(modelPath string, columns []ColumnInfo) error
	GetModelColumns(modelPath string) ([]ColumnInfo, error)
	DeleteModelColumns(modelPath string) error
	TraceColumnBackward(modelPath, columnName string) ([]TraceResult, error)
	TraceColumnForward(modelPath, columnName string) ([]TraceResult, error)

	// Macro operations
	SaveMacroNamespace(ns *MacroNamespace, functions []*MacroFunction) error
	GetMacroNamespaces() ([]*MacroNamespace, error)
	GetMacroNamespace(name string) (*MacroNamespace, error)
	GetMacroFunctions(namespace string) ([]*MacroFunction, error)
	GetMacroFunction(namespace, name string) (*MacroFunction, error)
	MacroFunctionExists(namespace, name string) (bool, error)
	SearchMacroNamespaces(prefix string) ([]*MacroNamespace, error)
	SearchMacroFunctions(namespace, prefix string) ([]*MacroFunction, error)
	DeleteMacroNamespace(name string) error
	DeleteMacroNamespaceByFilePath(filePath string) error

	// File hash tracking
	GetContentHash(filePath string) (string, error)
	SetContentHash(filePath, hash, fileType string) error
	DeleteContentHash(filePath string) error

	// List tracked file paths
	ListModelFilePaths() ([]string, error)
	ListMacroFilePaths() ([]string, error)

	// Column snapshot operations
	SaveColumnSnapshot(runID, modelPath, sourceTable string, columns []string) error
	GetColumnSnapshot(modelPath, sourceTable string) (columns []string, runID string, err error)
	DeleteOldSnapshots(keepRuns int) error

	// Batch operations
	BatchGetAllColumns() (map[string][]ColumnInfo, error)
	BatchGetAllDependencies() (map[string][]string, error)
	BatchGetAllDependents() (map[string][]string, error)
}

Store defines the interface for state management operations.

type StructField

type StructField struct {
	Key   string // Field name (can be identifier or string)
	Value Expr   // Field value
}

StructField represents a field in a struct literal.

type StructLiteral

type StructLiteral struct {
	NodeInfo
	Fields []StructField
}

StructLiteral represents a struct literal: {'key': value, ...}. DuckDB syntax for creating anonymous structs/records.

type SubqueryExpr

type SubqueryExpr struct {
	Select *SelectStmt
}

SubqueryExpr represents a subquery used as an expression (e.g., in EXISTS).

func (*SubqueryExpr) End

func (s *SubqueryExpr) End() token.Position

End implements Node.

func (*SubqueryExpr) Pos

func (s *SubqueryExpr) Pos() token.Position

Pos implements Node.

type TableMetadata

type TableMetadata struct {
	Schema    string
	Name      string
	Columns   []Column
	RowCount  int64
	SizeBytes int64
}

TableMetadata holds metadata about a database table.

type TableName

type TableName struct {
	NodeInfo
	Catalog string
	Schema  string
	Name    string
	Alias   string
}

TableName represents a table name reference.

func (*TableName) End

func (t *TableName) End() token.Position

End implements Node.

func (*TableName) Pos

func (t *TableName) Pos() token.Position

Pos implements Node.

type TableRef

type TableRef interface {
	Node
	// contains filtered or unexported methods
}

TableRef is a marker interface for table reference nodes.

type TargetConfig

type TargetConfig struct {
	Type string `koanf:"type"` // duckdb, postgres, snowflake, bigquery

	// File-based databases (DuckDB, SQLite)
	Database string `koanf:"database"` // file path or database name

	// Network databases
	Host     string `koanf:"host"`
	Port     int    `koanf:"port"`
	User     string `koanf:"user"`
	Password string `koanf:"password"`

	// Common
	Schema string `koanf:"schema"`

	// Snowflake-specific
	Account   string `koanf:"account"`
	Warehouse string `koanf:"warehouse"`
	Role      string `koanf:"role"`

	// Additional driver-specific options
	Options map[string]string `koanf:"options"`

	// Params holds adapter-specific configuration (e.g., DuckDB extensions, secrets, settings)
	Params map[string]any `koanf:"params"`
}

TargetConfig holds database target configuration.

type TestConfig

type TestConfig struct {
	Unique         []string
	NotNull        []string
	AcceptedValues *AcceptedValuesConfig
}

TestConfig represents test configuration for a model.

type TraceResult

type TraceResult struct {
	ModelPath  string
	ColumnName string
	Depth      int
	IsExternal bool
}

TraceResult represents a single node in a column lineage trace.

type TransformType

type TransformType string

TransformType describes how source columns are transformed.

const (
	// TransformDirect means the column is a direct copy (no transformation).
	TransformDirect TransformType = ""
	// TransformExpression means the column is derived from an expression.
	TransformExpression TransformType = "EXPR"
)

type UnaryExpr

type UnaryExpr struct {
	Op   token.TokenType
	Expr Expr
}

UnaryExpr represents a unary expression.

func (*UnaryExpr) End

func (u *UnaryExpr) End() token.Position

End implements Node.

func (*UnaryExpr) Pos

func (u *UnaryExpr) Pos() token.Position

Pos implements Node.

type UnpivotInGroup

type UnpivotInGroup struct {
	Columns []string // Column name(s) in this group
	Alias   string   // Optional row value alias
}

UnpivotInGroup represents a group of columns in UNPIVOT ... IN (...). For simple UNPIVOT: single column per group. For multi-column UNPIVOT: multiple columns per group.

type UnpivotTable

type UnpivotTable struct {
	NodeInfo
	Source       TableRef         // The source table/subquery
	ValueColumns []string         // Output column(s) for values
	NameColumn   string           // Output column for names
	InColumns    []UnpivotInGroup // Source columns to unpivot
	Alias        string           // Optional alias
}

UnpivotTable represents an UNPIVOT operation in FROM clause. SELECT * FROM table UNPIVOT (value FOR name IN (columns))

func (*UnpivotTable) End

func (u *UnpivotTable) End() token.Position

End implements Node.

func (*UnpivotTable) Pos

func (u *UnpivotTable) Pos() token.Position

Pos implements Node.

type WhenClause

type WhenClause struct {
	Condition Expr
	Result    Expr
}

WhenClause represents a WHEN clause in CASE expression.

type WindowDef

type WindowDef struct {
	Name string
	Spec *WindowSpec
}

WindowDef represents a named window definition in the WINDOW clause. Example: WINDOW w AS (PARTITION BY x ORDER BY y)

type WindowSpec

type WindowSpec struct {
	Name        string // Named window reference
	PartitionBy []Expr
	OrderBy     []OrderByItem
	Frame       *FrameSpec
}

WindowSpec represents a window specification (OVER clause).

type WithClause

type WithClause struct {
	NodeInfo
	Recursive bool
	CTEs      []*CTE
}

WithClause represents a WITH clause with CTEs.

Jump to

Keyboard shortcuts

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