Skip to content

Commit f07cf91

Browse files
committed
Start sketching on a rolling window function
1 parent cb34455 commit f07cf91

File tree

27 files changed

+227
-34
lines changed

27 files changed

+227
-34
lines changed

cmd/qfgenerate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"go/format"
88
"os"
99

10-
"github.com/tobgu/qframe/qerrors"
1110
bgenerator "github.com/tobgu/qframe/internal/bcolumn"
1211
egenerator "github.com/tobgu/qframe/internal/ecolumn"
1312
fgenerator "github.com/tobgu/qframe/internal/fcolumn"
1413
igenerator "github.com/tobgu/qframe/internal/icolumn"
1514
qfgenerator "github.com/tobgu/qframe/internal/qframe/generator"
1615
sgenerator "github.com/tobgu/qframe/internal/scolumn"
16+
"github.com/tobgu/qframe/qerrors"
1717
)
1818

1919
/*

config/csv/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ func RowCountHint(rowCount int) ConfigFunc {
9595
}
9696
}
9797

98-
9998
// Headers can be used to specify the header names for a CSV file without header.
10099
//
101100
// header - Slice with column names.

config/eval/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"reflect"
77
"strings"
88

9-
"github.com/tobgu/qframe/qerrors"
109
"github.com/tobgu/qframe/function"
1110
qfstrings "github.com/tobgu/qframe/internal/strings"
11+
"github.com/tobgu/qframe/qerrors"
1212
"github.com/tobgu/qframe/types"
1313
)
1414

config/rolling/config.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package rolling
2+
3+
import "github.com/tobgu/qframe/qerrors"
4+
5+
// DataValue can be any of int/float/*string/bool, eg. any type that a column may take.
6+
type DataValue = interface{}
7+
8+
// IntervalFunc is a function taking two parameters of the same DataValue and returning boolean stating if
9+
// the two values are part of the same interval or not.
10+
//
11+
// For example, x and y within one unit from each other (with x assumed to be <= y):
12+
type IntervalFunc = interface{}
13+
14+
// It should be considered a private implementation detail and should never be
15+
// referenced or used directly outside of the QFrame code. To manipulate it
16+
// use the functions returning ConfigFunc below.
17+
type Config struct {
18+
PadValue DataValue
19+
IntervalColName string
20+
IntervalFunc IntervalFunc
21+
WindowSize int
22+
Position string // center/start/end
23+
}
24+
25+
// ConfigFunc is a function that operates on a Config object.
26+
type ConfigFunc func(c *Config)
27+
28+
func NewConfig(ff []ConfigFunc) (Config, error) {
29+
c := Config{
30+
WindowSize: 1,
31+
Position: "center",
32+
}
33+
34+
for _, fn := range ff {
35+
fn(&c)
36+
}
37+
38+
if c.WindowSize <= 0 {
39+
return c, qerrors.New("Rolling config", "Window size must be positive, was %d", c.WindowSize)
40+
}
41+
42+
if c.Position != "center" && c.Position != "start" && c.Position != "end" {
43+
return c, qerrors.New("Rolling config", "Position must be center/start/end, was %s", c.Position)
44+
}
45+
46+
if c.IntervalFunc != nil && c.WindowSize != 1 {
47+
return c, qerrors.New("Rolling config", "Cannot set both interval function and window size")
48+
}
49+
50+
return c, nil
51+
}
52+
53+
// PadValue can be used to set the value to use in the beginning and/or end of the column to fill out any values
54+
// where fewer than WindowSize values are available.
55+
func PadValue(v DataValue) ConfigFunc {
56+
return func(c *Config) {
57+
c.PadValue = v
58+
}
59+
}
60+
61+
// IntervalFunction can be used to set a dynamic interval based on the content of another column.
62+
// QFrame will include all rows from the start row of the window until (but not including) the first row that is
63+
// not part of the interval according to 'fn'. The first parameter passed to 'fn' is always the value at the start
64+
// of the window.
65+
//
66+
// For example, lets say that you have a time series with millisecond resolution integer timestamps in column 'ts'
67+
// and values in column 'value' that you would like to compute a rolling average over a minute for.
68+
//
69+
// In this case:
70+
// col = "ts", fn = func(tsStart, tsEnd int) bool { return tsEnd < tsStart + int(time.Minute / time.Millisecond)}
71+
func IntervalFunction(colName string, fn IntervalFunc) ConfigFunc {
72+
return func(c *Config) {
73+
c.IntervalColName = colName
74+
c.IntervalFunc = fn
75+
}
76+
}
77+
78+
// WindowSize is used to set the size of the Window. By default this is 1.
79+
func WindowSize(s int) ConfigFunc {
80+
return func(c *Config) {
81+
c.WindowSize = s
82+
}
83+
}
84+
85+
// Position is used to set where in window the resulting value should be inserted.
86+
// Valid values: start/center/end
87+
// Default value: center
88+
func Position(p string) ConfigFunc {
89+
return func(c *Config) {
90+
c.Position = p
91+
}
92+
}

filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/tobgu/qframe/qerrors"
87
"github.com/tobgu/qframe/filter"
98
"github.com/tobgu/qframe/internal/index"
109
"github.com/tobgu/qframe/internal/math/integer"
10+
"github.com/tobgu/qframe/qerrors"
1111
)
1212

1313
// FilterClause is an internal interface representing a filter of some kind that can be applied on a QFrame.
@@ -22,8 +22,8 @@ type FilterClause interface {
2222
type Filter filter.Filter
2323

2424
type comboClause struct {
25-
err error //nolint:structcheck
26-
subClauses []FilterClause //nolint:structcheck
25+
err error //nolint:structcheck
26+
subClauses []FilterClause //nolint:structcheck
2727
}
2828

2929
// AndClause represents the logical conjunction of multiple clauses.

grouper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package qframe
22

33
import (
4-
"github.com/tobgu/qframe/qerrors"
54
"github.com/tobgu/qframe/internal/grouper"
65
"github.com/tobgu/qframe/internal/icolumn"
76
"github.com/tobgu/qframe/internal/index"
7+
"github.com/tobgu/qframe/qerrors"
88
"github.com/tobgu/qframe/types"
99
)
1010

internal/bcolumn/column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"reflect"
55
"strconv"
66

7-
"github.com/tobgu/qframe/qerrors"
87
"github.com/tobgu/qframe/internal/column"
98
"github.com/tobgu/qframe/internal/hash"
109
"github.com/tobgu/qframe/internal/index"
10+
"github.com/tobgu/qframe/qerrors"
1111
"github.com/tobgu/qframe/types"
1212
)
1313

internal/bcolumn/column_gen.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/column/column.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package column
22

33
import (
44
"fmt"
5+
"github.com/tobgu/qframe/config/rolling"
56

67
"github.com/tobgu/qframe/internal/hash"
78
"github.com/tobgu/qframe/internal/index"
@@ -23,6 +24,8 @@ type Column interface {
2324
Apply1(fn interface{}, ix index.Int) (interface{}, error)
2425
Apply2(fn interface{}, s2 Column, ix index.Int) (Column, error)
2526

27+
Rolling(fn interface{}, ix index.Int, config rolling.Config) (Column, error)
28+
2629
FunctionType() types.FunctionType
2730
DataType() types.DataType
2831
}

internal/ecolumn/column.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package ecolumn
22

33
import (
44
"fmt"
5+
"github.com/tobgu/qframe/config/rolling"
56
"reflect"
67
"strings"
78

8-
"github.com/tobgu/qframe/qerrors"
99
"github.com/tobgu/qframe/filter"
1010
"github.com/tobgu/qframe/internal/column"
1111
"github.com/tobgu/qframe/internal/hash"
1212
"github.com/tobgu/qframe/internal/index"
1313
"github.com/tobgu/qframe/internal/scolumn"
1414
qfstrings "github.com/tobgu/qframe/internal/strings"
15+
"github.com/tobgu/qframe/qerrors"
1516
"github.com/tobgu/qframe/types"
1617
)
1718

@@ -567,6 +568,10 @@ func (c Column) View(ix index.Int) View {
567568
return View{column: c, index: ix}
568569
}
569570

571+
func (c Column) Rolling(fn interface{}, ix index.Int, config rolling.Config) (column.Column, error) {
572+
return c, nil
573+
}
574+
570575
func (c Column) FunctionType() types.FunctionType {
571576
return types.FunctionTypeString
572577
}

internal/ecolumn/filters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package ecolumn
22

33
import (
4-
"github.com/tobgu/qframe/qerrors"
54
"github.com/tobgu/qframe/filter"
65
"github.com/tobgu/qframe/internal/index"
76
qfstrings "github.com/tobgu/qframe/internal/strings"
7+
"github.com/tobgu/qframe/qerrors"
88
)
99

1010
var filterFuncs0 = map[string]func(index.Int, []enumVal, index.Bool){

internal/fcolumn/column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"strconv"
77
"unsafe"
88

9-
"github.com/tobgu/qframe/qerrors"
109
"github.com/tobgu/qframe/internal/column"
1110
"github.com/tobgu/qframe/internal/hash"
1211
"github.com/tobgu/qframe/internal/index"
12+
"github.com/tobgu/qframe/qerrors"
1313
"github.com/tobgu/qframe/types"
1414
)
1515

internal/fcolumn/column_gen.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/grouper/grouper.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type tableEntry struct {
2727
}
2828

2929
type table struct {
30-
entries []tableEntry
31-
comparables []column.Comparable
32-
stats GroupStats
33-
hashBuf *hash.MemHash
34-
loadFactor float64
35-
groupCount uint32
36-
collectIx bool
30+
entries []tableEntry
31+
comparables []column.Comparable
32+
stats GroupStats
33+
hashBuf *hash.MemHash
34+
loadFactor float64
35+
groupCount uint32
36+
collectIx bool
3737
}
3838

3939
const growthFactor = 2

internal/hash/memhash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type stringStruct struct {
1616

1717
// MemHash holds a buffer of bytes and allows calculation of a hash over the bytes.
1818
type MemHash struct {
19-
buf []byte
19+
buf []byte
2020
}
2121

2222
// Reset resets the memory buffer

internal/hash/memhash_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Test_SmallIntDistribution(t *testing.T) {
8080
hasher := hash.MemHash{}
8181
for i := 1; i < 177; i++ {
8282
hasher.Reset()
83-
hasher.Write([]byte{0,0,0,0,0,0,0,byte(i)})
83+
hasher.Write([]byte{0, 0, 0, 0, 0, 0, 0, byte(i)})
8484
hash := hasher.Hash()
8585
result[hash] = result[hash] + 1
8686
}

internal/icolumn/column.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package icolumn
22

33
import (
4-
"github.com/tobgu/qframe/qerrors"
54
"github.com/tobgu/qframe/internal/column"
65
"github.com/tobgu/qframe/internal/hash"
76
"github.com/tobgu/qframe/internal/index"
7+
"github.com/tobgu/qframe/qerrors"
88
"github.com/tobgu/qframe/types"
99
"reflect"
1010
"strconv"

internal/icolumn/column_gen.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/io/csv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"io"
55
"math"
66

7-
"github.com/tobgu/qframe/qerrors"
87
"github.com/tobgu/qframe/internal/ecolumn"
98
"github.com/tobgu/qframe/internal/fastcsv"
109
"github.com/tobgu/qframe/internal/ncolumn"
1110
"github.com/tobgu/qframe/internal/strings"
11+
"github.com/tobgu/qframe/qerrors"
1212
"github.com/tobgu/qframe/types"
1313
)
1414

internal/io/sql/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"reflect"
66

7-
"github.com/tobgu/qframe/qerrors"
87
"github.com/tobgu/qframe/internal/bcolumn"
98
"github.com/tobgu/qframe/internal/column"
109
"github.com/tobgu/qframe/internal/ecolumn"
1110
"github.com/tobgu/qframe/internal/fcolumn"
1211
"github.com/tobgu/qframe/internal/icolumn"
1312
"github.com/tobgu/qframe/internal/index"
1413
"github.com/tobgu/qframe/internal/scolumn"
14+
"github.com/tobgu/qframe/qerrors"
1515
)
1616

1717
type SQLConfig struct {

0 commit comments

Comments
 (0)