forked from cmu-db/noisepage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg.tpl
85 lines (72 loc) · 2.33 KB
/
agg.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Expected output: 10
// SQL: SELECT col_b, count(col_a) FROM test_1 GROUP BY col_b
struct State {
table: AggregationHashTable
count: int32
}
struct Agg {
key: Integer
count: CountStarAggregate
}
fun setUpState(execCtx: *ExecutionContext, state: *State) -> nil {
state.count = 0
@aggHTInit(&state.table, execCtx, @sizeOf(Agg))
}
fun tearDownState(state: *State) -> nil {
@aggHTFree(&state.table)
}
fun keyCheck(agg: *Agg, vpi: *VectorProjectionIterator) -> bool {
var key = @vpiGetInt(vpi, 1)
return @sqlToBool(key == agg.key)
}
fun constructAgg(agg: *Agg, vpi: *VectorProjectionIterator) -> nil {
agg.key = @vpiGetInt(vpi, 1)
@aggInit(&agg.count)
}
fun updateAgg(agg: *Agg, vpi: *VectorProjectionIterator) -> nil {
var input = @vpiGetInt(vpi, 0)
@aggAdvance(&agg.count, &input)
}
fun pipeline_1(execCtx: *ExecutionContext, state: *State) -> nil {
var ht = &state.table
var tvi: TableVectorIterator
var table_oid = @testCatalogLookup(execCtx, "test_1", "")
var col_oids: [2]uint32
col_oids[0] = @testCatalogLookup(execCtx, "test_1", "colA")
col_oids[1] = @testCatalogLookup(execCtx, "test_1", "colB")
for (@tableIterInit(&tvi, execCtx, table_oid, col_oids); @tableIterAdvance(&tvi); ) {
var vec = @tableIterGetVPI(&tvi)
for (; @vpiHasNext(vec); @vpiAdvance(vec)) {
var hash_val = @hash(@vpiGetInt(vec, 1))
var agg = @ptrCast(*Agg, @aggHTLookup(ht, hash_val, keyCheck, vec))
if (agg == nil) {
agg = @ptrCast(*Agg, @aggHTInsert(ht, hash_val))
constructAgg(agg, vec)
} else {
updateAgg(agg, vec)
}
}
}
@tableIterClose(&tvi)
}
fun pipeline_2(state: *State) -> nil {
var aht_iter: AHTIterator
var iter = &aht_iter
for (@aggHTIterInit(iter, &state.table); @aggHTIterHasNext(iter); @aggHTIterNext(iter)) {
var agg = @ptrCast(*Agg, @aggHTIterGetRow(iter))
state.count = state.count + 1
}
@aggHTIterClose(iter)
}
fun execQuery(execCtx: *ExecutionContext, qs: *State) -> nil {
pipeline_1(execCtx, qs)
pipeline_2(qs)
}
fun main(execCtx: *ExecutionContext) -> int32 {
var state: State
setUpState(execCtx, &state)
execQuery(execCtx, &state)
tearDownState(&state)
var ret = state.count
return ret
}