sql

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2022 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// SQLParser is used to parse SQL statements
	SQLParser = participle.MustBuild(
		&Select{},
		participle.Lexer(sqlLexer),
		participle.CaseInsensitive("Keyword"),
		participle.CaseInsensitive("Timeword"),
	)
)

Functions

func FormatSQLTimestamp

func FormatSQLTimestamp(t time.Time) string

FormatSQLTimestamp - returns the a string representation of the timestamp as used in S3 Select

func IterToValue

func IterToValue(iter simdjson.Iter) (interface{}, error)

IterToValue converts a simdjson Iter to its underlying value. Objects are returned as simdjson.Object Arrays are returned as []interface{} with parsed values.

Types

type AliasedExpression

type AliasedExpression struct {
	Expression *Expression `parser:"@@"`
	As         string      `parser:"[ \"AS\" @Ident ]"`
}

AliasedExpression is an expression that can be optionally named

type AndCondition

type AndCondition struct {
	Condition []*Condition `parser:"@@ ( \"AND\" @@ )*"`
}

AndCondition represents logical conjunction of clauses

type Between

type Between struct {
	Not   bool     `parser:" @\"NOT\"? "`
	Start *Operand `parser:" \"BETWEEN\" @@ "`
	End   *Operand `parser:" \"AND\" @@ "`
}

Between represents the RHS of a BETWEEN expression

type Boolean

type Boolean bool

Boolean is a type for a parsed Boolean literal

func (*Boolean) Capture

func (b *Boolean) Capture(values []string) error

Capture interface used by participle

type CastFunc

type CastFunc struct {
	Expr     *Expression `parser:" \"CAST\" \"(\" @@ "`
	CastType string      `` /* 128-byte string literal not displayed */
}

CastFunc represents CAST sql function

type Compare

type Compare struct {
	Operator string   `parser:"@( \"<>\" | \"<=\" | \">=\" | \"=\" | \"<\" | \">\" | \"!=\" | \"IS\" \"NOT\" | \"IS\")"`
	Operand  *Operand `parser:"  @@"`
}

Compare represents the RHS of a comparison expression

type Condition

type Condition struct {
	Operand *ConditionOperand `parser:"  @@"`
	Not     *Condition        `parser:"| \"NOT\" @@"`
}

Condition represents a negation or a condition operand

type ConditionOperand

type ConditionOperand struct {
	Operand      *Operand      `parser:"@@"`
	ConditionRHS *ConditionRHS `parser:"@@?"`
}

ConditionOperand is a operand followed by an an optional operation expression

type ConditionRHS

type ConditionRHS struct {
	Compare *Compare `parser:"  @@"`
	Between *Between `parser:"| @@"`
	In      *In      `parser:"| \"IN\" @@"`
	Like    *Like    `parser:"| @@"`
}

ConditionRHS represents the right-hand-side of Compare, Between, In or Like expressions.

type CountFunc

type CountFunc struct {
	StarArg bool        `parser:" \"COUNT\" \"(\" ( @\"*\"?"`
	ExprArg *Expression `parser:" @@? )! \")\""`
}

CountFunc represents the COUNT sql function

type DateAddFunc

type DateAddFunc struct {
	DatePart  string       `` /* 161-byte string literal not displayed */
	Quantity  *Operand     `parser:" @@ \",\""`
	Timestamp *PrimaryTerm `parser:" @@ \")\""`
}

DateAddFunc represents the DATE_ADD function

type DateDiffFunc

type DateDiffFunc struct {
	DatePart   string       `` /* 163-byte string literal not displayed */
	Timestamp1 *PrimaryTerm `parser:" @@ \",\" "`
	Timestamp2 *PrimaryTerm `parser:" @@ \")\" "`
}

DateDiffFunc represents the DATE_DIFF function

type Expression

type Expression struct {
	And []*AndCondition `parser:"@@ ( \"OR\" @@ )*"`
}

Expression represents a logical disjunction of clauses

type ExtractFunc

type ExtractFunc struct {
	Timeword string       `` /* 215-byte string literal not displayed */
	From     *PrimaryTerm `parser:" \"FROM\" @@ \")\" "`
}

ExtractFunc represents EXTRACT sql function

type FuncExpr

type FuncExpr struct {
	SFunc     *SimpleArgFunc `parser:"  @@"`
	Count     *CountFunc     `parser:"| @@"`
	Cast      *CastFunc      `parser:"| @@"`
	Substring *SubstringFunc `parser:"| @@"`
	Extract   *ExtractFunc   `parser:"| @@"`
	Trim      *TrimFunc      `parser:"| @@"`
	DateAdd   *DateAddFunc   `parser:"| @@"`
	DateDiff  *DateDiffFunc  `parser:"| @@"`
	// contains filtered or unexported fields
}

