jepl

package module
v0.0.0-...-95b31cb Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2017 License: GPL-3.0 Imports: 12 Imported by: 1

README

Introduction

JSON Event Process Language is a SQL-like query language providing features specific to process json event and generate time series data.

The source code is based on Influxql.

The source code is More concise and focused.

Notation

The syntax is specified using Extended Backus-Naur Form ("EBNF").

Notation operators in order of increasing precedence:

|   alternation
()  grouping
[]  option (0 or 1 times)
{}  repetition (0 to n times)

Query representation

Characters

Jepl is Unicode text encoded in UTF-8.

newline             = /* the Unicode code point U+000A */ .
unicode_char        = /* an arbitrary Unicode code point except newline */ .

Letters and digits

Letters are the set of ASCII characters plus the underscore character _ (U+005F) is considered a letter.

Only decimal digits are supported.

letter              = ascii_letter | "_" .
ascii_letter        = "A" … "Z" | "a" … "z" .
digit               = "0" … "9" .

Identifiers

Identifiers are tokens which refer to topic names, field keys.

The rules:

  • must start with an upper or lowercase ASCII character or "_"
  • may contain only ASCII letters, decimal digits, and "_"
identifier          = ( letter ) { letter | digit }
Examples:
cpu
_cpu_stats

Keywords

ALL           AS            NI         IN
SELECT        WHERE         FROM       AND
OR

Literals

Integers

Jepl supports decimal integer literals. Hexadecimal and octal literals are not currently supported.

int_lit             = ( "1" … "9" ) { digit }
Floats

Jepl supports floating-point literals. Exponents are not currently supported.

float_lit           = int_lit "." int_lit
Strings

String literals must be surrounded by single quotes or double quotes. Strings may contain ' or " characters as long as they are escaped (i.e., \', \").

string_lit          = (`'` { unicode_char } `'`) | (`"` { unicode_char } `"`)
Booleans
bool_lit            = TRUE | FALSE
Regular Expressions
regex_lit           = "/" { unicode_char } "/"

Comparators: =~ matches against !~ doesn't match against

Statement

statement        = select_stmt
SELECT
select_stmt      = "SELECT" fields [from_clause] [ where_clause ] [ group_by_clause ]
Fields
fields           = field { "," field }

field            = metric_expr [ alias ]

alias            = "AS" identifier

metric_expr      = metric_term { "+" | "-"  metric_term }

metric_term      = metric_factor { "*" | "/" metric_factor }

metric_factor    =  int_lit | float_lit | func "(" arg_expr ")"

func             = "SUM" | "COUNT" | "MAX" | "MIN" | "AVG"

Metric Argument Expression
arg_expr         =  arg_term { "+" | "-"  arg_term }

arg_term         = arg_factor { "*" | "/" arg_factor }

arg_factor       = int_lit | float_lit | var_ref | "(" arg_expr ")"
Clauses
from_clause      = "FROM" identifier

where_clause     = "WHERE" cond_expr

group_by_clause = "GROUP BY" dimensions
Where Condition Expression
cond_expr        = unary_expr { binary_op unary_expr }

unary_expr       = "(" cond_expr ")" | var_ref | literal | list

binary_op        = "+" | "-" | "*" | "/" | "AND" | "OR" | "=" | "!=" | "<>" | "<" | "<=" | ">" | ">=" | "!~" | "=~" | "NI" | "IN"

var_ref          = identifier { "." identifier}

list             = "[" literal { "," literal } "]"

literal          = string_lit | int_lit | float_lit | bool_lit | regex_lit

Group By Dimensions
dimensions       = var_ref { "," var_ref }
Examples:
SELECT sum(tcp.bytes_in+tcp.bytes_out) AS total_bytes FROM packetbeat WHERE uid = 1 AND tcp.src_ip = '127.0.0.1' GROUP BY tcp.dst_ip

Documentation

Index

Constants

View Source
const (
	// Unknown primitive data type.
	Unknown DataType = 0
	// Float means the data type is a float
	Float = 1
	// Integer means the data type is a integer
	Integer = 2
	// String means the data type is a string of text.
	String = 3
	// Boolean means the data type is a boolean.
	Boolean = 4
	// AnyField means the data type is any field.
	AnyField = 5
)

Variables

View Source
var (
	// ErrInvalidTime is returned when the timestamp string used to
	// compare against time field is invalid.
	ErrInvalidTime = errors.New("invalid timestamp string")
)

Functions

func BinaryExprName

func BinaryExprName(expr *BinaryExpr) string

