sqlgen

package
v0.18.52 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sqlgen generates random-but-valid SQL queries over a described schema, for differential testing (#288). Generation is fully determined by the seed — a failing seed IS the repro — and is weighted toward the shapes that have historically produced distributed-mode divergence: aggregate-free GROUP BY (#163/#166), DISTINCT (#163), expressions in the SELECT list past a shuffle (#169/#273), and scalar-subquery thresholds in HAVING (#272).

The generator builds a structured Query first and renders SQL from it, so a failure can be shrunk structurally (see Shrink) instead of by text mutation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Name string
	Kind Kind
	Lits []string
}

Column is one generatable column. Lits are rendered SQL literals drawn from the column's actual domain; columns with no Lits appear in select lists and GROUP BY but never in predicates.

type Edge

type Edge struct {
	LeftTable, LeftCol   string
	RightTable, RightCol string
}

Edge is a joinable equality between two tables (FK pairs in practice — arbitrary column pairs explode row counts without exercising anything new).

type Gen

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

Gen is a seeded query generator.

func New

func New(seed int64, s *Schema) *Gen

New creates a generator. Identical (seed, schema) always yields the identical query sequence.

func (*Gen) Query

func (g *Gen) Query() *Query

Query generates one query.

type Kind

type Kind int

Kind classifies a column for operator/literal selection.

const (
	KindInt Kind = iota
	KindFloat
	KindString
)

type Query

type Query struct {
	Distinct bool
	Select   []string // rendered select items (aggregates already aliased)
	Tables   []string // FROM chain: Tables[0] base, rest joined via On
	On       []string // join conditions; On[i] joins Tables[i+1]
	Where    []string // conjuncts
	GroupBy  []string
	Having   string
	OrderBy  []string
	Limit    int
	Offset   int
	// LimitZero renders `LIMIT 0`. Zero is how Limit spells "no LIMIT", so
	// the one value with a rule of its own needed a field of its own (#487).
	LimitZero bool
}

Query is a structured query: rendered clause fragments plus enough structure for Shrink to remove parts and keep the query valid.

func Shrink

func Shrink(s *Schema, q *Query, fails func(*Query) bool) *Query

Shrink reduces q to a smaller query for which fails still returns true (fails = "still reproduces the divergence"). It repeatedly tries structural removals — LIMIT, ORDER BY, HAVING, DISTINCT, individual WHERE conjuncts, individual select/GROUP BY items, and unreferenced trailing joins — accepting any removal that keeps the failure, until a full pass removes nothing. The result is the minimal repro to report.

func (*Query) Clone

func (q *Query) Clone() *Query

Clone returns a deep copy of q.

func (*Query) SQL

func (q *Query) SQL() string

SQL renders the query.

type Schema

type Schema struct {
	Tables []Table
	Edges  []Edge
}

Schema is the generation universe.

type Table

type Table struct {
	Name string
	Cols []Column
}

Table is one generatable table.

Jump to

Keyboard shortcuts

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