FuncExpr represents a function call

type FuncName

type FuncName string

FuncName - SQL function name.

type Identifier

type Identifier struct {
	Unquoted *string           `parser:"  @Ident"`
	Quoted   *QuotedIdentifier `parser:"| @QuotIdent"`
}

Identifier represents a parsed identifier

func (*Identifier) String

func (i *Identifier) String() string

String removes double quotes in quoted identifiers

type In

type In struct {
	ListExpression *Expression `parser:"@@ "`
}

In represents the RHS of an IN expression

type JSONPath

type JSONPath struct {
	BaseKey  *Identifier        `parser:" @@"`
	PathExpr []*JSONPathElement `parser:"(@@)*"`
	// contains filtered or unexported fields
}

JSONPath represents a keypath. Instances should be treated idempotent and not change once created.

func (*JSONPath) String

func (e *JSONPath) String() string

String - returns the JSONPath representation

func (*JSONPath) StripTableAlias

func (e *JSONPath) StripTableAlias(tableAlias string) []*JSONPathElement

StripTableAlias removes a table alias from the path. The result is also cached for repeated lookups during SQL query evaluation.

type JSONPathElement

type JSONPathElement struct {
	Key            *ObjectKey `parser:"  @@"`               // ['name'] and .name forms
	Index          *int       `parser:"| \"[\" @Int \"]\""` // [3] form
	ObjectWildcard bool       `parser:"| @\".*\""`          // .* form
	ArrayWildcard  bool       `parser:"| @\"[*]\""`         // [*] form
}

JSONPathElement represents a keypath component

func (*JSONPathElement) String

func (e *JSONPathElement) String() string

type Like

type Like struct {
	Not        bool     `parser:" @\"NOT\"? "`
	Pattern    *Operand `parser:" \"LIKE\" @@ "`
	EscapeChar *Operand `parser:" (\"ESCAPE\" @@)? "`
}

Like represents the RHS of a LIKE expression

type ListExpr

type ListExpr struct {
	Elements []*Expression `parser:"\"(\" @@ ( \",\" @@ )* \")\" | \"[\" @@ ( \",\" @@ )* \"]\""`
}

ListExpr represents a literal list with elements as expressions.

type LitValue

type LitValue struct {
	Float   *float64       `parser:"(  @Float"`
	Int     *float64       `parser:" | @Int"` // To avoid value out of range, use float64 instead
	String  *LiteralString `parser:" | @LitString"`
	Boolean *Boolean       `parser:" | @(\"TRUE\" | \"FALSE\")"`
	Null    bool           `parser:" | @\"NULL\""`
	Missing bool           `parser:" | @\"MISSING\")"`
}

LitValue represents a literal value parsed from the sql

type LiteralList

type LiteralList []string

LiteralList is a type for parsed SQL lists literals

func (*LiteralList) Capture

func (ls *LiteralList) Capture(values []string) error

Capture interface used by participle

type LiteralString

type LiteralString string

LiteralString is a type for parsed SQL string literals

func (*LiteralString) Capture

func (ls *LiteralString) Capture(values []string) error

Capture interface used by participle

type Missing

type Missing struct{}

Missing is used to indicate a non-existing value.

type MultOp

type MultOp struct {
	Left  *UnaryTerm     `parser:"@@"`
	Right []*OpUnaryTerm `parser:"(@@)*"`
}

MultOp represents a single term followed by an optional sequence of terms separated by *, / or % operators.

type NegatedTerm

type NegatedTerm struct {
	Term *PrimaryTerm `parser:"\"-\" @@"`
}

NegatedTerm has a leading minus sign.

type ObjectKey

type ObjectKey struct {
	Lit *LiteralString `parser:" \"[\" @LitString \"]\""`
	ID  *Identifier    `parser:"| \".\" @@"`
}

ObjectKey is a type for parsed strings occurring in key paths

func (*ObjectKey) String

func (o *ObjectKey) String() string

type OpFactor

type OpFactor struct {
	Op    string  `parser:"@(\"+\" | \"-\")"`
	Right *MultOp `parser:"@@"`
}

OpFactor represents the right-side of a +/- operation.

type OpUnaryTerm

type OpUnaryTerm struct {
	Op    string     `parser:"@(\"*\" | \"/\" | \"%\")"`
	Right *UnaryTerm `parser:"@@"`
}

OpUnaryTerm represents the right side of *, / or % binary operations.

type Operand

type Operand struct {
	Left  *MultOp     `parser:"@@"`
	Right []*OpFactor `parser:"(@@)*"`
}