BinaryExprName returns the name of a binary expression by concatenating the variables in the binary expression with underscores.

func ContainsVarRef

func ContainsVarRef(expr Expr) bool

ContainsVarRef returns true if expr is a VarRef or contains one.

func Eval

func Eval(expr Expr, js *string) interface{}

Eval evaluates expr against a map.

func EvalBool

func EvalBool(expr Expr, js *string) bool

EvalBool evaluates expr and returns true if result is a boolean true. Otherwise returns false.

func EvalSQL

func EvalSQL(sql string, docs []string) map[string]Points

EvalSQL return metric points map[filter]metric

func IdentNeedsQuotes

func IdentNeedsQuotes(ident string) bool

IdentNeedsQuotes returns true if the ident string given would require quotes.

func IsListOp

func IsListOp(t Token) bool

IsListOp returns true if the operator accepts a list operand.

func IsRegexOp

func IsRegexOp(t Token) bool

IsRegexOp returns true if the operator accepts a regex operand.

func MatchSource

func MatchSource(sources Sources, name string) string

MatchSource returns the source name that matches a field name. Returns a blank string if no sources match.

func QuoteIdent

func QuoteIdent(segments ...string) string

QuoteIdent returns a quoted identifier from multiple bare identifiers.

func QuoteString

func QuoteString(s string) string

QuoteString returns a quoted string.

func ScanBareIdent

func ScanBareIdent(r io.RuneScanner) string

ScanBareIdent reads bare identifier from a rune reader.

func ScanDelimited

func ScanDelimited(r io.RuneScanner, start, end rune, escapes map[rune]rune, escapesPassThru bool) ([]byte, error)

ScanDelimited reads a delimited set of runes

func ScanString

func ScanString(r io.RuneScanner) (string, error)

ScanString reads a quoted string from a rune reader.

func Walk

func Walk(v Visitor, node Node)

Walk traverses a node hierarchy in depth-first order.

func WalkFunc

func WalkFunc(node Node, fn func(Node))

WalkFunc traverses a node hierarchy in depth-first order.

Types

type BinaryExpr

type BinaryExpr struct {
	Op  Token
	LHS Expr
	RHS Expr
}

BinaryExpr represents an operation between two expressions.

func (*BinaryExpr) String

func (e *BinaryExpr) String() string

String returns a string representation of the binary expression.

type BooleanLiteral

type BooleanLiteral struct {
	Val bool
}

BooleanLiteral represents a boolean literal.

func (*BooleanLiteral) String

func (l *BooleanLiteral) String() string

String returns a string representation of the literal.

type Call

type Call struct {
	Name string
	Args []Expr // must hava not funcCall expr

	First bool
	Count int
	// contains filtered or unexported fields
}

Call represents a function call.

func (*Call) Fields

func (c *Call) Fields() []string

Fields will extract any field names from the call. Only specific calls support this.

func (*Call) String

func (c *Call) String() string

String returns a string representation of the call.

type DataType

type DataType int

DataType represents the primitive data types available in InfluxQL.

func InspectDataType

func InspectDataType(v interface{}) DataType

InspectDataType returns the data type of a given value.

func InspectDataTypes

func InspectDataTypes(a []interface{}) []DataType

InspectDataTypes returns all of the data types for an interface slice.

func (DataType) String

func (d DataType) String() string

type Dimension

type Dimension struct {
	Expr Expr
}

Dimension represents an expression that a select statement is grouped by.

func (*Dimension) String

func (d *Dimension) String() string

String returns a string representation of the dimension.

type Dimensions

type Dimensions []*Dimension

Dimensions represents a list of dimensions.

func (Dimensions) String

func (a Dimensions) String() string

String returns a string representation of the dimensions.

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr represents an expression that can be evaluated to a value.

func CloneExpr

func CloneExpr(expr Expr) Expr

CloneExpr returns a deep copy of the expression.

type Field

type Field struct {
	Expr  Expr
	Alias string
}

Field represents an expression retrieved from a select statement.

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the field. Returns alias, if set. Otherwise uses the function name or variable name.

func (*Field) String

func (f *Field) String() string

String returns a string representation of the field.

type Fields

type Fields []*Field

Fields represents a list of fields.

func (Fields) AliasNames

func (a Fields) AliasNames() []string

AliasNames returns a list of calculated field names in order of alias, function name, then field.

func (Fields) Names

func (a Fields) Names() []string

Names returns a list of field names.

func (Fields) String

func (a Fields) String() string

String returns a string representation of the fields.

type HasDefaultDatabase

