sql

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: Apache-2.0, MIT Imports: 7 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

Variables

This section is empty.

Functions

This section is empty.

Types

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 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() SQLStatement

ParseSQL parses a SQL statement

type SQLExecutor

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

SQLExecutor executes SQL queries against registered DataFrames

func NewSQLExecutor

func NewSQLExecutor(mem memory.Allocator) *SQLExecutor

NewSQLExecutor creates a new SQL executor

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) (*SQLQuery, 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 SQLFunction

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

SQLFunction represents a SQL function call

func (*SQLFunction) String

func (f *SQLFunction) String() string

type SQLQuery

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

SQLQuery represents a prepared SQL query for reuse

func (*SQLQuery) Execute

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

Execute executes a prepared SQL query

func (*SQLQuery) String

func (q *SQLQuery) String() string

String returns the SQL query string

type SQLStatement

type SQLStatement interface {
	StatementType() SQLStatementType
	String() string
}

SQLStatement interface for all SQL statement types

func ParseSQL

func ParseSQL(input string) (SQLStatement, error)

ParseSQL is the main entry point for SQL parsing

type SQLStatementType

type SQLStatementType int

SQLStatementType represents the type of SQL statement

const (
	SelectStatementType SQLStatementType = iota
	InsertStatementType
	UpdateStatementType
	DeleteStatementType
)

type SQLTranslator

type SQLTranslator struct {
	// 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) RegisterTable

func (t *SQLTranslator) RegisterTable(name string, df *dataframe.DataFrame)

RegisterTable registers a DataFrame with a table name for SQL queries

func (*SQLTranslator) TranslateFunctionCall

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

TranslateFunctionCall translates SQL function calls to Gorilla expressions

func (*SQLTranslator) TranslateStatement

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

TranslateStatement translates a SQL statement to a LazyFrame

func (*SQLTranslator) ValidateSQLSyntax

func (t *SQLTranslator) ValidateSQLSyntax(stmt SQLStatement) 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() SQLStatementType

func (*SelectStatement) String

func (s *SelectStatement) String() string

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 (
	// Special tokens
	EOF TokenType = iota
	ILLEGAL

	// Literals
	IDENT  // column names, table names, function names
	INT    // integers
	FLOAT  // floating point numbers
	STRING // string literals

	// Keywords
	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

	// Operators
	EQ    // =
	NE    // != or <>
	LT    // <
	LE    // <=
	GT    // >
	GE    // >=
	PLUS  // +
	MINUS // -
	MULT  // *
	DIV   // /
	MOD   // %

	// Delimiters
	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

Jump to

Keyboard shortcuts

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