Skip to content

Commit b21c69d

Browse files
committed
Fix mode.
1 parent b0f8ff4 commit b21c69d

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

ext/stats/mode.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type mode struct {
1919

2020
func (m mode) Value(ctx sqlite3.Context) {
2121
var (
22-
max = 0
2322
typ = sqlite3.NULL
23+
max uint
2424
i64 int64
2525
f64 float64
2626
str string
@@ -32,7 +32,6 @@ func (m mode) Value(ctx sqlite3.Context) {
3232
i64 = k
3333
}
3434
}
35-
f64 = float64(i64)
3635
for k, v := range m.reals {
3736
if v > max || v == max && k < f64 {
3837
typ = sqlite3.FLOAT
@@ -66,33 +65,45 @@ func (m mode) Value(ctx sqlite3.Context) {
6665
}
6766
}
6867

69-
func (b *mode) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {
68+
func (m *mode) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {
7069
switch arg[0].Type() {
7170
case sqlite3.INTEGER:
72-
b.ints.add(arg[0].Int64())
71+
if m.reals == nil {
72+
m.ints.add(arg[0].Int64())
73+
break
74+
}
75+
fallthrough
7376
case sqlite3.FLOAT:
74-
b.reals.add(arg[0].Float())
77+
m.reals.add(arg[0].Float())
78+
for k, v := range m.ints {
79+
m.reals[float64(k)] += v
80+
}
81+
m.ints = nil
7582
case sqlite3.TEXT:
76-
b.texts.add(arg[0].Text())
83+
m.texts.add(arg[0].Text())
7784
case sqlite3.BLOB:
78-
b.blobs.add(string(arg[0].RawBlob()))
85+
m.blobs.add(string(arg[0].RawBlob()))
7986
}
8087
}
8188

82-
func (b *mode) Inverse(ctx sqlite3.Context, arg ...sqlite3.Value) {
89+
func (m *mode) Inverse(ctx sqlite3.Context, arg ...sqlite3.Value) {
8390
switch arg[0].Type() {
8491
case sqlite3.INTEGER:
85-
b.ints.del(arg[0].Int64())
92+
if m.reals == nil {
93+
m.ints.del(arg[0].Int64())
94+
break
95+
}
96+
fallthrough
8697
case sqlite3.FLOAT:
87-
b.reals.del(arg[0].Float())
98+
m.reals.del(arg[0].Float())
8899
case sqlite3.TEXT:
89-
b.texts.del(arg[0].Text())
100+
m.texts.del(arg[0].Text())
90101
case sqlite3.BLOB:
91-
b.blobs.del(string(arg[0].RawBlob()))
102+
m.blobs.del(string(arg[0].RawBlob()))
92103
}
93104
}
94105

95-
type counter[T comparable] map[T]int
106+
type counter[T comparable] map[T]uint
96107

97108
func (c *counter[T]) add(k T) {
98109
if (*c) == nil {
@@ -102,11 +113,9 @@ func (c *counter[T]) add(k T) {
102113
}
103114

104115
func (c counter[T]) del(k T) {
105-
switch n := c[k]; n {
106-
default:
107-
c[k] = n - 1
108-
case 1:
116+
if n := c[k]; n == 1 {
109117
delete(c, k)
110-
case 0:
118+
} else {
119+
c[k] = n - 1
111120
}
112121
}

ext/stats/mode_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,20 @@ func TestRegister_mode(t *testing.T) {
8282
for stmt.Step() {
8383
}
8484
stmt.Close()
85+
86+
stmt, _, err = db.Prepare(`SELECT mode(column1) FROM (VALUES (?), (?), (?), (?), (?))`)
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
stmt.BindInt(1, 1)
91+
stmt.BindInt(2, 1)
92+
stmt.BindInt(3, 2)
93+
stmt.BindFloat(4, 2)
94+
stmt.BindFloat(5, 2)
95+
if stmt.Step() {
96+
if got := stmt.ColumnInt(0); got != 2 {
97+
t.Errorf("got %v, want 2", got)
98+
}
99+
}
100+
stmt.Close()
85101
}

0 commit comments

Comments
 (0)