-
Notifications
You must be signed in to change notification settings - Fork 30
/
column.go
176 lines (149 loc) · 4.22 KB
/
column.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package fcolumn
import (
"github.com/tobgu/qframe/internal/ryu"
"math"
"math/rand"
"reflect"
"strconv"
"unsafe"
"github.com/tobgu/qframe/internal/column"
"github.com/tobgu/qframe/internal/hash"
"github.com/tobgu/qframe/internal/index"
"github.com/tobgu/qframe/qerrors"
"github.com/tobgu/qframe/types"
)
func (c Column) DataType() types.DataType {
return types.Float
}
func (c Column) StringAt(i uint32, naRep string) string {
value := c.data[i]
if math.IsNaN(value) {
return naRep
}
return strconv.FormatFloat(c.data[i], 'f', -1, 64)
}
func (c Column) AppendByteStringAt(buf []byte, i uint32) []byte {
value := c.data[i]
if math.IsNaN(value) {
return append(buf, "null"...)
}
return ryu.AppendFloat64f(buf, value)
}
func (c Column) ByteSize() int {
// Slice header + data
return 2*8 + 8*cap(c.data)
}
func (c Column) Equals(index index.Int, other column.Column, otherIndex index.Int) bool {
otherI, ok := other.(Column)
if !ok {
return false
}
for ix, x := range index {
v1, v2 := c.data[x], otherI.data[otherIndex[ix]]
if v1 != v2 {
// NaN != NaN but for our purposes they are the same
if !(math.IsNaN(v1) && math.IsNaN(v2)) {
return false
}
}
}
return true
}
func (c Comparable) Compare(i, j uint32) column.CompareResult {
x, y := c.data[i], c.data[j]
if x < y {
return c.ltValue
}
if x > y {
return c.gtValue
}
if math.IsNaN(x) || math.IsNaN(y) {
if !math.IsNaN(x) {
return c.nullGtValue
}
if !math.IsNaN(y) {
return c.nullLtValue
}
return c.equalNullValue
}
return column.Equal
}
func (c Comparable) Hash(i uint32, seed uint64) uint64 {
f := c.data[i]
if math.IsNaN(f) && c.equalNullValue == column.NotEqual {
// Use a random value here to avoid hash collisions when
// we don't consider null to equal null.
return rand.Uint64()
}
bits := math.Float64bits(c.data[i])
b := (*[8]byte)(unsafe.Pointer(&bits))[:]
return hash.HashBytes(b, seed)
}
func (c Column) filterBuiltIn(index index.Int, comparator string, comparatee interface{}, bIndex index.Bool) error {
switch t := comparatee.(type) {
case float64:
if math.IsNaN(t) {
return qerrors.New("filter float", "NaN not allowed as filter argument")
}
compFunc, ok := filterFuncs1[comparator]
if !ok {
return qerrors.New("filter float", "invalid comparison operator to single argument filter, %v", comparator)
}
compFunc(index, c.data, t, bIndex)
case Column:
compFunc, ok := filterFuncs2[comparator]
if !ok {
return qerrors.New("filter float", "invalid comparison operator to column - column filter, %v", comparator)
}
compFunc(index, c.data, t.data, bIndex)
case nil:
compFunc, ok := filterFuncs0[comparator]
if !ok {
return qerrors.New("filter float", "invalid comparison operator to zero argument filter, %v", comparator)
}
compFunc(index, c.data, bIndex)
default:
return qerrors.New("filter float", "invalid comparison value type %v", reflect.TypeOf(comparatee))
}
return nil
}
func (c Column) filterCustom1(index index.Int, fn func(float64) bool, bIndex index.Bool) {
for i, x := range bIndex {
if !x {
bIndex[i] = fn(c.data[index[i]])
}
}
}
func (c Column) filterCustom2(index index.Int, fn func(float64, float64) bool, comparatee interface{}, bIndex index.Bool) error {
otherC, ok := comparatee.(Column)
if !ok {
return qerrors.New("filter float", "expected comparatee to be float column, was %v", reflect.TypeOf(comparatee))
}
for i, x := range bIndex {
if !x {
bIndex[i] = fn(c.data[index[i]], otherC.data[index[i]])
}
}
return nil
}
func (c Column) Filter(index index.Int, comparator interface{}, comparatee interface{}, bIndex index.Bool) error {
var err error
switch t := comparator.(type) {
case string:
err = c.filterBuiltIn(index, t, comparatee, bIndex)
case func(float64) bool:
c.filterCustom1(index, t, bIndex)
case func(float64, float64) bool:
err = c.filterCustom2(index, t, comparatee, bIndex)
default:
err = qerrors.New("filter float", "invalid filter type %v", reflect.TypeOf(comparator))
}
return err
}
func (c Column) FunctionType() types.FunctionType {
return types.FunctionTypeFloat
}
func (c Column) Append(cols ...column.Column) (column.Column, error) {
// TODO Append
return nil, qerrors.New("Append", "Not implemented yet")
}