expr

package
v0.0.0-...-df46cc1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package expr provides a framework for Expressions that evaluate to floating point values and allow various functions that can aggregate data, perform calculations on that data, evaluate boolean expressions against that data and serialize the data to/from bytes for durable storage in the database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertFloatEquals

func AssertFloatEquals(t *testing.T, a, b float64) bool

AssertFloatEquals does a fuzzy comparison of floats.

func AssertFloatWithin

func AssertFloatWithin(t *testing.T, e, expected float64, actual float64, msg string) bool

AssertFloatWithin checks whether a given float is within e error (decimal) of another float

func IsField

func IsField(e Expr) (string, bool)

IsField checks whether the given expression is a field expression and if so, returns the name of the field.

func IsPercentile

func IsPercentile(e Expr) bool

IsPercentile indicates whether the given expression is a percentile expression.

Types

type Expr

type Expr interface {
	// Validate makes sure that this expression is valid and returns an error if
	// it is not.
	Validate() error

	// EncodedWidth returns the number of bytes needed to represent the internal
	// state of this Expr.
	EncodedWidth() int

	// Shift returns the total cumulative shift in time, including
	// subexpressions.
	Shift() time.Duration

	// Update updates the value in buf by applying the given Params. Metadata
	// provides additional metadata that can be used in evaluating how to apply
	// the update.
	Update(b []byte, params Params, metadata goexpr.Params) (remain []byte, value float64, updated bool)

	// Merge merges x and y, writing the result to b. It returns the remaining
	// portions of x and y.
	Merge(b []byte, x []byte, y []byte) (remainB []byte, remainX []byte, remainY []byte)

	// SubMergers returns a list of functions that merge values of the given
	// subexpressions into this Expr. The list is the same length as the number of
	// sub expressions. For any subexpression that is not represented in our
	// Expression, the corresponding function in the list is nil.
	SubMergers(subs []Expr) []SubMerge

	// Get gets the value in buf, returning the value, a boolean indicating
	// whether or not the value was actually set, and the remaining byte array
	// after consuming the underlying data.
	Get(b []byte) (value float64, ok bool, remain []byte)

	// IsConstant indicates whether or not this is a constant expression
	IsConstant() bool

	// DeAggregate strips aggregates from this expression and returns the result
	DeAggregate() Expr

	String() string
}

An Expr is expression that stores its value in a byte array and that evaluates to a float64.

func ADD

func ADD(left interface{}, right interface{}) Expr

ADD creates an Expr that obtains its value by adding right and left.

func AND

func AND(left interface{}, right interface{}) Expr

AND tests whether left and right is true

func AVG

func AVG(val interface{}) Expr

AVG creates an Expr that obtains its value as the arithmetic mean over the given value.

func BOUNDED

func BOUNDED(expr interface{}, min float64, max float64) Expr

BOUNDED bounds the given expression to min <= val <= max. Any values that fall outside of the bounds will appear as unset (i.e. they are discarded.)

func CONST

func CONST(value float64) Expr

CONST returns an Accumulator that always has a constant value.

func COUNT

func COUNT(expr interface{}) Expr

COUNT creates an Expr that counts the number of values.

func DIV

func DIV(left interface{}, right interface{}) Expr

DIV creates an Expr that obtains its value by dividing left by right. If right is 0, this returns 0.

func EQ

func EQ(left interface{}, right interface{}) Expr

EQ tests whether left equals right

func FIELD

func FIELD(name string) Expr

FIELD creates an Expr that obtains its value from a named field.

func GT

func GT(left interface{}, right interface{}) Expr

GT tests whether left is greater than right

func GTE

func GTE(left interface{}, right interface{}) Expr

GTE tests whether left is greater than or equal to right

func IF

func IF(cond goexpr.Expr, wrapped interface{}) Expr

func LT

func LT(left interface{}, right interface{}) Expr

LT tests whether left is less than right

func LTE

func LTE(left interface{}, right interface{}) Expr

LTE tests whether left is less than or equal to the right

func MAX

func MAX(expr interface{}) Expr

MAX creates an Expr that keeps track of the maximum value of the wrapped expression or field.

func MIN

func MIN(expr interface{}) Expr

MIN creates an Expr that keeps track of the minimum value of the wrapped expression or field.

func MULT

func MULT(left interface{}, right interface{}) Expr

MULT creates an Expr that obtains its value by multiplying right and left.

func NEQ

func NEQ(left interface{}, right interface{}) Expr

NEQ tests whether left is different from right

func OR

func OR(left interface{}, right interface{}) Expr

OR tests whether left or right is true

func PERCENTILE

func PERCENTILE(value interface{}, percentile interface{}, min float64, max float64, precision int) Expr

PERCENTILE tracks estimated percentile values for the given expression assuming the given min and max possible values, to the given precision (where precision is the number of decimal points).

Inputs are automatically bounded to the min/max using BOUNDED, such that values falling outside the range are discarded. Percentile is input in percent (e.g. 0-100). See https://godoc.org/github.com/codahale/hdrhistogram.

It is possible to wrap an existing PERCENTILE with a new PERCENTILE to reuse the original PERCENTILE's storage but look at a different percentile. In this case, the min, max and precision parameters are ignored.

WARNING - when PERCENTILEs that wrap existing PERCENTILEs are not stored and as such are only suitable for use in querying but not in tables or views unless those explicitly include the original PERCENTILE as well.

WARNING - relative to other types of expressions, PERCENTILE types can be very large (e.g. on the order of Kilobytes vs 8 or 16 bytes for most other expressions) so it is best to keep these relatively low cardinality.

func PERCENTILEOPT

func PERCENTILEOPT(wrapped interface{}, percentile interface{}) Expr

PERCENTILEOPT returns an optimized PERCENTILE that wraps an existing PERCENTILE.

func SHIFT

func SHIFT(wrapped interface{}, offset time.Duration) Expr

func SUB

func SUB(left interface{}, right interface{}) Expr

SUB creates an Expr that obtains its value by subtracting right from left.

func SUM

func SUM(expr interface{}) Expr

SUM creates an Expr that obtains its value by summing the given expressions or fields.

func UnaryMath

func UnaryMath(name string, wrapped interface{}) (Expr, error)

func WAVG

func WAVG(val interface{}, weight interface{}) Expr

WAVG creates an Expr that obtains its value as the weighted arithmetic mean over the given value weighted by the given weight.

type FloatParams

type FloatParams float64

FloatParams is an implementation of Params that always returns the same float64 value.

func (FloatParams) Get

func (p FloatParams) Get(name string) (val float64, found bool)

type Map

type Map map[string]float64

Map is an implementation of the Params interface using a map.

func (Map) Get

func (p Map) Get(name string) (val float64, found bool)

Get implements the method from the Params interface

type Params

type Params interface {
	// Get returns the named value. Found should be false if nothing was found for
	// the given name.
	Get(name string) (val float64, found bool)
}

Params is an interface for data structures that can contain named values.

type SubMerge

type SubMerge func(data []byte, other []byte, otherRes time.Duration, metadata goexpr.Params)

SubMerge is a function that merges other into data for a given Expr, potentially taking into account the supplied metadata. otherRes is the amount of time represented by each period in other.

Jump to

Keyboard shortcuts

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