-
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.
Dev measure aggregate function (#528)
* MeasureAggregateFunctionService Support --------- Co-authored-by: 吴晟 Wu Sheng <wu.sheng@foxmail.com> Co-authored-by: Gao Hongtao <hanahmily@gmail.com>
- Loading branch information
1 parent
a8cee7f
commit 25c1969
Showing
15 changed files
with
616 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 | ||
|
||
// Count calculates the count value of elements. | ||
type Count[A, B Input, R Output] struct { | ||
count int64 | ||
} | ||
|
||
// Combine takes elements to do the aggregation. | ||
// Count uses none of type parameters. | ||
func (f *Count[A, B, R]) Combine(arguments Arguments[A, B]) error { | ||
i := 0 | ||
n := len(arguments.arg0) | ||
// step-4 aggregate | ||
for ; i <= n-4; i += 4 { | ||
f.count += int64(arguments.arg0[i]) + int64(arguments.arg0[i+1]) + | ||
int64(arguments.arg0[i+2]) + int64(arguments.arg0[i+3]) | ||
} | ||
// tail aggregate | ||
for ; i < n; i++ { | ||
f.count += int64(arguments.arg0[i]) | ||
} | ||
return nil | ||
} | ||
|
||
// Result gives the result for the aggregation. | ||
func (f *Count[A, B, R]) Result() (A, B, R) { | ||
return A(f.count), zeroValue[B](), R(f.count) | ||
} | ||
|
||
// NewCountArguments constructs arguments. | ||
func NewCountArguments(a []int64) Arguments[int64, Void] { | ||
return Arguments[int64, Void]{ | ||
arg0: a, | ||
arg1: nil, | ||
} | ||
} |
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,40 @@ | ||
// 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1" | ||
"github.com/apache/skywalking-banyandb/banyand/measure/aggregate" | ||
) | ||
|
||
func TestCount(t *testing.T) { | ||
var err error | ||
|
||
// case1: input int64 values | ||
countInt64, _ := aggregate.NewFunction[int64, aggregate.Void, int64](modelv1.MeasureAggregate_MEASURE_AGGREGATE_COUNT) | ||
err = countInt64.Combine(aggregate.NewCountArguments( | ||
[]int64{1, 2, 3}, // mock the "count" column | ||
)) | ||
assert.NoError(t, err) | ||
_, _, r1 := countInt64.Result() | ||
assert.Equal(t, int64(6), r1) | ||
} |
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,47 @@ | ||
// 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 | ||
|
||
// Max calculates the maximum value of elements. | ||
type Max[A, B Input, R Output] struct { | ||
maximum R | ||
} | ||
|
||
// Combine takes elements to do the aggregation. | ||
// Max uses type parameter A. | ||
func (f *Max[A, B, R]) Combine(arguments Arguments[A, B]) error { | ||
for _, arg0 := range arguments.arg0 { | ||
if R(arg0) > f.maximum { | ||
f.maximum = R(arg0) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Result gives the result for the aggregation. | ||
func (f *Max[A, B, R]) Result() (A, B, R) { | ||
return A(f.maximum), zeroValue[B](), f.maximum | ||
} | ||
|
||
// NewMaxArguments constructs arguments. | ||
func NewMaxArguments[A Input](a []A) Arguments[A, Void] { | ||
return Arguments[A, Void]{ | ||
arg0: a, | ||
arg1: nil, | ||
} | ||
} |
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,49 @@ | ||
// 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_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
modelv1 "github.com/apache/skywalking-banyandb/api/proto/banyandb/model/v1" | ||
"github.com/apache/skywalking-banyandb/banyand/measure/aggregate" | ||
) | ||
|
||
func TestMax(t *testing.T) { | ||
var err error | ||
|
||
// case1: input int64 values | ||
maxInt64, _ := aggregate.NewFunction[int64, aggregate.Void, int64](modelv1.MeasureAggregate_MEASURE_AGGREGATE_MAX) | ||
err = maxInt64.Combine(aggregate.NewMaxArguments[int64]( | ||
[]int64{1, 2, 3}, // mock the "maximum" column | ||
)) | ||
assert.NoError(t, err) | ||
_, _, r1 := maxInt64.Result() | ||
assert.Equal(t, int64(3), r1) | ||
|
||
// case2: input float64 values | ||
maxFloat64, _ := aggregate.NewFunction[float64, aggregate.Void, float64](modelv1.MeasureAggregate_MEASURE_AGGREGATE_MAX) | ||
err = maxFloat64.Combine(aggregate.NewMaxArguments[float64]( | ||
[]float64{1.0, 2.0, 3.0}, // mock the "maximum" column | ||
)) | ||
assert.NoError(t, err) | ||
_, _, r2 := maxFloat64.Result() | ||
assert.Equal(t, 3.0, r2) | ||
} |
Oops, something went wrong.