dialect

package
v0.11.0 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: 16 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 dialect is how Baleia writes SQL for each engine kind: quoting, qualification, literals, statement splitting, previews, metadata queries and the SQL-generation primitives of the semantic layer (#14, #33, #91).

Every difference between engines is declared here; no other package spells engine SQL. Dialects describe what SQL looks like; they never authorize anything (that is internal/policy).

Index

Constants

View Source
const (
	Trino      = "trino"
	PostgreSQL = "postgresql"
	MySQL      = "mysql"
	MariaDB    = "mariadb"
	TSQL       = "tsql"
	Oracle     = "oracle"
	SQLite     = "sqlite"
	ClickHouse = "clickhouse"
	FlightSQL  = "flightsql-generic"
)

Names of the v1 dialects.

Variables

View Source
var (
	// ErrUnsupported means the dialect cannot express the request; callers
	// report it rather than falling back to another dialect's SQL.
	ErrUnsupported = errors.New("not supported by this dialect")
	// ErrInvalid means the input (identifier, type, value) is malformed.
	ErrInvalid = errors.New("invalid input")
)
View Source
var ErrDelimiter = errors.New("DELIMITER is a mysql client command, not SQL; end each statement with ';' instead")

ErrDelimiter is returned for the MySQL client's DELIMITER command.

Grains lists every TimeGrain, finest first.

Functions

func Callable

func Callable(dialectName, name string) bool

Callable reports whether a function in the dialect takes parentheses; the special forms (current_date) do not. MySQL/MariaDB expose USER(), SESSION_USER() and SYSTEM_USER() as callable functions.

func ParseStatement

func ParseStatement(d Dialect, sql string) (expressions.Expression, error)

ParseStatement parses one statement with a registered Baleia dialect. It is intentionally a package function so Dialect's SQL-generation interface does not force test doubles or external dialect implementations to depend on the parser.

func SqlglotDialect

func SqlglotDialect(d Dialect) (dialects.DialectType, error)

SqlglotDialect returns the sqlglot dialect name for d so external packages (reference extraction, classification) can drive the fork's optimizer and scope passes without depending on the unexported spec type.

Types

type AggFunc

type AggFunc string

AggFunc is an Aggregate function.

const (
	AggSum                 AggFunc = "sum"
	AggCount               AggFunc = "count"
	AggAvg                 AggFunc = "avg"
	AggMin                 AggFunc = "min"
	AggMax                 AggFunc = "max"
	AggCountDistinct       AggFunc = "count_distinct"
	AggApproxCountDistinct AggFunc = "approx_count_distinct"
)

type Dialect

type Dialect interface {
	Name() string

	// QuoteIdent returns s as a delimited identifier. Empty names, NUL bytes
	// and names over the engine's identifier limit are errors; nothing is
	// silently altered.
	QuoteIdent(s string) (string, error)
	// QualifiedName quotes each part and joins them with ".".
	QualifiedName(parts ...string) (string, error)
	// Qualify names an object following the engine's namespace rules:
	// catalogs are dropped where one connection sees one catalog, and a
	// schema path deeper than the engine supports is an error.
	Qualify(catalog string, schema []string, object string) (string, error)
	// QuoteString returns s as a string literal.
	QuoteString(s string) string
	// Literal renders v as a literal of type t (nil is NULL).
	Literal(v any, t LogicalType) (string, error)

	// SplitStatements splits a script into statements.
	SplitStatements(sql string) ([]string, error)
	// Statements is SplitStatements with byte offsets into sql.
	Statements(sql string) ([]Span, error)
	// PreviewSQL selects the first limit rows of an already qualified object.
	PreviewSQL(object string, limit int) string
	// Limit caps the rows of a query by wrapping it.
	Limit(sql string, n int) string
	// ExplainSQL returns the statement that explains sql, or false when the
	// engine has no single-statement EXPLAIN.
	ExplainSQL(sql string) (string, bool)
	// Placeholder is the n-th (1-based) bind parameter.
	Placeholder(n int) string
	MetadataQueries() MetadataQueries
	// FunctionQuery returns the engine's function-catalog SQL (#788) without
	// executing it. Names are in the first result column; additional columns
	// and the representation of overloads vary by dialect. An empty string
	// means no query is configured.
	FunctionQuery() string

	// SQL-generation primitives. Expressions are SQL the caller built from
	// quoted identifiers; they are not re-checked.
	Aggregate(fn AggFunc, expr string, distinct bool) (string, error)
	TimeTrunc(expr string, grain TimeGrain) (string, error)
	Cast(expr string, t LogicalType) (string, error)
	// SafeDivide divides as floating point and yields NULL for a zero
	// denominator.
	SafeDivide(num, den string) string

	// Editor support.
	FormatterLanguage() string // sql-formatter language id
	CodeMirrorBase() string    // @maltzsama/lang-sql base dialect
}

Dialect is how one engine kind spells SQL.

type LogicalType

type LogicalType string

LogicalType is an engine-independent column type: boolean, int64, decimal(p,s), float64, string, date, timestamp, timestamptz, binary, json.

const (
	TypeBoolean     LogicalType = "boolean"
	TypeInt64       LogicalType = "int64"
	TypeFloat64     LogicalType = "float64"
	TypeString      LogicalType = "string"
	TypeDate        LogicalType = "date"
	TypeTimestamp   LogicalType = "timestamp"
	TypeTimestampTZ LogicalType = "timestamptz"
	TypeBinary      LogicalType = "binary"
	TypeJSON        LogicalType = "json"
)

func Decimal

func Decimal(precision, scale int) LogicalType

Decimal returns the logical type decimal(p,s).

type MetadataQueries

type MetadataQueries struct {
	Schemas string // schema_name
	Tables  string // table_name, table_type ('TABLE' or 'VIEW')
	Columns string // column_name, data_type, is_nullable ('YES'/'NO'), ordinal_position
	// Catalog reads every table of the session's catalog with its columns in
	// one query, for the metadata sync through an engine (#15): table_schema,
	// table_name, table_type ('TABLE' or 'VIEW'), column_name, data_type,
	// is_nullable ('YES'/'NO'), ordinal_position, ordered by schema, table
	// and position; a table without columns has NULL column fields. Empty
	// when the engine has no such query.
	Catalog string
}

MetadataQueries are the catalog queries of an engine, in the dialect's own bind-parameter syntax (Placeholder). Schemas takes no parameter; Tables takes the schema; Columns takes the schema and the table. Each returns the documented columns in order.

type Registry

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

Registry holds every dialect by name (#364 follow-up): built once by NewRegistry and only read afterwards, so it is safe for concurrent use.

func NewRegistry

func NewRegistry() *Registry

NewRegistry builds the dialects of every engine Baleia supports.

func (*Registry) ForEngineKind

func (r *Registry) ForEngineKind(kind string) (Dialect, bool)

ForEngineKind returns the dialect of a query-engine kind.

func (*Registry) Get

func (r *Registry) Get(name string) (Dialect, bool)

Get returns a dialect by name.

func (*Registry) MustGet

func (r *Registry) MustGet(name string) Dialect

MustGet returns a dialect and panics for an unknown name; use it only with the name constants.

func (*Registry) Names

func (r *Registry) Names() []string

Names lists the dialects, sorted.

type Span

type Span struct {
	Text       string
	Start, End int
}

Span is one statement of a script: Text is the statement without its terminator or surrounding whitespace; Start and End are byte offsets of Text in the script.

type TimeGrain

type TimeGrain string

TimeGrain is a TimeTrunc unit.

const (
	GrainSecond  TimeGrain = "second"
	GrainMinute  TimeGrain = "minute"
	GrainHour    TimeGrain = "hour"
	GrainDay     TimeGrain = "day"
	GrainWeek    TimeGrain = "week" // ISO week, starting on Monday
	GrainMonth   TimeGrain = "month"
	GrainQuarter TimeGrain = "quarter"
	GrainYear    TimeGrain = "year"
)

Jump to

Keyboard shortcuts

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