An Operand is a single term followed by an optional sequence of terms separated by +/-

type PrimaryTerm

type PrimaryTerm struct {
	Value         *LitValue   `parser:"  @@"`
	JPathExpr     *JSONPath   `parser:"| @@"`
	ListExpr      *ListExpr   `parser:"| @@"`
	SubExpression *Expression `parser:"| \"(\" @@ \")\""`
	// Include function expressions here.
	FuncCall *FuncExpr `parser:"| @@"`
}

PrimaryTerm represents a Value, Path expression, a Sub-expression or a function call.

type QuotedIdentifier

type QuotedIdentifier string

QuotedIdentifier is a type for parsed strings that are double quoted.

func (*QuotedIdentifier) Capture

func (qi *QuotedIdentifier) Capture(values []string) error

Capture interface used by participle

type Record

type Record interface {
	Get(name string) (*Value, error)

	// Set a value.
	// Can return a different record type.
	Set(name string, value *Value) (Record, error)
	WriteCSV(writer io.Writer, opts WriteCSVOpts) error
	WriteJSON(writer io.Writer) error

	// Clone the record and if possible use the destination provided.
	Clone(dst Record) Record
	Reset()

	// Returns underlying representation
	Raw() (SelectObjectFormat, interface{})

	// Replaces the underlying data
	Replace(k interface{}) error
}

Record - is a type containing columns and their values.

type Select

type Select struct {
	Expression *SelectExpression `parser:"\"SELECT\" @@"`
	From       *TableExpression  `parser:"\"FROM\" @@"`
	Where      *Expression       `parser:"( \"WHERE\" @@ )?"`
	Limit      *LitValue         `parser:"( \"LIMIT\" @@ )?"`
}

Select is the top level AST node type

type SelectExpression

type SelectExpression struct {
	All         bool                 `parser:"  @\"*\""`
	Expressions []*AliasedExpression `parser:"| @@ { \",\" @@ }"`
}

SelectExpression represents the items requested in the select statement

type SelectObjectFormat

type SelectObjectFormat int

SelectObjectFormat specifies the format of the underlying data

const (
	// SelectFmtUnknown - unknown format (default value)
	SelectFmtUnknown SelectObjectFormat = iota
	// SelectFmtCSV - CSV format
	SelectFmtCSV
	// SelectFmtJSON - JSON format
	SelectFmtJSON
	// SelectFmtSIMDJSON - SIMD JSON format
	SelectFmtSIMDJSON
	// SelectFmtParquet - Parquet format
	SelectFmtParquet
)

type SelectStatement

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

SelectStatement is the top level parsed and analyzed structure

func ParseSelectStatement

func ParseSelectStatement(s string) (stmt SelectStatement, err error)

ParseSelectStatement - parses a select query from the given string and analyzes it.

func (*SelectStatement) AggregateResult

func (e *SelectStatement) AggregateResult(output Record) error

AggregateResult - returns the aggregated result after all input records have been processed. Applies only to aggregation queries.

func (*SelectStatement) AggregateRow

func (e *SelectStatement) AggregateRow(input Record) error

AggregateRow - aggregates the input record. Applies only to aggregation queries.

func (*SelectStatement) Eval

func (e *SelectStatement) Eval(input, output Record) (Record, error)

Eval - evaluates the Select statement for the given record. It applies only to non-aggregation queries. The function returns whether the statement passed the WHERE clause and should be outputted.

func (*SelectStatement) EvalFrom

func (e *SelectStatement) EvalFrom(format string, input Record) ([]*Record, error)

EvalFrom evaluates the From clause on the input record. It only applies to JSON input data format (currently).

func (*SelectStatement) IsAggregated

func (e *SelectStatement) IsAggregated() bool

IsAggregated returns if the statement involves SQL aggregation

func (*SelectStatement) LimitReached

func (e *SelectStatement) LimitReached() bool

LimitReached - returns true if the number of records output has reached the value of the `LIMIT` clause.

type SimpleArgFunc

type SimpleArgFunc struct {
	FunctionName string `` /* 193-byte string literal not displayed */

	ArgsList []*Expression `parser:"\"(\" (@@ (\",\" @@)*)?\")\""`
}

SimpleArgFunc represents functions with simple expression arguments.

type SubstringFunc

type SubstringFunc struct {
	Expr *PrimaryTerm `parser:" \"SUBSTRING\" \"(\" @@ "`
	From *Operand     `parser:" ( \"FROM\" @@ "`
	For  *Operand     `parser:"   (\"FOR\" @@)? \")\" "`
	Arg2 *Operand     `parser:" | \",\" @@ "`
	Arg3 *Operand     `parser:"   (\",\" @@)? \")\" )"`
}