type HasDefaultDatabase interface {
	Node

	DefaultDatabase() string
	// contains filtered or unexported methods
}

HasDefaultDatabase provides an interface to get the default database from a Statement.

type IntegerLiteral

type IntegerLiteral struct {
	Val int64
}

IntegerLiteral represents an integer literal.

func (*IntegerLiteral) String

func (l *IntegerLiteral) String() string

String returns a string representation of the literal.

type ListLiteral

type ListLiteral struct {
	Vals []interface{}
}

ListLiteral represents a list of strings literal.

func (*ListLiteral) String

func (s *ListLiteral) String() string

String returns a string representation of the literal.

type Literal

type Literal interface {
	Expr
	// contains filtered or unexported methods
}

Literal represents a static literal.

type Measurement

type Measurement struct {
	Database string
}

Measurement represents a single measurement used as a datasource.

func (*Measurement) String

func (m *Measurement) String() string

String returns a string representation of the measurement.

type Measurements

type Measurements []*Measurement

Measurements represents a list of measurements.

func (Measurements) String

func (a Measurements) String() string

String returns a string representation of the measurements.

type Node

type Node interface {
	String() string
	// contains filtered or unexported methods
}

Node represents a node in the InfluxDB abstract syntax tree.

type NowValuer

type NowValuer struct {
	Now time.Time
}

NowValuer returns only the value for "now()".

func (*NowValuer) Value

func (v *NowValuer) Value(key string) (interface{}, bool)

Value is a method that returns the value and existence flag for a given key.

type NumberLiteral

type NumberLiteral struct {
	Val float64
}

NumberLiteral represents a numeric literal.

func (*NumberLiteral) String

func (l *NumberLiteral) String() string

String returns a string representation of the literal.

type ParenExpr

type ParenExpr struct {
	Expr Expr
}

ParenExpr represents a parenthesized expression.

func (*ParenExpr) String

func (e *ParenExpr) String() string

String returns a string representation of the parenthesized expression.

type ParseError

type ParseError struct {
	Message  string
	Found    string
	Expected []string
	Pos      Pos
}

ParseError represents an error that occurred during parsing.

func (*ParseError) Error

func (e *ParseError) Error() string

Error returns the string representation of the error.

type Parser

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

Parser represents an InfluxQL parser.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser returns a new instance of Parser.

func (*Parser) ParseExpr

func (p *Parser) ParseExpr() (Expr, error)

ParseExpr parses an expression.

func (*Parser) ParseStatement

func (p *Parser) ParseStatement() (Statement, error)

ParseStatement parses an InfluxQL string and returns a Statement AST object.

type Points

type Points []point

Points is a slice timeseries metric valus

type Pos

type Pos struct {
	Line int
	Char int
}

Pos specifies the line and character position of a token. The Char and Line are both zero-based indexes.

type RegexLiteral

type RegexLiteral struct {
	Val *regexp.Regexp
}

RegexLiteral represents a regular expression.

func CloneRegexLiteral

func CloneRegexLiteral(r *RegexLiteral) *RegexLiteral

CloneRegexLiteral returns a clone of the RegexLiteral.

func (*RegexLiteral) String

func (r *RegexLiteral) String() string

String returns a string representation of the literal.

type Rewriter

type Rewriter interface {
	Rewrite(Node) Node
}

Rewriter can be called by Rewrite to replace nodes in the AST hierarchy. The Rewrite() function is called once per node.

type Scanner

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

Scanner represents a lexical scanner for InfluxQL.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner returns a new instance of Scanner.

func (*Scanner) Scan

func (s *Scanner) Scan() (tok Token, pos Pos, lit string)

Scan returns the next token and position from the underlying reader. Also returns the literal text read for strings, numbers, and duration tokens since these token types can have different literal representations.

func (*Scanner) ScanRegex

func (s *Scanner) ScanRegex() (tok Token, pos Pos, lit string)

ScanRegex consumes a token to find escapes

type SelectStatement

type SelectStatement struct {
	// Expressions returned from the selection.
	Fields Fields

	// Data sources that fields are extracted from.
	Sources Sources

	// An expression evaluated on data point.
	Condition Expr

	// Expressions used for grouping the selection.
	Dimensions Dimensions

	// if it's a query for raw data values (i.e. not an aggregate)
	IsRawQuery bool

	// Removes duplicate rows from raw queries.
	Dedupe bool

	Count int
}

SelectStatement represents a command for extracting data from the database.

func (*SelectStatement) Clone

func (s *SelectStatement) Clone() *SelectStatement

