-
Notifications
You must be signed in to change notification settings - Fork 5
/
func_window_test.go
158 lines (132 loc) · 3.24 KB
/
func_window_test.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package sqlite_test
import (
"database/sql"
. "go.riyazali.net/sqlite"
"testing"
)
// Sum implements a window functions (that also doubles up as a normal aggregate function)
// It follows the spec and description defined at https://sqlite.org/lang_aggfunc.html#sumunc
// The source of this file is adapted from https://sqlite.org/src/file?name=src/func.c
type Sum struct{}
func (s *Sum) Args() int { return 1 }
func (s *Sum) Deterministic() bool { return true }
type SumContext struct {
rSum float64
iSum int64
count int64
approx bool
}
func (s *Sum) Step(ctx *AggregateContext, values ...Value) {
if ctx.Data() == nil {
ctx.SetData(&SumContext{})
}
var val = values[0]
var sumCtx = ctx.Data().(*SumContext)
if !val.IsNil() {
sumCtx.count++
if val.Type() == SQLITE_INTEGER {
sumCtx.iSum += val.Int64()
} else {
sumCtx.approx = true
sumCtx.rSum += val.Float()
}
}
}
func (s *Sum) Final(ctx *AggregateContext) {
if ctx.Data() != nil {
var sumCtx = ctx.Data().(*SumContext)
if sumCtx.count > 0 {
if sumCtx.approx {
ctx.ResultFloat(sumCtx.rSum)
} else {
ctx.ResultInt64(sumCtx.iSum)
}
}
}
}
func (s *Sum) Inverse(ctx *AggregateContext, values ...Value) {
var val = values[0]
var sumCtx = ctx.Data().(*SumContext)
if val.Type() == SQLITE_INTEGER && !sumCtx.approx {
var v = val.Int64()
sumCtx.rSum -= float64(v)
sumCtx.iSum -= v
} else {
sumCtx.rSum -= val.Float()
}
}
func (s *Sum) Value(ctx *AggregateContext) { s.Final(ctx) }
func TestWindowFunction(t *testing.T) {
var err error
Register(func(api *ExtensionApi) (ErrorCode, error) {
if err := api.CreateFunction("sum", &Sum{}); err != nil {
return SQLITE_ERROR, err
}
return SQLITE_OK, nil
})
var db *sql.DB
if db, err = Connect(Memory); err != nil {
t.Fatal(err)
}
defer db.Close()
t.Run("normal aggregation", func(t *testing.T) {
var stmt *sql.Stmt
stmt, err = db.Prepare(`
WITH RECURSIVE generate_series(value) AS (
SELECT 1
UNION ALL
SELECT value+1 FROM generate_series
WHERE value+1<=10
) SELECT SUM(value) FROM generate_series`)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
var rows *sql.Rows
if rows, err = stmt.Query(); err != nil {
t.Fatal(err)
}
defer rows.Close()
if !rows.Next() {
t.Fatal("expected query to return single row")
}
var result int
if err = rows.Scan(&result); err != nil {
t.Fatal(err)
}
if result != 55 {
t.Fatalf("invalid result: got %q", result)
}
})
t.Run("running sum", func(t *testing.T) {
var stmt *sql.Stmt
stmt, err = db.Prepare(`
WITH RECURSIVE generate_series(value) AS (
SELECT 1
UNION ALL
SELECT value+1 FROM generate_series
WHERE value+1<=10
) SELECT SUM(value) OVER(ROWS UNBOUNDED PRECEDING) AS running_total FROM generate_series`)
if err != nil {
t.Fatal(err)
}
defer stmt.Close()
var rows *sql.Rows
if rows, err = stmt.Query(); err != nil {
t.Fatal(err)
}
defer rows.Close()
var series = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var total = 0
for i := 0; rows.Next(); i++ {
total += series[i]
var j int
if err = rows.Scan(&j); err != nil {
t.Fatal(err)
}
if total != j {
t.Fatalf("value mismatch: total(%d) != j(%d)", total, j)
}
}
})
}