SubstringFunc represents SUBSTRING sql function

type TableExpression

type TableExpression struct {
	Table *JSONPath `parser:"@@"`
	As    string    `parser:"( \"AS\"? @Ident )?"`
}

TableExpression represents the FROM clause

func (*TableExpression) HasKeypath

func (from *TableExpression) HasKeypath() bool

HasKeypath returns if the from clause has a key path - e.g. S3object[*].id

type TrimFunc

type TrimFunc struct {
	TrimWhere *string      `parser:" \"TRIM\" \"(\" ( @( \"LEADING\" | \"TRAILING\" | \"BOTH\" ) "`
	TrimChars *PrimaryTerm `parser:"             @@?  "`
	TrimFrom  *PrimaryTerm `parser:"             \"FROM\" )? @@ \")\" "`
}

TrimFunc represents TRIM sql function

type UnaryTerm

type UnaryTerm struct {
	Negated *NegatedTerm `parser:"  @@"`
	Primary *PrimaryTerm `parser:"| @@"`
}

UnaryTerm represents a single negated term or a primary term

type Value

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

Value represents a value of restricted type reduced from an expression represented by an ASTNode. Only one of the fields is non-nil.

In cases where we are fetching data from a data source (like csv), the type may not be determined yet. In these cases, a byte-slice is used.

func FromArray

func FromArray(a []Value) *Value

FromArray creates a Value from an array of values.

func FromBool

func FromBool(b bool) *Value

FromBool creates a Value from a bool

func FromBytes

func FromBytes(b []byte) *Value

FromBytes creates a Value from a []byte

func FromFloat

func FromFloat(f float64) *Value

FromFloat creates a Value from a number

func FromInt

func FromInt(f int64) *Value

FromInt creates a Value from an int

func FromMissing

func FromMissing() *Value

FromMissing creates a Value with Missing value

func FromNull

func FromNull() *Value

FromNull creates a Value with Null value

func FromString

func FromString(str string) *Value

FromString creates a Value from a string

func FromTimestamp

func FromTimestamp(t time.Time) *Value

FromTimestamp creates a Value from a timestamp

func (Value) CSVString

func (v Value) CSVString() string

CSVString - convert to string for CSV serialization

func (Value) Equals

func (v Value) Equals(b Value) (ok bool)

Equals returns whether the values strictly match. Both type and value must match.

func (Value) GetTypeString

func (v Value) GetTypeString() string

GetTypeString returns a string representation for vType

func (*Value) InferBytesType

func (v *Value) InferBytesType() (err error)

InferBytesType will attempt to infer the data type of bytes. Will fail if value type is not bytes or it would result in invalid utf8. ORDER: int, float, bool, JSON (object or array), timestamp, string If the content is valid JSON, the type will still be bytes.

func (Value) IsArray

func (v Value) IsArray() (ok bool)

IsArray returns whether the value is an array.

func (Value) IsMissing

func (v Value) IsMissing() bool

IsMissing - checks if value is missing.

func (Value) IsNull

func (v Value) IsNull() bool

IsNull - checks if value is missing.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON provides json marshaling of values.

func (Value) Repr

func (v Value) Repr() string

Repr returns a string representation of value.

func (Value) SameTypeAs

func (v Value) SameTypeAs(b Value) (ok bool)

SameTypeAs return whether the two types are strictly the same.

func (Value) String

func (v Value) String() string

func (Value) ToArray

func (v Value) ToArray() (val []Value, ok bool)

ToArray returns the value if it is a slice of values.

func (Value) ToBool

func (v Value) ToBool() (val bool, ok bool)

ToBool returns the bool value; second return value refers to if the bool conversion succeeded.

func (Value) ToBytes

func (v Value) ToBytes() (val []byte, ok bool)

ToBytes returns the value if byte-slice.

func (Value) ToFloat

func (v Value) ToFloat() (val float64, ok bool)

ToFloat works for int and float values

func (Value) ToInt

func (v Value) ToInt() (val int64, ok bool)

ToInt returns the value if int.

func (Value) ToString

func (v Value) ToString() (val string, ok bool)

ToString returns the value if string.

func (Value) ToTimestamp

func (v Value) ToTimestamp() (t time.Time, ok bool)

ToTimestamp returns the timestamp value if present.

type WriteCSVOpts

type WriteCSVOpts struct {
	FieldDelimiter rune
	Quote          rune
	QuoteEscape    rune
	AlwaysQuote    bool
}

WriteCSVOpts - encapsulates options for Select CSV output

Jump to

Keyboard shortcuts

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