Clone returns a deep copy of the statement.

func (*SelectStatement) ColumnNames

func (s *SelectStatement) ColumnNames() []string

ColumnNames will walk all fields and functions and return the appropriate field names for the select statement while maintaining order of the field names

func (*SelectStatement) FlatStatByGroup

func (s *SelectStatement) FlatStatByGroup(docs []string) map[string]*SelectStatement

FlatStatByGroup divergent multi SelectStatement based on group by clause

func (*SelectStatement) FunctionCalls

func (s *SelectStatement) FunctionCalls() []*Call

FunctionCalls returns the Call objects from the query

func (*SelectStatement) FunctionCallsByPosition

func (s *SelectStatement) FunctionCallsByPosition() [][]*Call

FunctionCallsByPosition returns the Call objects from the query in the order they appear in the select statement

func (*SelectStatement) NamesInSelect

func (s *SelectStatement) NamesInSelect() []string

NamesInSelect returns the field and tag names (idents) in the select clause

func (*SelectStatement) NamesInWhere

func (s *SelectStatement) NamesInWhere() []string

NamesInWhere returns the field and tag names (idents) referenced in the where clause

func (*SelectStatement) String

func (s *SelectStatement) String() string

String returns a string representation of the select statement.

type Source

type Source interface {
	Node
	// contains filtered or unexported methods
}

Source represents a source of data for a statement.

type Sources

type Sources []Source

Sources represents a list of sources.

func (Sources) Names

func (a Sources) Names() []string

Names returns a list of source names.

func (Sources) String

func (a Sources) String() string

String returns a string representation of a Sources array.

type Statement

type Statement interface {
	Node
	// contains filtered or unexported methods
}

Statement represents a single command in InfluxQL.

func ParseStatement

func ParseStatement(s string) (Statement, error)

ParseStatement parses a statement string and returns its AST representation.

type Statements

type Statements []Statement

Statements represents a list of statements.

func (Statements) String

func (a Statements) String() string

String returns a string representation of the statements.

type StringLiteral

type StringLiteral struct {
	Val string
}

StringLiteral represents a string literal.

func (*StringLiteral) String

func (l *StringLiteral) String() string

String returns a string representation of the literal.

type Token

type Token int

Token is a lexical token of the InfluxQL language.

const (
	// ILLEGAL Token, EOF, WS are Special InfluxQL tokens.
	ILLEGAL Token = iota
	EOF
	WS

	// IDENT and the following are InfluxQL literal tokens.
	IDENT     // main
	NUMBER    // 12345.67
	INTEGER   // 12345
	STRING    // "abc"
	BADSTRING // "abc
	BADESCAPE // \q
	TRUE      // true
	FALSE     // false
	REGEX     // Regular expressions
	BADREGEX  // `.*

	// ADD and the following are InfluxQL Operators
	ADD // +
	SUB // -
	MUL // *
	DIV // /
	MOD // %

	AND // AND
	OR  // OR
	NI  // not in
	IN

	EQ       // =
	NEQ      // !=
	EQREGEX  // =~
	NEQREGEX // !~
	LT       // <
	LTE      // <=
	GT       // >
	GTE      // >=

	LBRACKET // [
	LPAREN   // (
	RBRACKET // ]
	RPAREN   // )
	COMMA    // ,
	DOT      // .

	ALL
	AS
	FROM
	SELECT
	WHERE
	GROUP
	BY
)

These are a comprehensive list of InfluxQL language tokens.

func Lookup

func Lookup(ident string) Token

Lookup returns the token associated with a given string.

func (Token) Precedence

func (tok Token) Precedence() int

Precedence returns the operator precedence of the binary operator token.

func (Token) String

func (tok Token) String() string

String returns the string representation of the token.

type Valuer

type Valuer interface {
	Value(key string) (interface{}, bool)
}

Valuer is the interface that wraps the Value() method.

Value returns the value and existence flag for a given key.

type VarRef

type VarRef struct {
	Val      string
	Segments []string
}

VarRef represents a reference to a variable.

func (*VarRef) String

func (r *VarRef) String() string

String returns a string representation of the variable reference.

type VarRefs

type VarRefs []VarRef

VarRefs represents a slice of VarRef types.

func (VarRefs) Strings

func (a VarRefs) Strings() []string

Strings returns a slice of the variable names.

type Visitor

type Visitor interface {
	Visit(Node) Visitor
}

Visitor can be called by Walk to traverse an AST hierarchy. The Visit() function is called once per node.

Jump to

Keyboard shortcuts

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