chsql

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package chsql provides fluent Clickhouse SQL query builder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectTokens added in v0.15.0

func CollectTokens[S ~string | ~[]byte](s S, cb func(s S) bool)

CollectTokens iterates over tokens in given string.

func PutPrinter

func PutPrinter(*Printer)

PutPrinter returns Printer to pool.

Types

type Expr

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

Expr is a Clickhouse expression.

func And

func And(left, right Expr) Expr

And returns new `AND` operation.

func ArgMax added in v0.34.1

func ArgMax(arg, val Expr) Expr

ArgMax returns `argMax(<arg>, <val>)` function call expression.

func ArgMin added in v0.34.1

func ArgMin(arg, val Expr) Expr

ArgMin returns `argMin(<arg>, <val>)` function call expression.

func Array

func Array(args ...Expr) Expr

Array returns `array(<args>...)` function call expression.

func ArrayConcat

func ArrayConcat(args ...Expr) Expr

ArrayConcat returns `arrayConcat(<args>...)` function call expression.

func ArrayElement added in v0.34.1

func ArrayElement(arr, index Expr) Expr

ArrayElement returns `arrayElement(<arr>, <index>)` function call expression.

func ArrayJoin

func ArrayJoin(args ...Expr) Expr

ArrayJoin returns `arrayJoin(<args>...)` function call expression.

func Bool

func Bool(v bool) Expr

Bool returns bool literal.

func Cast added in v0.14.0

func Cast(x Expr, typ string) Expr

Cast returns `CAST(<x>, <typ>)` function call expression.

func Coalesce

func Coalesce(args ...Expr) Expr

Coalesce returns `coalesce(<args>...)` function call expression.

func ColumnEq

func ColumnEq[V litValue](column string, right V) Expr

ColumnEq returns new `=` operation on column and literal.

func Contains

func Contains(column, needle string) Expr

Contains returns boolean expression to filter strings containing needle.

func DateTime added in v0.34.1

func DateTime(t time.Time) Expr

DateTime returns an expression representing time.Time as DateTime.

func DateTime64 added in v0.32.0

func DateTime64(t time.Time, prec proto.Precision) Expr

DateTime64 returns an expression representing time.Time with given precision as DateTime64.

func Eq

func Eq(left, right Expr) Expr

Eq returns new `=` operation.

func FirstValue added in v0.34.1

func FirstValue(arg Expr) Expr

FirstValue returns `first_value(<arg>)` function call expression.

func Float

func Float[I litFloat](v I) Expr

Float returns float literal.

func Function

func Function(name string, args ...Expr) Expr

Function reutrns function call expression.

func Gt

func Gt(left, right Expr) Expr

Gt returns new `>` operation.

func Gte

func Gte(left, right Expr) Expr

Gte returns new `>=` operation.

func Has

func Has(arr, elem Expr) Expr

Has returns `has(<arr>, <elem>)` function call expression.

func HasToken

func HasToken(haystack Expr, token string) Expr

HasToken returns `hasToken(<haystack>, <token>)` function call expression.

func Hex

func Hex(arg Expr) Expr

Hex returns `hex(<arg>)` function call expression.

func Hop added in v0.34.1

func Hop(arg Expr, hopInterval, windowInterval time.Duration) Expr

Hop returns `hop(<time_attr>, <hop_interval>, <window_interval>)` function call expression.

func HopEnd added in v0.34.1

func HopEnd(arg Expr, hopInterval, windowInterval time.Duration) Expr

HopEnd returns `hopEnd(<time_attr>, <hop_interval>, <window_interval>)` function call expression.

func HopStart added in v0.34.1

func HopStart(arg Expr, hopInterval, windowInterval time.Duration) Expr

HopStart returns `hopStart(<time_attr>, <hop_interval>, <window_interval>)` function call expression.

func Ident

func Ident(tok string) Expr

Ident returns identifier.

func In

func In(left, right Expr) Expr

In returns new `IN` operation.

func InTimeRange

func InTimeRange(column string, start, end time.Time, prec proto.Precision) Expr

InTimeRange returns boolean expression to filter by [start:end].

func Integer

