dialect

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 dialect provides SQL dialect configuration and function classification.

This package contains the public contract for dialect definitions used by the parser, lineage analyzer, and other SQL-aware components. Concrete dialect implementations are registered from pkg/dialects/*/ packages.

The Dialect struct is now defined in pkg/core. This package provides a Builder for type-safe construction and manages the global registry.

Package dialect provides SQL dialect configuration and function classification.

This file contains stateless clause handlers that form the "toolbox" of reusable parsing logic. These handlers are pure functions that accept spi.ParserOps and return spi.Node.

Package dialect provides SQL dialect configuration and function classification.

This file contains join type definitions that form the "toolbox" of reusable join configurations. These can be composed into any dialect.

Package dialect provides SQL dialect configuration and function classification.

This file contains operator definitions that form the "toolbox" of reusable operator configurations. These can be composed into any dialect.

Package dialect provides SQL dialect configuration and function classification.

This file contains pre-built ClauseDef definitions - the "menu items" that dialects can compose from. Each ClauseDef bundles a token, handler, slot, and metadata together.

Index

Constants

This section is empty.

Variables

View Source
var (
	// StandardWhere is the standard WHERE clause definition.
	StandardWhere = core.ClauseDef{
		Token:   token.WHERE,
		Handler: spi.ClauseHandler(ParseWhere),
		Slot:    core.SlotWhere,
	}

	// StandardGroupBy is the standard GROUP BY clause definition.
	StandardGroupBy = core.ClauseDef{
		Token:    token.GROUP,
		Handler:  spi.ClauseHandler(ParseGroupBy),
		Slot:     core.SlotGroupBy,
		Keywords: []string{"GROUP", "BY"},
	}

	// StandardHaving is the standard HAVING clause definition.
	StandardHaving = core.ClauseDef{
		Token:   token.HAVING,
		Handler: spi.ClauseHandler(ParseHaving),
		Slot:    core.SlotHaving,
	}

	// StandardWindow is the standard WINDOW clause definition.
	StandardWindow = core.ClauseDef{
		Token:   token.WINDOW,
		Handler: spi.ClauseHandler(ParseWindow),
		Slot:    core.SlotWindow,
	}

	// StandardOrderBy is the standard ORDER BY clause definition.
	StandardOrderBy = core.ClauseDef{
		Token:    token.ORDER,
		Handler:  spi.ClauseHandler(ParseOrderBy),
		Slot:     core.SlotOrderBy,
		Keywords: []string{"ORDER", "BY"},
	}

	// StandardLimit is the standard LIMIT clause definition.
	StandardLimit = core.ClauseDef{
		Token:   token.LIMIT,
		Handler: spi.ClauseHandler(ParseLimit),
		Slot:    core.SlotLimit,
		Inline:  true,
	}

	// StandardOffset is the standard OFFSET clause definition.
	StandardOffset = core.ClauseDef{
		Token:   token.OFFSET,
		Handler: spi.ClauseHandler(ParseOffset),
		Slot:    core.SlotOffset,
		Inline:  true,
	}

	// StandardFetch is the standard FETCH clause definition (SQL:2008).
	StandardFetch = core.ClauseDef{
		Token:   token.FETCH,
		Handler: spi.ClauseHandler(ParseFetch),
		Slot:    core.SlotFetch,
	}

	// StandardQualify is the standard QUALIFY clause definition (DuckDB, Databricks, etc.).
	StandardQualify = core.ClauseDef{
		Token:   token.QUALIFY,
		Handler: spi.ClauseHandler(ParseQualify),
		Slot:    core.SlotQualify,
	}
)
View Source
var ANSIJoinTypes = []core.JoinTypeDef{
	{
		Token:       token.INNER,
		Type:        core.JoinInner,
		RequiresOn:  true,
		AllowsUsing: true,
	},
	{
		Token:         token.LEFT,
		Type:          core.JoinLeft,
		OptionalToken: token.OUTER,
		RequiresOn:    true,
		AllowsUsing:   true,
	},
	{
		Token:         token.RIGHT,
		Type:          core.JoinRight,
		OptionalToken: token.OUTER,
		RequiresOn:    true,
		AllowsUsing:   true,
	},
	{
		Token:         token.FULL,
		Type:          core.JoinFull,
		OptionalToken: token.OUTER,
		RequiresOn:    true,
		AllowsUsing:   true,
	},
	{
		Token:       token.CROSS,
		Type:        core.JoinCross,
		RequiresOn:  false,
		AllowsUsing: false,
	},
}

ANSIJoinTypes contains standard SQL join types.

View Source
var ANSIOperators = []core.OperatorDef{

	{Token: token.OR, Precedence: core.PrecedenceOr},
	{Token: token.AND, Precedence: core.PrecedenceAnd},

	{Token: token.EQ, Precedence: core.PrecedenceComparison},
	{Token: token.NE, Precedence: core.PrecedenceComparison},
	{Token: token.LT, Precedence: core.PrecedenceComparison},
	{Token: token.GT, Precedence: core.PrecedenceComparison},
	{Token: token.LE, Precedence: core.PrecedenceComparison},
	{Token: token.GE, Precedence: core.PrecedenceComparison},
	{Token: token.LIKE, Precedence: core.PrecedenceComparison},
	{Token: token.IN, Precedence: core.PrecedenceComparison},
	{Token: token.BETWEEN, Precedence: core.PrecedenceComparison},
	{Token: token.IS, Precedence: core.PrecedenceComparison},

	{Token: token.PLUS, Precedence: core.PrecedenceAddition},
	{Token: token.MINUS, Precedence: core.PrecedenceAddition},
	{Token: token.DPIPE, Precedence: core.PrecedenceAddition},

	{Token: token.STAR, Precedence: core.PrecedenceMultiply},
	{Token: token.SLASH, Precedence: core.PrecedenceMultiply},
	{Token: token.MOD, Precedence: core.PrecedenceMultiply},
}

ANSIOperators contains standard SQL operators with their precedence.

StandardSelectClauses is the typical ANSI SELECT clause sequence. Dialects can use this directly or compose their own from the individual defs.

Functions

func AllClauseTokens

func AllClauseTokens(d *core.Dialect) []token.TokenType

AllClauseTokens returns all clause tokens registered in this dialect.

func AllFunctions

func AllFunctions(d *core.Dialect) []string

AllFunctions returns all known function names.

func AllJoinTypeTokens

func AllJoinTypeTokens(d *core.Dialect) []token.TokenType

AllJoinTypeTokens returns all join type tokens registered in this dialect.

func AllKnownClauses

func AllKnownClauses() map[token.TokenType]string

AllKnownClauses returns all registered clause tokens. Useful for debugging and testing. This is a convenience wrapper around core.AllKnownClauses.

func ClauseHandler

func ClauseHandler(d *core.Dialect, t token.TokenType) spi.ClauseHandler

ClauseHandler returns the handler for a clause token type.

func FormatPlaceholder

func FormatPlaceholder(d *core.Dialect, index int) string

FormatPlaceholder returns a placeholder for the given parameter index (1-based).

func FromItemHandler

func FromItemHandler(d *core.Dialect, t token.TokenType) spi.FromItemHandler

FromItemHandler returns the handler for a FROM item token type.

func FunctionLineageType

func FunctionLineageType(d *core.Dialect, name string) core.FunctionLineageType

FunctionLineageType returns the lineage classification for a function.

func Get

func Get(name string) (*core.Dialect, bool)

Get returns a dialect by name.

func GroupBy

func GroupBy(opts GroupByOpts) core.ClauseDef

GroupBy returns a ClauseDef for GROUP BY with options.

func InfixHandler

func InfixHandler(d *core.Dialect, t token.TokenType) spi.InfixHandler

InfixHandler returns the custom infix handler for an operator token.

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. This is a convenience wrapper around core.IsKnownClause.

func Keywords

func Keywords(d *core.Dialect) []string

Keywords returns all reserved keywords.

func List

func List() []string

List returns all registered dialect names (sorted).

func OrderBy

func OrderBy(opts OrderByOpts) core.ClauseDef

OrderBy returns a ClauseDef for ORDER BY with options.

func ParseFetch

func ParseFetch(p spi.ParserOps) (spi.Node, error)

ParseFetch handles the FETCH FIRST/NEXT clause (SQL:2008). The FETCH keyword has already been consumed.

func ParseGroupBy

func ParseGroupBy(p spi.ParserOps) (spi.Node, error)

ParseGroupBy handles the standard GROUP BY clause. The GROUP keyword has already been consumed.

func ParseGroupByWithAll

func ParseGroupByWithAll(p spi.ParserOps) (spi.Node, error)

ParseGroupByWithAll handles GROUP BY with optional ALL keyword. The GROUP keyword has already been consumed.

func ParseHaving

func ParseHaving(p spi.ParserOps) (spi.Node, error)

ParseHaving handles the standard HAVING clause. The HAVING keyword has already been consumed.

func ParseLimit

func ParseLimit(p spi.ParserOps) (spi.Node, error)

ParseLimit handles the standard LIMIT clause. The LIMIT keyword has already been consumed.

func ParseOffset

func ParseOffset(p spi.ParserOps) (spi.Node, error)

ParseOffset handles the standard OFFSET clause. The OFFSET keyword has already been consumed.

func ParseOrderBy

func ParseOrderBy(p spi.ParserOps) (spi.Node, error)

ParseOrderBy handles the standard ORDER BY clause. The ORDER keyword has already been consumed.

func ParseOrderByWithAll

func ParseOrderByWithAll(p spi.ParserOps) (spi.Node, error)

ParseOrderByWithAll handles ORDER BY with optional ALL keyword. The ORDER keyword has already been consumed.

func ParseQualify

func ParseQualify(p spi.ParserOps) (spi.Node, error)

ParseQualify handles the QUALIFY clause (DuckDB, Databricks, etc.). The QUALIFY keyword has already been consumed.

func ParseWhere

func ParseWhere(p spi.ParserOps) (spi.Node, error)

ParseWhere handles the standard WHERE clause. The WHERE keyword has already been consumed.

func ParseWindow

func ParseWindow(_ spi.ParserOps) (spi.Node, error)

ParseWindow handles named window definitions. The WINDOW keyword has already been consumed.

func PrefixHandler

func PrefixHandler(d *core.Dialect, t token.TokenType) spi.PrefixHandler

PrefixHandler returns the custom prefix handler for an operator token.

func Register

func Register(d *core.Dialect)

Register registers a dialect in the global registry. Called by dialect implementations in their init() functions.

func StarModifierHandler

func StarModifierHandler(d *core.Dialect, t token.TokenType) spi.StarModifierHandler

StarModifierHandler returns the handler for a star modifier token type.

Types

type Builder

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

Builder provides a fluent API for constructing dialects.

func New

func New(cfg *core.DialectConfig) *Builder

New creates a dialect builder from a DialectConfig. The builder will auto-wire features based on config flags when Build() is called.

func NewDialect

func NewDialect(name string) *Builder

NewDialect creates a new dialect builder with the given name.

func (*Builder) AddClauseAfter

func (b *Builder) AddClauseAfter(after, t token.TokenType, handler spi.ClauseHandler, slot core.ClauseSlot, opts ...ClauseOption) *Builder

AddClauseAfter inserts a clause into the sequence after another clause.

func (*Builder) AddFromItem

func (b *Builder) AddFromItem(t token.TokenType, handler spi.FromItemHandler) *Builder

AddFromItem registers a FROM clause item handler.

func (*Builder) AddInfix

func (b *Builder) AddInfix(t token.TokenType, precedence int) *Builder

AddInfix registers an infix operator with precedence.

func (*Builder) AddInfixWithHandler

func (b *Builder) AddInfixWithHandler(t token.TokenType, precedence int, handler spi.InfixHandler) *Builder

AddInfixWithHandler registers an infix operator with custom handler.

func (*Builder) AddJoinType

func (b *Builder) AddJoinType(t token.TokenType, def core.JoinTypeDef) *Builder

AddJoinType registers a dialect-specific join type.

func (*Builder) AddKeyword

func (b *Builder) AddKeyword(name string, t token.TokenType) *Builder

AddKeyword registers a dynamic keyword for the lexer.

func (*Builder) AddOperator

func (b *Builder) AddOperator(symbol string, t token.TokenType) *Builder

AddOperator registers a custom operator symbol for the lexer.

func (*Builder) AddPrefix

func (b *Builder) AddPrefix(t token.TokenType, handler spi.PrefixHandler) *Builder

AddPrefix registers a prefix expression handler.

func (*Builder) AddStarModifier

func (b *Builder) AddStarModifier(t token.TokenType, handler spi.StarModifierHandler) *Builder

AddStarModifier registers a star expression modifier handler.

func (*Builder) Aggregates

func (b *Builder) Aggregates(funcs ...string) *Builder

Aggregates adds aggregate functions to the dialect.

func (*Builder) Build

func (b *Builder) Build() *core.Dialect

Build returns the constructed dialect. If the builder was created with New(cfg), this auto-wires features based on config flags.

func (*Builder) ClauseHandler

func (b *Builder) ClauseHandler(t token.TokenType, handler spi.ClauseHandler, slot core.ClauseSlot, opts ...ClauseOption) *Builder

ClauseHandler registers a handler for a clause token with storage slot.

func (*Builder) ClauseSequence

func (b *Builder) ClauseSequence(tokens ...token.TokenType) *Builder

ClauseSequence sets the full clause sequence (for base dialects).

func (*Builder) Clauses

func (b *Builder) Clauses(defs ...core.ClauseDef) *Builder

Clauses sets the clause sequence from a list of ClauseDefs.

func (*Builder) DefaultSchema

func (b *Builder) DefaultSchema(schema string) *Builder

DefaultSchema sets the default schema name.

func (*Builder) Generators

func (b *Builder) Generators(funcs ...string) *Builder

Generators adds generator functions (no input columns) to the dialect.

func (*Builder) Identifiers

func (b *Builder) Identifiers(quote, quoteEnd, escape string, norm core.NormalizationStrategy) *Builder

Identifiers configures identifier quoting and normalization.

func (*Builder) JoinTypes

func (b *Builder) JoinTypes(sets ...[]core.JoinTypeDef) *Builder

JoinTypes adds join type definitions in bulk.

func (*Builder) Operators

func (b *Builder) Operators(sets ...[]core.OperatorDef) *Builder

Operators adds operator definitions in bulk.

func (*Builder) PlaceholderStyle

func (b *Builder) PlaceholderStyle(style core.PlaceholderStyle) *Builder

PlaceholderStyle sets how query parameters are formatted.

func (*Builder) RemoveClause

func (b *Builder) RemoveClause(t token.TokenType) *Builder

RemoveClause removes a clause from the sequence.

func (*Builder) TableFunctions

func (b *Builder) TableFunctions(funcs ...string) *Builder

TableFunctions adds table-valued functions to the dialect.

func (*Builder) Windows

func (b *Builder) Windows(funcs ...string) *Builder

Windows adds window-only functions to the dialect.

func (*Builder) WithDataTypes

func (b *Builder) WithDataTypes(types ...string) *Builder

WithDataTypes registers supported data types.

func (*Builder) WithDocs

func (b *Builder) WithDocs(docs map[string]core.FunctionDoc) *Builder

WithDocs registers documentation for functions.

func (*Builder) WithKeywords

func (b *Builder) WithKeywords(kws ...string) *Builder

WithKeywords registers reserved keywords.

func (*Builder) WithReservedWords

func (b *Builder) WithReservedWords(words ...string) *Builder

WithReservedWords registers words that need quoting when used as identifiers.

type ClauseOption

type ClauseOption func(*core.ClauseDef)

ClauseOption configures a ClauseDef.

func WithInline

func WithInline() ClauseOption

WithInline marks a clause as inline (keyword and value on same line).

func WithKeywords

func WithKeywords(keywords ...string) ClauseOption

WithKeywords sets the display keywords for a clause.

type FetchClause

type FetchClause struct {
	First    bool      // true = FIRST, false = NEXT (semantically identical)
	Count    core.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). This is defined here to avoid import cycles with the parser package.

func (*FetchClause) GetCount

func (f *FetchClause) GetCount() core.Expr

GetCount implements FetchClauseData.

func (*FetchClause) GetFirst

func (f *FetchClause) GetFirst() bool

GetFirst implements FetchClauseData.

func (*FetchClause) GetPercent

func (f *FetchClause) GetPercent() bool

GetPercent implements FetchClauseData.

func (*FetchClause) GetWithTies

func (f *FetchClause) GetWithTies() bool

GetWithTies implements FetchClauseData.

type GroupByOpts

type GroupByOpts struct {
	AllowAll bool // Support GROUP BY ALL
}

GroupByOpts configures GROUP BY clause behavior.

type OrderByOpts

type OrderByOpts struct {
	AllowAll bool // Support ORDER BY ALL
}

OrderByOpts configures ORDER BY clause behavior.

Jump to

Keyboard shortcuts

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