sql

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 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 sql implements the Elasticsearch SQL API: it parses a subset of SQL SELECT, translates it to the zed query DSL and formats the result.

Index

Constants

View Source
const (
	FormatJSON = "json"
	FormatTXT  = "txt"
	FormatCSV  = "csv"
	FormatTSV  = "tsv"
)

supported response formats.

View Source
const (
	AggNone          = ""
	AggCountStar     = "count_star"
	AggCount         = "value_count"
	AggCountDistinct = "cardinality"
	AggSum           = "sum"
	AggAvg           = "avg"
	AggMin           = "min"
	AggMax           = "max"
)

aggregate kinds, named after the DSL aggregation they translate to.

View Source
const (
	GroupTerms         = "terms"
	GroupDateHistogram = "date_histogram"
	GroupHistogram     = "histogram"
)

GROUP BY kinds, named after the DSL bucket aggregation they translate to.

View Source
const DefaultFetchSize = 1000

DefaultFetchSize bounds a SELECT that carries no LIMIT.

View Source
const GroupAggName = "groupby"

GroupAggName is the name of the GROUP BY bucket aggregation.

View Source
const MaxBuckets = 1000

MaxBuckets bounds the number of groups a GROUP BY query considers.

Variables

This section is empty.

Functions

func BuildDSL

func BuildDSL(stmt *Statement, fields FieldTypes, fetchSize int) (map[string]interface{}, error)

BuildDSL translates a statement into the zed query DSL. fetchSize, when positive, caps the requested number of rows.

func BuildQuery

func BuildQuery(dsl map[string]interface{}) (*meta.ZedQuery, error)

BuildQuery converts a DSL document into the query object the search engine consumes.

func ContentType

func ContentType(format string) string

ContentType is the response content type of a format.

func ParseFormat

func ParseFormat(format, accept string) (string, error)

ParseFormat resolves the response format from the request format (body or query string) and the Accept header.

func Translate

func Translate(sql string, fetchSize int) (map[string]interface{}, error)

Translate parses sql and returns the query DSL it maps to.

Types

type Column

type Column struct {
	Label string // output name: the alias, or the canonical expression text
	Field string // source field, empty for COUNT(*)
	Agg   string // one of the Agg* constants
	Group bool   // column is the GROUP BY key
}

Column is one output column of the SELECT.

type Condition

type Condition struct {
	Label string
	Op    string
	Value float64
}

Condition is one HAVING comparison against a selected aggregate.

type Error

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

Error marks a SQL request zed cannot answer: a parse failure or an unsupported construct. Handlers render it as HTTP 400.

func (*Error) Error

func (e *Error) Error() string

type FieldTypes

type FieldTypes map[string]string

FieldTypes maps a field name to its zed mapping type. Unmapped fields resolve to the empty string and are treated as keywords.

type GroupKey

type GroupKey struct {
	Label    string
	Field    string
	Kind     string  // one of the Group* constants
	Interval string  // date_histogram interval, e.g. 1d
	Step     float64 // histogram interval
}

GroupKey is the single supported GROUP BY key.

type OrderKey

type OrderKey struct {
	Label string
	Desc  bool
}

OrderKey is one ORDER BY entry.

type Response

type Response struct {
	Columns []ResultColumn  `json:"columns"`
	Rows    [][]interface{} `json:"rows"`
}

Response is the result of a SQL query, in the Elasticsearch SQL shape.

func Query

func Query(sql string, fetchSize int) (*Response, error)

Query parses, translates and runs sql, and returns the rows it produced.

func (*Response) Render

func (r *Response) Render(format string) ([]byte, error)

Render serializes the response in the given format.

type ResultColumn

type ResultColumn struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

ResultColumn describes one output column.

type Statement

type Statement struct {
	Indexes    []string
	Star       bool // SELECT *
	Columns    []Column
	Group      *GroupKey
	Where      sqlparser.Expr
	Having     []Condition
	Order      []OrderKey
	Size       int
	Offset     int
	HasLimit   bool
	Aggregated bool
}

Statement is a parsed SELECT, reduced to what the DSL can express.

func Parse

func Parse(sql string) (*Statement, error)

Parse turns SQL text into a Statement, rejecting unsupported constructs.

Jump to

Keyboard shortcuts

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