func Integer[I litInt](v I) Expr

Integer returns integer literal.

func Interval added in v0.34.1

func Interval(d time.Duration) Expr

Interval converts duration d to ClickHouse Interval expression with given precision.

func JSONExtract

func JSONExtract(from Expr, typ string) Expr

JSONExtract returns `JSONExtract(<from>, <typ>)` function call expression.

func JSONExtractField

func JSONExtractField(from Expr, field, typ string) Expr

JSONExtractField returns `JSONExtract(<from>, <field>, <typ>)` function call expression.

func JSONExtractKeys

func JSONExtractKeys(from Expr) Expr

JSONExtractKeys returns `JSONExtractKeys(<from>)` function call expression.

func JSONExtractString

func JSONExtractString(from Expr, field string) Expr

JSONExtractString returns `JSONExtractString(<from>, <field>)` function call expression.

func JoinAnd

func JoinAnd(args ...Expr) Expr

JoinAnd joins given expressions using AND op.

  • If len(args) == 0, returns `true` literal.
  • If len(args) == 1, returns first argument.
  • Otherwise, joins arguments with AND.

func JoinOr

func JoinOr(args ...Expr) Expr

JoinOr joins given expressions using OR op.

  • If len(args) == 0, returns `true` literal.
  • If len(args) == 1, returns first argument.
  • Otherwise, joins arguments with OR.

func LastValue added in v0.34.1

func LastValue(arg Expr) Expr

LastValue returns `last_value(<arg>)` function call expression.

func Length

func Length(arg Expr) Expr

Length returns `length(<arg>)` function call expression.

func Like

func Like(haystack, pattern Expr) Expr

Like returns `like(<haystack>, <pattern>)` function call expression.

func Lt

func Lt(left, right Expr) Expr

Lt returns new `<` operation.

func Lte

func Lte(left, right Expr) Expr

Lte returns new `<=` operation.

func Map

func Map(args ...Expr) Expr

Map returns `map(<args>...)` function call expression.

func MapConcat

func MapConcat(args ...Expr) Expr

MapConcat returns `mapConcat(<args>...)` function call expression.

func Match

func Match(haystack, pattern Expr) Expr

Match returns `match(<haystack>, <pattern>)` function call expression.

func Minus

func Minus(left, right Expr) Expr

Minus returns new `-` operation.

func Not

func Not(sub Expr) Expr

Not returns new `NOT` operation.

func NotEq

func NotEq(left, right Expr) Expr

NotEq returns new `!=` operation.

func Or

func Or(left, right Expr) Expr

Or returns new `OR` operation.

func Position

func Position(haystack, needle Expr) Expr

Position returns `position(<haystack>, <needle>)` function call expression.

func PositionUTF8

func PositionUTF8(haystack, needle Expr) Expr

PositionUTF8 returns `positionUTF8(<haystack>, <needle>)` function call expression.

func PrefixedIdent added in v0.35.0

func PrefixedIdent(prefix, tok string) Expr

PrefixedIdent return identifier with prefix.

func SimpleJSONHas

func SimpleJSONHas(json Expr, field string) Expr

SimpleJSONHas returns `simpleJSONHas(<json>, <field>)` function call expression.

func String

func String(v string) Expr

String returns string literal.

func SubQuery

func SubQuery(q Query) Expr

SubQuery returns sub-query as an expression.

func ToDateTime added in v0.34.0

func ToDateTime(arg Expr) Expr

ToDateTime returns `toDateTime(<arg>)` function call expression.

func ToDateTime64 added in v0.32.0

func ToDateTime64(arg Expr, prec proto.Precision) Expr

ToDateTime64 returns `toDateTime64(<arg>, <prec>)` function call expression.

func ToFloat64

func ToFloat64(arg Expr) Expr

ToFloat64 returns `toFloat64(<arg>)` function call expression.

func ToInt64 added in v0.34.1

func ToInt64(arg Expr) Expr

ToInt64 returns `toInt64(<arg>)` function call expression.

func ToStartOfHour added in v0.27.0

func ToStartOfHour(arg Expr) Expr

ToStartOfDay returns `toStartOfHour(<arg>)` function call expression.

