-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrow_based_typed_batch.go
93 lines (83 loc) · 1.95 KB
/
row_based_typed_batch.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
const batchSize = 1024
type TypedBatchOperator interface {
next() [][]TypedDatum
}
type mulInt64BatchOperator struct {
input TypedBatchOperator
arg int64
columnsToMultiply []int
}
func (m mulInt64BatchOperator) next() [][]TypedDatum {
rows := m.input.next()
if rows == nil {
return nil
}
for _, row := range rows {
for _, c := range m.columnsToMultiply {
row[c] = TypedDatum{t: Int64Type, int64: row[c].int64 * m.arg}
}
}
return rows
}
type mulFloat64BatchOperator struct {
input TypedBatchOperator
arg float64
columnsToMultiply []int
}
func (m mulFloat64BatchOperator) next() [][]TypedDatum {
rows := m.input.next()
if rows == nil {
return nil
}
for _, row := range rows {
for _, c := range m.columnsToMultiply {
row[c] = TypedDatum{t: Float64Type, float64: row[c].float64 * m.arg}
}
}
return rows
}
type typedBatchTableReader struct {
curIdx int
rows [][]TypedDatum
}
func (t *typedBatchTableReader) next() [][]TypedDatum {
if t.curIdx >= len(t.rows) {
return nil
}
endIdx := t.curIdx + batchSize
if endIdx > len(t.rows) {
endIdx = len(t.rows)
}
retRows := t.rows[t.curIdx:endIdx]
t.curIdx = endIdx
return retRows
}
func (t *typedBatchTableReader) reset() {
t.curIdx = 0
}
// makeTypedInput creates numRows rows of numCols each of the given type. For
// each row, all of its columns will be its index (zero-indexed).
func makeTypedBatchInput(numRows int, numCols int, t T) [][]TypedDatum {
result := make([][]TypedDatum, numRows)
for i := range result {
result[i] = make([]TypedDatum, numCols)
}
switch t {
case Int64Type:
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
result[i][j] = TypedDatum{t: t, int64: int64(i)}
}
}
case Float64Type:
for i := 0; i < numRows; i++ {
for j := 0; j < numCols; j++ {
result[i][j] = TypedDatum{t: t, float64: float64(i)}
}
}
default:
panic("unhandled type")
}
return result
}