Skip to content

Commit

Permalink
feat(influxql): Add hyper log log operators (influxdata#22322)
Browse files Browse the repository at this point in the history
In addition to helping with normal queries, this can improve the 'SHOW CARDINALITY'
meta-queries.


Co-authored-by: Sam Arnold <[email protected]>
  • Loading branch information
danxmoran and lesam authored Aug 30, 2021
1 parent 6bb95ae commit 37088e8
Show file tree
Hide file tree
Showing 14 changed files with 597 additions and 102 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [21972](https://github.com/influxdata/influxdb/pull/21972): Added support for notebooks and annotations.
1. [22135](https://github.com/influxdata/influxdb/pull/22135): Added route to return known resources.
1. [22311](https://github.com/influxdata/influxdb/pull/22311): Add `storage-no-validate-field-size` config to `influxd` to disable enforcement of max field size.
1. [22316](https://github.com/influxdata/influxdb/pull/22316): Optimize series iteration for queries that can be answered without inspecting TSM data.
1. [22322](https://github.com/influxdata/influxdb/pull/22322): Add support for `merge_hll`, `sum_hll`, and `count_hll` in InfluxQL.

### Bug Fixes

Expand All @@ -75,7 +77,6 @@ Because of the version bump to `go`, the macOS build for this release requires a
1. [21910](https://github.com/influxdata/influxdb/pull/21910): Added `--ui-disabled` option to `influxd` to allow for running with the UI disabled.
1. [21958](https://github.com/influxdata/influxdb/pull/21958): Telemetry improvements: Do not record telemetry data for non-existant paths; replace invalid static asset paths with a slug.
1. [22023](https://github.com/influxdata/influxdb/pull/22023): Upgrade Flux to v0.124.0.
1. [22316](https://github.com/influxdata/influxdb/pull/22316): Optimize series iteration for queries that can be answered without inspecting TSM data.

### Bug Fixes

Expand Down
69 changes: 69 additions & 0 deletions influxql/query/call_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func NewCallIterator(input Iterator, opt IteratorOptions) (Iterator, error) {
return newLastIterator(input, opt)
case "mean":
return newMeanIterator(input, opt)
case "sum_hll":
return NewSumHllIterator(input, opt)
case "merge_hll":
return NewMergeHllIterator(input, opt)
default:
return nil, fmt.Errorf("unsupported function call: %s", name)
}
Expand Down Expand Up @@ -1529,3 +1533,68 @@ func newIntegralIterator(input Iterator, opt IteratorOptions, interval Interval)
return nil, fmt.Errorf("unsupported integral iterator type: %T", input)
}
}

// NewSumHllIterator returns an iterator for operating on a distinct() call.
func NewSumHllIterator(input Iterator, opt IteratorOptions) (Iterator, error) {
switch input := input.(type) {
case FloatIterator:
createFn := func() (FloatPointAggregator, StringPointEmitter) {
fn := NewFloatSumHllReducer()
return fn, fn
}
return newFloatReduceStringIterator(input, opt, createFn), nil
case IntegerIterator:
createFn := func() (IntegerPointAggregator, StringPointEmitter) {
fn := NewIntegerSumHllReducer()
return fn, fn
}
return newIntegerReduceStringIterator(input, opt, createFn), nil
case UnsignedIterator:
createFn := func() (UnsignedPointAggregator, StringPointEmitter) {
fn := NewUnsignedSumHllReducer()
return fn, fn
}
return newUnsignedReduceStringIterator(input, opt, createFn), nil
case StringIterator:
createFn := func() (StringPointAggregator, StringPointEmitter) {
fn := NewStringSumHllReducer()
return fn, fn
}
return newStringReduceStringIterator(input, opt, createFn), nil
case BooleanIterator:
createFn := func() (BooleanPointAggregator, StringPointEmitter) {
fn := NewBooleanSumHllReducer()
return fn, fn
}
return newBooleanReduceStringIterator(input, opt, createFn), nil
default:
return nil, fmt.Errorf("unsupported sum_hll iterator type: %T", input)
}
}

// NewSumHllIterator returns an iterator for operating on a distinct() call.
func NewMergeHllIterator(input Iterator, opt IteratorOptions) (Iterator, error) {
switch input := input.(type) {
case StringIterator:
createFn := func() (StringPointAggregator, StringPointEmitter) {
fn := NewStringMergeHllReducer()
return fn, fn
}
return newStringReduceStringIterator(input, opt, createFn), nil
default:
return nil, fmt.Errorf("unsupported merge_hll iterator type: %T", input)
}
}

func NewCountHllIterator(input Iterator, opt IteratorOptions) (Iterator, error) {
switch input := input.(type) {
case StringIterator:
createFn := func() (StringPointAggregator, UnsignedPointEmitter) {
fn := NewCountHllReducer()
return fn, fn
}
return newStringStreamUnsignedIterator(input, createFn, opt), nil
default:
return nil, fmt.Errorf("unsupported count_hll iterator type: %T", input)
}
}
17 changes: 16 additions & 1 deletion influxql/query/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ func (c *compiledField) compileExpr(expr influxql.Expr) error {
return c.compileElapsed(expr.Args)
case "integral":
return c.compileIntegral(expr.Args)
case "count_hll":
return c.compileCountHll(expr.Args)
case "holt_winters", "holt_winters_with_fit":
withFit := expr.Name == "holt_winters_with_fit"
return c.compileHoltWinters(expr.Args, withFit)
Expand Down Expand Up @@ -393,7 +395,7 @@ func (c *compiledField) compileFunction(expr *influxql.Call) error {
switch expr.Name {
case "max", "min", "first", "last":
// top/bottom are not included here since they are not typical functions.
case "count", "sum", "mean", "median", "mode", "stddev", "spread":
case "count", "sum", "mean", "median", "mode", "stddev", "spread", "sum_hll":
// These functions are not considered selectors.
c.global.OnlySelectors = false
default:
Expand Down Expand Up @@ -784,6 +786,19 @@ func (c *compiledField) compileIntegral(args []influxql.Expr) error {
return c.compileSymbol("integral", args[0])
}

func (c *compiledField) compileCountHll(args []influxql.Expr) error {
if exp, got := 1, len(args); exp != got {
return fmt.Errorf("invalid number of arguments for count_hll, expected %d, got %d", exp, got)
}
c.global.OnlySelectors = false
switch arg0 := args[0].(type) {
case *influxql.Call:
return c.compileExpr(arg0)
default:
return c.compileSymbol("count_hll", arg0)
}
}

func (c *compiledField) compileHoltWinters(args []influxql.Expr, withFit bool) error {
name := "holt_winters"
if withFit {
Expand Down
Loading

0 comments on commit 37088e8

Please sign in to comment.