sql

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: Apache-2.0, MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sql provides SQL query parsing and execution for DataFrames

Index

Constants

View Source
const (
	AscOrder  = "ASC"
	DescOrder = "DESC"
)

Sort directions.

View Source
const (
	CountFunction = "COUNT"
	SumFunction   = "SUM"
	AvgFunction   = "AVG"
	MinFunction   = "MIN"
	MaxFunction   = "MAX"
)

Aggregation functions.

View Source
const (
	UpperFunction  = "UPPER"
	LowerFunction  = "LOWER"
	LengthFunction = "LENGTH"
	TrimFunction   = "TRIM"
	SubstrFunction = "SUBSTR"
)

String functions.

View Source
const (
	AbsFunction   = "ABS"
	RoundFunction = "ROUND"
	FloorFunction = "FLOOR"
	CeilFunction  = "CEIL"
)

Math functions.

View Source
const (
	NowFunction      = "NOW"
	DateAddFunction  = "DATE_ADD"
	DateSubFunction  = "DATE_SUB"
	DateDiffFunction = "DATE_DIFF"
	ExtractFunction  = "EXTRACT"
)

Date functions.

View Source
const (
	LOWEST      int
	LOGICAL     // AND, OR
	EQUALS      // ==
	LESSGREATER // > or <
	SUMPREC     // + (precedence)
	PRODUCT     // *
	PREFIX      // -X or !X
	CALL        // myFunction(X)
)

Precedence constants for expression parsing.

View Source
const (
	// OffsetOnlyLimit is a special value used in LimitClause.Count to indicate
	// that this is an OFFSET-only query (no LIMIT constraint).
	OffsetOnlyLimit = -1
)

Constants for LIMIT clause handling.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor added in v0.3.1

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

Executor executes SQL queries against registered DataFrames.

func NewExecutor added in v0.3.1

func NewExecutor(mem memory.Allocator) *Executor

NewExecutor creates a new SQL executor.

type FromClause

type FromClause struct {
	TableName string
	Alias     string
	Joins     []JoinClause
}

FromClause represents the FROM clause with table references.

func (*FromClause) String

func (f *FromClause) String() string

type Function added in v0.3.1

type Function struct {
	Name string
	Args []expr.Expr
}

Function represents a SQL function call.

func (*Function) String added in v0.3.1

func (f *Function) String() string

type GroupByClause

type GroupByClause struct {
	Columns []expr.Expr
}

GroupByClause represents the GROUP BY clause.

func (*GroupByClause) String

func (g *GroupByClause) String() string

type HavingClause

type HavingClause struct {
	Condition expr.Expr
}

HavingClause represents the HAVING clause.

func (*HavingClause) String

func (h *HavingClause) String() string

type JoinClause

type JoinClause struct {
	Type      JoinType
	TableName string
	Alias     string
	OnClause  expr.Expr
}

JoinClause represents a JOIN clause.

func (*JoinClause) String

func (j *JoinClause) String() string

type JoinType

type JoinType int

JoinType represents different types of joins.

const (
	InnerJoinType JoinType = iota
	LeftJoinType
	RightJoinType
	FullJoinType
)

func (JoinType) String

func (jt JoinType) String() string

type Lexer

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

Lexer tokenizes SQL input.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer instance.

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken scans the input and returns the next token.

type LimitClause

type LimitClause struct {
	Count  int64
	Offset int64
}

LimitClause represents the LIMIT clause.

func (*LimitClause) String

func (l *LimitClause) String() string

type OrderByClause

type OrderByClause struct {
	OrderItems []OrderByItem
}

OrderByClause represents the ORDER BY clause.

func (*OrderByClause) String

func (o *OrderByClause) String() string

type OrderByItem

type OrderByItem struct {
	Expression expr.Expr
	Direction  OrderDirection
}

OrderByItem represents an item in the ORDER BY clause.

func (*OrderByItem) String

func (o *OrderByItem) String() string

type OrderDirection

type OrderDirection int

OrderDirection represents sort direction.

const (
	AscendingOrder OrderDirection = iota
	DescendingOrder
)

func (OrderDirection) String

func (od OrderDirection) String() string

type Parser

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

Parser parses SQL tokens into AST.

func NewParser

func NewParser(lexer *Lexer) *Parser

NewParser creates a new parser instance.

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns parse errors.

func (*Parser) ParseSQL

func (p *Parser) ParseSQL() Statement

ParseSQL parses a SQL statement.

type Query added in v0.3.1

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

Query represents a prepared SQL query for reuse.

func (*Query) Execute added in v0.3.1

func (q *Query) Execute() (*dataframe.DataFrame, error)

Execute executes a prepared SQL query.

func (*Query) String added in v0.3.1

func (q *Query) String() string

String returns the SQL query string.

type SQLExecutor

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

SQLExecutor is deprecated, use Executor instead.

func NewSQLExecutor

func NewSQLExecutor(mem memory.Allocator) *SQLExecutor

NewSQLExecutor creates a new SQL executor. Deprecated: Use NewExecutor instead.

func (*SQLExecutor) BatchExecute

func (e *SQLExecutor) BatchExecute(queries []string) ([]*dataframe.DataFrame, error)