func ToString

func ToString(arg Expr) Expr

ToString returns `toString(<arg>)` function call expression.

func ToUInt64 added in v0.34.1

func ToUInt64(arg Expr) Expr

ToUInt64 returns `toUInt64(<arg>)` function call expression.

func ToUnixTimestamp64Nano

func ToUnixTimestamp64Nano(arg Expr) Expr

ToUnixTimestamp64Nano returns `toUnixTimestamp64Nano(<arg>)` function call expression.

func Tumble added in v0.34.1

func Tumble(arg Expr, hopInterval time.Duration) Expr

Tumble returns `tumble(<time_attr>, <hop_interval>)` function call expression.

func TumbleEnd added in v0.34.1

func TumbleEnd(arg Expr, hopInterval time.Duration) Expr

TumbleEnd returns `tumbleEnd(<time_attr>, <hop_interval>)` function call expression.

func TumbleStart added in v0.34.1

func TumbleStart(arg Expr, hopInterval time.Duration) Expr

TumbleStart returns `tumbleStart(<time_attr>, <hop_interval>)` function call expression.

func Tuple

func Tuple(args ...Expr) Expr

Tuple returns tuple of given expressions.

func TupleValues

func TupleValues[V litValue](vals ...V) Expr

TupleValues returns tuple of given values.

func Unhex

func Unhex(arg Expr) Expr

Unhex returns `unhex(<arg>)` function call expression.

func UnixNano

func UnixNano(t time.Time) Expr

UnixNano returns time.Time as unix nano timestamp.

func Value

func Value[V litValue](v V) Expr

Value returns literal.

func (Expr) IsZero

func (e Expr) IsZero() bool

type OnResult

type OnResult = func(ctx context.Context, block proto.Block) error

OnResult defines ch.Query.OnResult callback type.

type Order

type Order uint8

Order defines sorting order.

const (
	Desc Order = iota + 1
	Asc
)

func (Order) String

func (o Order) String() string

String implements fmt.Stringer.

type Printer

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

Printer prints SQL query.

func GetPrinter

func GetPrinter() *Printer

GetPrinter creates a new Printer.

func (*Printer) And

func (p *Printer) And()

And writes `AND` ident.

func (*Printer) Asc

func (p *Printer) Asc()

Asc writes `ASC` ident.

func (*Printer) By

func (p *Printer) By()

By writes `BY` ident.

func (*Printer) CloseParen

func (p *Printer) CloseParen()

CloseParen writes a paren.

func (*Printer) Comma

func (p *Printer) Comma()

Comma writes a comma.

func (*Printer) Cross added in v0.35.0

func (p *Printer) Cross()

Cross writes `CROSS` ident.

func (*Printer) Desc

func (p *Printer) Desc()

Desc writes `DESC` ident.

func (*Printer) Distinct

func (p *Printer) Distinct()

Distinct writes `DISTINCT` ident.

func (*Printer) Dot added in v0.35.0

func (p *Printer) Dot()

Dot writes a dot.

func (*Printer) Final added in v0.30.0

func (p *Printer) Final()

Final writes `FINAL` ident.

func (*Printer) From

func (p *Printer) From()

From writes `FROM` ident.

func (*Printer) Full added in v0.35.0

func (p *Printer) Full()

Full writes `FULL` ident.

func (*Printer) Group added in v0.16.0

func (p *Printer) Group()

Group writes `GROUP` ident.

func (*Printer) Having added in v0.26.0

func (p *Printer) Having()

Having writes `HAVING` ident.

func (*Printer) Ident

func (p *Printer) Ident(tok string)

Ident writes an identifier.

func (*Printer) Inner added in v0.35.0

func (p *Printer) Inner()

Inner writes `INNER` ident.

func (*Printer) Join added in v0.35.0

func (p *Printer) Join()

Join writes `JOIN` ident.

func (*Printer) Left added in v0.35.0

func (p *Printer) Left()

Left writes `LEFT` ident.

func (*Printer) Limit

func (p *Printer) Limit()

Limit writes `LIMIT` ident.

func (*Printer) Literal

func (p *Printer) Literal(lit string)

Literal writes an literal.

