-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
462 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// Package aggregate for measure aggregate function. | ||
package aggregate | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1" | ||
) | ||
|
||
// Void type contains nothing. It works as a placeholder for type parameters of `Arguments`. | ||
type Void struct{} | ||
|
||
// Input covers possible types of Function's arguments. It synchronizes with `FieldType` in schema. | ||
type Input interface { | ||
Void | ~int64 | ~float64 | ||
} | ||
|
||
// Output covers possible types of Function's return value. | ||
type Output interface { | ||
~int64 | ~float64 | ||
} | ||
|
||
var errFieldValueType = fmt.Errorf("unsupported input value type on this field") | ||
|
||
// Arguments represents the argument array, with one argument or two arguments. | ||
type Arguments[A, B Input] struct { | ||
arg0 []A | ||
arg1 []B | ||
} | ||
|
||
// Function describes two stages of aggregation. | ||
type Function[A, B Input, R Output] interface { | ||
// Combine takes elements to do the aggregation. | ||
// It uses a two-dimensional array to represent the argument array. | ||
Combine(arguments Arguments[A, B]) error | ||
|
||
// Result gives the result for the aggregation. | ||
Result() R | ||
} | ||
|
||
// NewFunction constructs the aggregate function with given kind and parameter types. | ||
func NewFunction[A, B Input, R Output](kind modelv1.MeasureAggregate) (Function[A, B, R], error) { | ||
var function Function[A, B, R] | ||
switch kind { | ||
case modelv1.MeasureAggregate_MEASURE_AGGREGATE_MIN: | ||
function = &Min[A, B, R]{minimum: maxValue[R]()} | ||
case modelv1.MeasureAggregate_MEASURE_AGGREGATE_AVG: | ||
function = &Avg[A, B, R]{summation: zeroValue[R](), count: 0} | ||
default: | ||
return nil, fmt.Errorf("MeasureAggregate unknown") | ||
} | ||
|
||
return function, nil | ||
} | ||
|
||
func zeroValue[R Output]() R { | ||
var r R | ||
return r | ||
} | ||
|
||
func maxValue[R Output]() (r R) { | ||
switch a := any(&r).(type) { | ||
case *int64: | ||
*a = math.MaxInt64 | ||
case *float64: | ||
*a = math.MaxFloat64 | ||
case *string: | ||
*a = "" | ||
default: | ||
panic("unreachable") | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to Apache Software Foundation (ASF) under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Apache Software Foundation (ASF) licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package aggregate | ||
|
||
// Avg calculates the average value of elements. | ||
type Avg[A, B Input, R Output] struct { | ||
summation R | ||
count int64 | ||
} | ||
|
||
// Combine takes elements to do the aggregation. | ||
// Avg uses type parameter A and B. | ||
func (a *Avg[A, B, R]) Combine(arguments Arguments[A, B]) error { | ||
for _, arg0 := range arguments.arg0 { | ||
switch arg0 := any(arg0).(type) { | ||
case int64: | ||
a.summation += R(arg0) | ||
case float64: | ||
a.summation += R(arg0) | ||
default: | ||
return errFieldValueType | ||
} | ||
} | ||
|
||
for _, arg1 := range arguments.arg1 { | ||
switch arg1 := any(arg1).(type) { | ||
case int64: | ||
a.count += arg1 | ||
default: | ||
return errFieldValueType | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Result gives the result for the aggregation. | ||
func (a *Avg[A, B, R]) Result() R { | ||
// In unusual situations it returns the zero value. | ||
if a.count == 0 { | ||
return zeroValue[R]() | ||
} | ||
// According to the semantics of GoLang, the division of one int by another int | ||
// returns an int, instead of a float. | ||
return a.summation / R(a.count) | ||
} | ||
|
||
// NewAvgArguments constructs arguments. | ||
func NewAvgArguments[A Input](a []A, b []int64) Arguments[A, int64] { | ||
return Arguments[A, int64]{ | ||
arg0: a, | ||
arg1: b, | ||
} | ||
} |
Oops, something went wrong.