BatchExecute executes multiple SQL statements in sequence.

func (*SQLExecutor) ClearTables

func (e *SQLExecutor) ClearTables()

ClearTables removes all registered tables.

func (*SQLExecutor) Execute

func (e *SQLExecutor) Execute(query string) (*dataframe.DataFrame, error)

Execute executes a SQL query and returns the result DataFrame.

func (*SQLExecutor) Explain

func (e *SQLExecutor) Explain(query string) (string, error)

Explain returns the execution plan for a SQL query.

func (*SQLExecutor) GetRegisteredTables

func (e *SQLExecutor) GetRegisteredTables() []string

GetRegisteredTables returns the list of registered table names.

func (*SQLExecutor) PrepareQuery

func (e *SQLExecutor) PrepareQuery(query string) (*Query, error)

PrepareQuery prepares a SQL query for multiple executions.

func (*SQLExecutor) RegisterTable

func (e *SQLExecutor) RegisterTable(name string, df *dataframe.DataFrame)

RegisterTable registers a DataFrame with a table name.

func (*SQLExecutor) ValidateQuery

func (e *SQLExecutor) ValidateQuery(query string) error

ValidateQuery validates a SQL query without executing it.

type SQLTranslator

type SQLTranslator struct {
	*sqlutil.BaseTranslator
	// contains filtered or unexported fields
}

SQLTranslator translates SQL AST to DataFrame operations.

func NewSQLTranslator

func NewSQLTranslator(mem memory.Allocator) *SQLTranslator

NewSQLTranslator creates a new SQL translator.

func (*SQLTranslator) ClearTables

func (t *SQLTranslator) ClearTables()

ClearTables removes all registered tables.

func (*SQLTranslator) GetRegisteredTables

func (t *SQLTranslator) GetRegisteredTables() []string

GetRegisteredTables returns the list of registered table names.

func (*SQLTranslator) TranslateFunctionCall

func (t *SQLTranslator) TranslateFunctionCall(fn *Function) (expr.Expr, error)

TranslateFunctionCall translates SQL function calls to Gorilla expressions.

func (*SQLTranslator) TranslateStatement

func (t *SQLTranslator) TranslateStatement(stmt Statement) (*dataframe.LazyFrame, error)

TranslateStatement translates a SQL statement to a LazyFrame.

func (*SQLTranslator) ValidateSQLSyntax

func (t *SQLTranslator) ValidateSQLSyntax(stmt Statement) error

ValidateSQLSyntax performs basic validation of SQL statement.

type SelectItem

type SelectItem struct {
	Expression expr.Expr
	Alias      string
	IsWildcard bool
}

SelectItem represents an item in the SELECT list.

func (*SelectItem) String

func (s *SelectItem) String() string

type SelectStatement

type SelectStatement struct {
	SelectList    []SelectItem
	FromClause    *FromClause
	WhereClause   *WhereClause
	GroupByClause *GroupByClause
	HavingClause  *HavingClause
	OrderByClause *OrderByClause
	LimitClause   *LimitClause
}

SelectStatement represents a SQL SELECT statement.

func (*SelectStatement) StatementType

func (s *SelectStatement) StatementType() StatementType

func (*SelectStatement) String

func (s *SelectStatement) String() string

type Statement added in v0.3.1

type Statement interface {
	StatementType() StatementType
	String() string
}

Statement interface for all SQL statement types.

func ParseSQL

func ParseSQL(input string) (Statement, error)

ParseSQL is the main entry point for SQL parsing.

type StatementType added in v0.3.1

type StatementType int

StatementType represents the type of SQL statement.

const (
	SelectStatementType StatementType = iota
	InsertStatementType
	UpdateStatementType
	DeleteStatementType
)

type Token

type Token struct {
	Type     TokenType
	Literal  string
	Position int
}

Token represents a single SQL token.

type TokenType

type TokenType int

TokenType represents the type of a SQL token.

const (
	// EOF represents end of file token.
	EOF TokenType = iota
	ILLEGAL

	// IDENT represents identifier tokens like column names, table names, function names.
	IDENT
	INT    // integers
	FLOAT  // floating point numbers
	STRING // string literals

	// SELECT represents the SELECT keyword.
	SELECT
	FROM
	WHERE
	GROUP
	BY
	HAVING
	ORDER
	LIMIT
	OFFSET
	AS
	AND
	OR
	NOT
	IN
	EXISTS
	NULL
	TRUE
	FALSE
	JOIN
	INNER
	LEFT
	RIGHT
	FULL
	ON
	COUNT
	SUM
	AVG
	MIN
	MAX
	DISTINCT

	// EQ represents the equals operator (=).
	EQ
	NE    // != or <>
	LT    // <
	LE    // <=
	GT    // >
	GE    // >=
	PLUS  // +
	MINUS // -
	MULT  // *
	DIV   // /
	MOD   // %

	// COMMA represents the comma delimiter (,).
	COMMA
	SEMICOLON // ;
	LPAREN    // (
	RPAREN    // )
	DOT       // .
)

type WhereClause

type WhereClause struct {
	Condition expr.Expr
}

WhereClause represents the WHERE clause.

func (*WhereClause) String

func (w *WhereClause) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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