func (*Printer) On added in v0.35.0

func (p *Printer) On()

On writes `ON` ident.

func (*Printer) OpenParen

func (p *Printer) OpenParen()

OpenParen writes a paren.

func (*Printer) Order

func (p *Printer) Order()

Order writes `ORDER` ident.

func (*Printer) Outer added in v0.35.0

func (p *Printer) Outer()

Outer writes `OUTER` ident.

func (*Printer) Prewhere

func (p *Printer) Prewhere()

Prewhere writes `PREWHERE` ident.

func (*Printer) Right added in v0.35.0

func (p *Printer) Right()

Right writes `RIGHT` ident.

func (*Printer) Select

func (p *Printer) Select()

Select writes `SELECT` ident.

func (*Printer) String

func (p *Printer) String() string

String returns query.

func (*Printer) Where

func (p *Printer) Where()

Where writes `WHERE` ident.

func (*Printer) With added in v0.34.1

func (p *Printer) With()

With writes `WITH` ident.

func (*Printer) WriteExpr

func (p *Printer) WriteExpr(e Expr) error

WriteExpr writes given expression.

type Query

type Query interface {
	WriteSQL(p *Printer) error
}

Query is an query interface.

type ResultColumn

type ResultColumn struct {
	Name string
	Expr Expr
	Data proto.ColResult
}

ResultColumn defines a column result.

func Column

func Column(name string, data proto.ColResult) ResultColumn

Column returns new ResultColumn.

type SelectQuery

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

SelectQuery is a SELECT query builder.

func Select

func Select(table string, columns ...ResultColumn) *SelectQuery

Select creates a new SelectQuery.

func SelectFrom

func SelectFrom(sub *SelectQuery, columns ...ResultColumn) *SelectQuery

SelectFrom creates a new SelectQuery from subquery.

func (*SelectQuery) Alias added in v0.35.0

func (q *SelectQuery) Alias(alias string) *SelectQuery

Alias sets alias for selected table.

func (*SelectQuery) Distinct

func (q *SelectQuery) Distinct(b bool) *SelectQuery

Distinct sets if query is `DISTINCT`.

func (*SelectQuery) Final added in v0.30.0

func (q *SelectQuery) Final(b bool) *SelectQuery

Final sets if query is `FINAL`.

func (*SelectQuery) GroupBy added in v0.16.0

func (q *SelectQuery) GroupBy(groups ...Expr) *SelectQuery

GroupBy adds grouping to query.

func (*SelectQuery) Having added in v0.26.0

func (q *SelectQuery) Having(filters ...Expr) *SelectQuery

Having adds grouped filters to query.

func (*SelectQuery) InnerJoin added in v0.35.0

func (q *SelectQuery) InnerJoin(table, alias string, on Expr) *SelectQuery

InnerJoin adds an INNER JOIN to query.

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(n int) *SelectQuery

Limit sets query limit.

If n is equal to or less than zero, limit is ignored.

func (*SelectQuery) Order

func (q *SelectQuery) Order(e Expr, order Order) *SelectQuery

Order adds order to query.

func (*SelectQuery) Prepare

func (q *SelectQuery) Prepare(onResult OnResult) (ch.Query, error)

Prepare builds SQL query and passes columns to ch.Query.

func (*SelectQuery) Prewhere

func (q *SelectQuery) Prewhere(filters ...Expr) *SelectQuery

Prewhere adds filters to query.

func (*SelectQuery) Results

func (q *SelectQuery) Results() (r proto.Results)

Results returns list of result columns.

func (*SelectQuery) Where

func (q *SelectQuery) Where(filters ...Expr) *SelectQuery

Where adds filters to query.

func (*SelectQuery) With added in v0.34.1

func (q *SelectQuery) With(name string, expr Expr) *SelectQuery

With adds common table expressions to a query.

func (*SelectQuery) WriteSQL

func (q *SelectQuery) WriteSQL(p *Printer) error

WriteSQL writes SQL query.

type WithColumn added in v0.34.1

type WithColumn struct {
	Name string
	Expr Expr
}

WithColumn defines a Common table expression.

Jump to

Keyboard shortcuts

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