-
Notifications
You must be signed in to change notification settings - Fork 4
/
row_based_interface.go
93 lines (80 loc) · 1.71 KB
/
row_based_interface.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
package main
type Operator interface {
next() []Datum
}
type Datum interface{}
type mulFn func(a Datum, b Datum) Datum
func mulIntDatums(a Datum, b Datum) Datum {
aInt := a.(Int).int64
bInt := b.(Int).int64
return Int{int64: aInt * bInt}
}
func mulFloat64Datums(a Datum, b Datum) Datum {
aFloat := a.(Float64).float64
bFloat := b.(Float64).float64
return Float64{float64: aFloat * bFloat}
}
// Int implements the Datum interface.
type Int struct {
int64
}
// Float64 implements the Datum interface.
type Float64 struct {
float64
}
type mulOperator struct {
input Operator
fn mulFn
arg Datum
columnsToMultiply []int
}
func (m mulOperator) next() []Datum {
row := m.input.next()
if row == nil {
return nil
}
for _, c := range m.columnsToMultiply {
row[c] = m.fn(row[c], m.arg)
}
return row
}
type tableReader struct {
curIdx int
rows [][]Datum
}
func (t *tableReader) next() []Datum {
if t.curIdx >= len(t.rows) {
return nil
}
row := t.rows[t.curIdx]
t.curIdx++
return row
}
func (t *tableReader) reset() {
t.curIdx = 0
}
// makeInput creates numRows rows of numCols each of the given type. For each
// row, all of its columns will be its index (zero-indexed).
func makeInput(numRows int, numCols int, t Datum) [][]Datum {
result := make([][]Datum, numRows)
for i := range result {
result[i] = make([]Datum, numCols)
}
switch t.(type) {
case Int:
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
result[i][j] = Int{int64: int64(i)}
}
}
case Float64:
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
result[i][j] = Float64{float64: float64(i)}
}
}
default:
panic("unhandled type")
}
return result
}