Skip to content

Commit ac29950

Browse files
authored
refactor: use the go generic to rewrite interface of agg iterator (openGemini#599)
Signed-off-by: scuzyj <[email protected]>
1 parent 8cbcb29 commit ac29950

10 files changed

+847
-2023
lines changed

engine/executor/agg_func.gen.go

-1,045
This file was deleted.

engine/executor/agg_func.gen.go.tmpl

-672
This file was deleted.

engine/executor/agg_func.go

+509-146
Large diffs are not rendered by default.

engine/executor/agg_func_prom.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323

2424
const smallDeltaTolerance = 1e-12
2525

26-
func MinPromReduce(c Chunk, ordinal, start, end int) (int, float64, bool) {
27-
minValue := c.Column(ordinal).FloatValue(start)
26+
func MinPromReduce(c Chunk, values []float64, ordinal, start, end int) (int, float64, bool) {
27+
minValue := values[start]
2828
for i := start + 1; i < end; i++ {
29-
v := c.Column(ordinal).FloatValue(i)
29+
v := values[i]
3030
if minValue > v || math.IsNaN(minValue) {
3131
minValue = v
3232
}
@@ -40,10 +40,10 @@ func MinPromMerge(prevPoint, currPoint *Point[float64]) {
4040
}
4141
}
4242

43-
func MaxPromReduce(c Chunk, ordinal, start, end int) (int, float64, bool) {
44-
maxValue := c.Column(ordinal).FloatValue(start)
43+
func MaxPromReduce(c Chunk, values []float64, ordinal, start, end int) (int, float64, bool) {
44+
maxValue := values[start]
4545
for i := start + 1; i < end; i++ {
46-
v := c.Column(ordinal).FloatValue(i)
46+
v := values[i]
4747
if maxValue < v || math.IsNaN(maxValue) {
4848
maxValue = v
4949
}
@@ -57,7 +57,7 @@ func MaxPromMerge(prevPoint, currPoint *Point[float64]) {
5757
}
5858
}
5959

60-
func FloatCountPromReduce(c Chunk, ordinal, start, end int) (int, float64, bool) {
60+
func FloatCountPromReduce(c Chunk, values []float64, ordinal, start, end int) (int, float64, bool) {
6161
count := float64(end - start)
6262
return start, count, count == 0
6363
}

engine/executor/agg_func_test.go

+212-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package executor
1818

1919
import (
20+
"bytes"
2021
"testing"
2122

2223
"github.com/openGemini/openGemini/engine/hybridqp"
@@ -44,13 +45,13 @@ func TestIntegerFirstReduce(t *testing.T) {
4445
chunk.AddColumn(c2)
4546

4647
// first column
47-
idx, v, isNil := IntegerFirstReduce(chunk, 0, 0, 5)
48+
idx, v, isNil := FirstReduce[int64](chunk, chunk.Column(0).IntegerValues(), 0, 0, 5)
4849
if !(idx == 0 && v == 1 && !isNil) {
4950
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
5051
}
5152

5253
// second column
53-
idx, v, isNil = IntegerFirstReduce(chunk, 1, 0, 5)
54+
idx, v, isNil = FirstReduce[int64](chunk, chunk.Column(1).IntegerValues(), 1, 0, 5)
5455
if !(idx == 0 && v == 11 && !isNil) {
5556
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
5657
}
@@ -60,6 +61,7 @@ func TestFloatFirstReduce(t *testing.T) {
6061
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
6162
influxql.VarRef{Val: "val_float1", Type: influxql.Float},
6263
influxql.VarRef{Val: "val_float2", Type: influxql.Float},
64+
influxql.VarRef{Val: "val_float3", Type: influxql.Float},
6365
influxql.VarRef{Val: "time", Type: influxql.Integer},
6466
), "test")
6567

@@ -71,22 +73,33 @@ func TestFloatFirstReduce(t *testing.T) {
7173
c2.AppendNilsV2(true, true, true, true, true)
7274
c2.AppendFloatValues([]float64{11.1, 18.2, 19.3, 20.4, 21.5})
7375

76+
c3 := NewColumnImpl(influxql.Float)
77+
c3.AppendNilsV2(true, true, true, false, false)
78+
c3.AppendFloatValues([]float64{11.1, 18.2, 19.3})
79+
7480
chunk.SetTime([]int64{1, 2, 3, 4, 5})
7581
chunk.ResetIntervalIndex(0)
7682
chunk.AddColumn(c1)
7783
chunk.AddColumn(c2)
84+
chunk.AddColumn(c3)
7885

7986
// first column
80-
idx, v, isNil := FloatFirstReduce(chunk, 0, 0, 5)
87+
idx, v, isNil := FirstReduce[float64](chunk, chunk.Column(0).FloatValues(), 0, 0, 5)
8188
if !(idx == 0 && v == 1.1 && !isNil) {
8289
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
8390
}
8491

8592
// second column
86-
idx, v, isNil = FloatFirstReduce(chunk, 1, 0, 5)
93+
idx, v, isNil = FirstReduce[float64](chunk, chunk.Column(1).FloatValues(), 1, 0, 5)
8794
if !(idx == 0 && v == 11.1 && !isNil) {
8895
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
8996
}
97+
98+
// third column
99+
idx, v, isNil = FirstReduce[float64](chunk, chunk.Column(2).FloatValues(), 2, 4, 5)
100+
if !(idx == 4 && v == 0 && isNil) {
101+
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
102+
}
90103
}
91104

92105
func TestStringFirstReduce(t *testing.T) {
@@ -109,12 +122,12 @@ func TestStringFirstReduce(t *testing.T) {
109122
chunk.AddColumn(c1)
110123
chunk.AddColumn(c2)
111124

112-
idx, v, isNil := StringFirstReduce(chunk, 0, 0, 5)
125+
idx, v, isNil := FirstReduce[string](chunk, chunk.Column(0).StringValuesV2(nil), 0, 0, 5)
113126
if !(idx == 0 && v == "string1" && !isNil) {
114127
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
115128
}
116129

117-
idx, v, isNil = StringFirstReduce(chunk, 1, 0, 5)
130+
idx, v, isNil = FirstReduce[string](chunk, chunk.Column(1).StringValuesV2(nil), 1, 0, 5)
118131
if !(idx == 0 && v == "string11" && !isNil) {
119132
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
120133
}
@@ -124,29 +137,219 @@ func TestBooleanFirstReduce(t *testing.T) {
124137
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
125138
influxql.VarRef{Val: "val_bool1", Type: influxql.Boolean},
126139
influxql.VarRef{Val: "val_bool2", Type: influxql.Boolean},
140+
influxql.VarRef{Val: "val_bool3", Type: influxql.Boolean},
127141
influxql.VarRef{Val: "time", Type: influxql.Integer},
128142
), "test")
129143

130144
c1 := NewColumnImpl(influxql.Boolean)
131145
c1.AppendNilsV2(true, true, true, false, false)
132146
c1.AppendBooleanValues([]bool{true, false, false})
133147

134-
c2 := NewColumnImpl(influxql.Integer)
148+
c2 := NewColumnImpl(influxql.Boolean)
135149
c2.AppendNilsV2(true, true, true, true, true)
136150
c2.AppendBooleanValues([]bool{false, true, true, true, true})
137151

152+
c3 := NewColumnImpl(influxql.Boolean)
153+
c3.AppendNilsV2(true, true, true, false, false)
154+
c3.AppendBooleanValues([]bool{false, true, true, true, true})
155+
138156
chunk.SetTime([]int64{1, 2, 3, 4, 5})
139157
chunk.ResetIntervalIndex(0)
140158
chunk.AddColumn(c1)
141159
chunk.AddColumn(c2)
160+
chunk.AddColumn(c3)
142161

143-
idx, v, isNil := BooleanFirstReduce(chunk, 0, 0, 5)
162+
idx, v, isNil := BooleanFirstReduce(chunk, chunk.Column(0).BooleanValues(), 0, 0, 5)
144163
if !(idx == 0 && v == true && !isNil) {
145164
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
146165
}
147166

148-
idx, v, isNil = BooleanFirstReduce(chunk, 1, 0, 5)
167+
idx, v, isNil = BooleanFirstReduce(chunk, chunk.Column(1).BooleanValues(), 1, 0, 5)
149168
if !(idx == 0 && v == false && !isNil) {
150169
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
151170
}
171+
172+
idx, v, isNil = BooleanFirstReduce(chunk, chunk.Column(2).BooleanValues(), 2, 4, 5)
173+
if !(idx == 4 && v == false && isNil) {
174+
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
175+
}
176+
}
177+
178+
func TestIntegerLastReduce(t *testing.T) {
179+
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
180+
influxql.VarRef{Val: "val_int1", Type: influxql.Integer},
181+
influxql.VarRef{Val: "val_int2", Type: influxql.Integer},
182+
influxql.VarRef{Val: "val_int3", Type: influxql.Integer},
183+
influxql.VarRef{Val: "time", Type: influxql.Integer},
184+
), "test")
185+
186+
c1 := NewColumnImpl(influxql.Integer)
187+
c1.AppendNilsV2(true, true, true, false, false)
188+
c1.AppendIntegerValues([]int64{1, 8, 9})
189+
190+
c2 := NewColumnImpl(influxql.Integer)
191+
c2.AppendNilsV2(true, true, true, true, true)
192+
c2.AppendIntegerValues([]int64{11, 18, 19, 20, 21})
193+
194+
c3 := NewColumnImpl(influxql.Integer)
195+
c3.AppendNilsV2(true, true, true, false, false)
196+
c3.AppendIntegerValues([]int64{11, 18, 19})
197+
198+
chunk.SetTime([]int64{1, 2, 3, 4, 5})
199+
chunk.ResetIntervalIndex(0)
200+
chunk.AddColumn(c1)
201+
chunk.AddColumn(c2)
202+
chunk.AddColumn(c3)
203+
204+
// first column
205+
idx, v, isNil := LastReduce[int64](chunk, chunk.Column(0).IntegerValues(), 0, 0, 5)
206+
if !(idx == 2 && v == 9 && !isNil) {
207+
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
208+
}
209+
210+
// second column
211+
idx, v, isNil = LastReduce[int64](chunk, chunk.Column(1).IntegerValues(), 1, 0, 5)
212+
if !(idx == 4 && v == 21 && !isNil) {
213+
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
214+
}
215+
216+
// third column
217+
idx, v, isNil = LastReduce[int64](chunk, chunk.Column(2).IntegerValues(), 2, 4, 5)
218+
if !(idx == 4 && v == 0 && isNil) {
219+
t.Fatal("not expect, idx ", idx, "v ", v, "exist", isNil)
220+
}
221+
}
222+
223+
func TestBooleanLastReduce(t *testing.T) {
224+
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
225+
influxql.VarRef{Val: "val_bool1", Type: influxql.Boolean},
226+
influxql.VarRef{Val: "val_bool2", Type: influxql.Boolean},
227+
influxql.VarRef{Val: "val_bool3", Type: influxql.Boolean},
228+
influxql.VarRef{Val: "time", Type: influxql.Integer},
229+
), "test")
230+
231+
c1 := NewColumnImpl(influxql.Boolean)
232+
c1.AppendNilsV2(true, true, true, false, false)
233+
c1.AppendBooleanValues([]bool{true, false, false})
234+
235+
c2 := NewColumnImpl(influxql.Integer)
236+
c2.AppendNilsV2(true, true, true, true, true)
237+
c2.AppendBooleanValues([]bool{false, true, true, true, true})
238+
239+
c3 := NewColumnImpl(influxql.Integer)
240+
c3.AppendNilsV2(true, true, true, false, false)
241+
c3.AppendBooleanValues([]bool{false, true, true})
242+
243+
chunk.SetTime([]int64{1, 2, 3, 4, 5})
244+
chunk.ResetIntervalIndex(0)
245+
chunk.AddColumn(c1)
246+
chunk.AddColumn(c2)
247+
chunk.AddColumn(c3)
248+
249+
idx, v, isNil := BooleanLastReduce(chunk, chunk.Column(0).BooleanValues(), 0, 0, 5)
250+
if !(idx == 2 && v == false && !isNil) {
251+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
252+
}
253+
254+
idx, v, isNil = BooleanLastReduce(chunk, chunk.Column(1).BooleanValues(), 1, 0, 5)
255+
if !(idx == 4 && v == true && !isNil) {
256+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
257+
}
258+
259+
idx, v, isNil = BooleanLastReduce(chunk, chunk.Column(2).BooleanValues(), 2, 4, 5)
260+
if !(idx == 4 && v == false && isNil) {
261+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
262+
}
263+
}
264+
265+
func TestBooleanMinReduce(t *testing.T) {
266+
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
267+
influxql.VarRef{Val: "val_bool1", Type: influxql.Boolean},
268+
influxql.VarRef{Val: "val_bool2", Type: influxql.Boolean},
269+
influxql.VarRef{Val: "time", Type: influxql.Integer},
270+
), "test")
271+
272+
c1 := NewColumnImpl(influxql.Boolean)
273+
c1.AppendNilsV2(true, true, true, false, false)
274+
c1.AppendBooleanValues([]bool{true, false, false})
275+
276+
c2 := NewColumnImpl(influxql.Integer)
277+
c2.AppendNilsV2(true, true, true, true, true)
278+
c2.AppendBooleanValues([]bool{false, true, true, true, true})
279+
280+
chunk.SetTime([]int64{1, 2, 3, 4, 5})
281+
chunk.ResetIntervalIndex(0)
282+
chunk.AddColumn(c1)
283+
chunk.AddColumn(c2)
284+
285+
idx, v, isNil := BooleanMinReduce(chunk, chunk.Column(0).BooleanValues(), 0, 0, 5)
286+
if !(idx == 1 && v == false && !isNil) {
287+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
288+
}
289+
290+
idx, v, isNil = BooleanMinReduce(chunk, chunk.Column(1).BooleanValues(), 1, 0, 5)
291+
if !(idx == 0 && v == false && !isNil) {
292+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
293+
}
294+
}
295+
296+
func TestBooleanMaxReduce(t *testing.T) {
297+
chunk := NewChunkImpl(hybridqp.NewRowDataTypeImpl(
298+
influxql.VarRef{Val: "val_bool1", Type: influxql.Boolean},
299+
influxql.VarRef{Val: "val_bool2", Type: influxql.Boolean},
300+
influxql.VarRef{Val: "time", Type: influxql.Integer},
301+
), "test")
302+
303+
c1 := NewColumnImpl(influxql.Boolean)
304+
c1.AppendNilsV2(true, true, true, false, false)
305+
c1.AppendBooleanValues([]bool{true, false, false})
306+
307+
c2 := NewColumnImpl(influxql.Integer)
308+
c2.AppendNilsV2(true, true, true, true, true)
309+
c2.AppendBooleanValues([]bool{false, true, true, true, true})
310+
311+
chunk.SetTime([]int64{1, 2, 3, 4, 5})
312+
chunk.ResetIntervalIndex(0)
313+
chunk.AddColumn(c1)
314+
chunk.AddColumn(c2)
315+
316+
idx, v, isNil := BooleanMaxReduce(chunk, chunk.Column(0).BooleanValues(), 0, 0, 5)
317+
if !(idx == 0 && v == true && !isNil) {
318+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
319+
}
320+
321+
idx, v, isNil = BooleanMaxReduce(chunk, chunk.Column(1).BooleanValues(), 1, 0, 5)
322+
if !(idx == 1 && v == true && !isNil) {
323+
t.Fatal("not expect, idx ", idx, ", v ", v, ", exist", isNil)
324+
}
325+
}
326+
327+
func TestStringLastMerge(t *testing.T) {
328+
prePoint := newStringPoint()
329+
prePoint.Set(0, 1, "string1")
330+
331+
currentPoint := newStringPoint()
332+
currentPoint.Set(1, 2, "string2")
333+
334+
StringLastMerge(prePoint, currentPoint)
335+
if !(prePoint.time == currentPoint.time &&
336+
prePoint.index == currentPoint.index &&
337+
bytes.Compare(prePoint.value, currentPoint.value) == 0) {
338+
t.Fatal("prePoint and currentPoint is not equal")
339+
}
340+
}
341+
342+
func TestBooleanLastMerge(t *testing.T) {
343+
prePoint := newPoint[bool]()
344+
prePoint.Set(0, 1, true)
345+
346+
currentPoint := newPoint[bool]()
347+
currentPoint.Set(1, 2, false)
348+
349+
BooleanLastMerge(prePoint, currentPoint)
350+
if !(prePoint.time == currentPoint.time &&
351+
prePoint.index == currentPoint.index &&
352+
prePoint.value == currentPoint.value) {
353+
t.Fatal("prePoint and currentPoint is not equal")
354+
}
152355
}

0 commit comments

Comments
 (0)