-
Notifications
You must be signed in to change notification settings - Fork 30
/
column.go
133 lines (112 loc) · 3.15 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
package bcolumn
import (
"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"
"reflect"
"strconv"
)
func (c Comparable) Compare(i, j uint32) column.CompareResult {
x, y := c.data[i], c.data[j]
if x == y {
return column.Equal
}
if x {
return c.gtValue
}
return c.ltValue
}
func (c Comparable) Hash(i uint32, seed uint64) uint64 {
if c.data[i] {
b := [1]byte{1}
return hash.HashBytes(b[:], seed)
}
b := [1]byte{0}
return hash.HashBytes(b[:], seed)
}
func (c Column) DataType() types.DataType {
return types.Bool
}
func (c Column) StringAt(i uint32, _ string) string {
return strconv.FormatBool(c.data[i])
}
func (c Column) AppendByteStringAt(buf []byte, i uint32) []byte {
return strconv.AppendBool(buf, c.data[i])
}
func (c Column) ByteSize() int {
// Slice header + data
return 2*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 {
if c.data[x] != otherI.data[otherIndex[ix]] {
return false
}
}
return true
}
func (c Column) filterBuiltIn(index index.Int, comparator string, comparatee interface{}, bIndex index.Bool) error {
switch t := comparatee.(type) {
case bool:
compFunc, ok := filterFuncs[comparator]
if !ok {
return qerrors.New("filter bool", "invalid comparison operator for bool, %v", comparator)
}
compFunc(index, c.data, t, bIndex)
case Column:
compFunc, ok := filterFuncs2[comparator]
if !ok {
return qerrors.New("filter bool", "invalid comparison operator for bool, %v", comparator)
}
compFunc(index, c.data, t.data, bIndex)
default:
return qerrors.New("filter bool", "invalid comparison value type %v", reflect.TypeOf(comparatee))
}
return nil
}
func (c Column) filterCustom1(index index.Int, fn func(bool) 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(bool, bool) bool, comparatee interface{}, bIndex index.Bool) error {
otherC, ok := comparatee.(Column)
if !ok {
return qerrors.New("filter bool", "expected comparatee to be bool 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(bool) bool:
c.filterCustom1(index, t, bIndex)
case func(bool, bool) bool:
err = c.filterCustom2(index, t, comparatee, bIndex)
default:
err = qerrors.New("filter bool", "invalid filter type %v", reflect.TypeOf(comparator))
}
return err
}
func (c Column) FunctionType() types.FunctionType {
return types.FunctionTypeBool
}
func (c Column) Append(cols ...column.Column) (column.Column, error) {
// TODO Append
return nil, qerrors.New("Append", "Not implemented yet")
}