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
|
package proto
import (
"github.com/go-faster/errors"
)
// Compile-time assertions for Array.
var (
_ ColInput = NewArray[string]((*ColStr)(nil))
_ ColResult = NewArray[string]((*ColStr)(nil))
_ Column = NewArray[string]((*ColStr)(nil))
_ StateEncoder = NewArray[string]((*ColStr)(nil))
_ StateDecoder = NewArray[string]((*ColStr)(nil))
_ Inferable = NewArray[string]((*ColStr)(nil))
_ Preparable = NewArray[string]((*ColStr)(nil))
)
// Arrayable constraint specifies ability of column T to be Array(T).
type Arrayable[T any] interface {
Array() *ColArr[T]
}
// ColArr is Array(T).
type ColArr[T any] struct {
Offsets ColUInt64
Data ColumnOf[T]
}
// NewArray returns ColArr of c.
//
// Example: NewArray[string](new(ColStr))
func NewArray[T any](c ColumnOf[T]) *ColArr[T] {
return &ColArr[T]{
Data: c,
}
}
// Type returns type of array, i.e. Array(T).
func (c ColArr[T]) Type() ColumnType {
return ColumnTypeArray.Sub(c.Data.Type())
}
// Rows returns rows count.
func (c ColArr[T]) Rows() int {
return c.Offsets.Rows()
}
func (c *ColArr[T]) DecodeState(r *Reader) error {
if s, ok := c.Data.(StateDecoder); ok {
if err := s.DecodeState(r); err != nil {
return errors.Wrap(err, "data state")
}
}
return nil
}
func (c *ColArr[T]) EncodeState(b *Buffer) {
if s, ok := c.Data.(StateEncoder); ok {
s.EncodeState(b)
}
}
// Prepare ensures Preparable column propagation.
func (c *ColArr[T]) Prepare() error {
if v, ok := c.Data.(Preparable); ok {
if err := v.Prepare(); err != nil {
return errors.Wrap(err, "prepare data")
}
}
return nil
}
// Infer ensures Inferable column propagation.
func (c *ColArr[T]) Infer(t ColumnType) error {
if v, ok := c.Data.(Inferable); ok {
if err := v.Infer(t.Elem()); err != nil {
return errors.Wrap(err, "infer data")
}
}
return nil
}
// RowAppend appends i-th row to target and returns it.
func (c ColArr[T]) RowAppend(i int, target []T) []T {
var start int
end := int(c.Offsets[i])
if i > 0 {
start = int(c.Offsets[i-1])
}
for idx := start; idx < end; idx++ {
target = append(target, c.Data.Row(idx))
}
return target
}
// Row returns i-th row.
func (c ColArr[T]) Row(i int) []T {
return c.RowAppend(i, nil)
}
// DecodeColumn implements ColResult.
func (c *ColArr[T]) DecodeColumn(r *Reader, rows int) error {
if err := c.Offsets.DecodeColumn(r, rows); err != nil {
return errors.Wrap(err, "read offsets")
}
var size int
if l := len(c.Offsets); l > 0 {
// Pick last offset as total size of "elements" column.
size = int(c.Offsets[l-1])
}
if err := checkRows(size); err != nil {
return errors.Wrap(err, "array size")
}
if err := c.Data.DecodeColumn(r, size); err != nil {
return errors.Wrap(err, "decode data")
}
return nil
}
// Reset implements ColResult.
func (c *ColArr[T]) Reset() {
c.Data.Reset()
c.Offsets.Reset()
}
// EncodeColumn implements ColInput.
func (c ColArr[T]) EncodeColumn(b *Buffer) {
c.Offsets.EncodeColumn(b)
c.Data.EncodeColumn(b)
}
// Append appends new row to column.
func (c *ColArr[T]) Append(v []T) {
c.Data.AppendArr(v)
c.Offsets = append(c.Offsets, uint64(c.Data.Rows()))
}
// AppendArr appends new slice of rows to column.
func (c *ColArr[T]) AppendArr(vs [][]T) {
for _, v := range vs {
c.Data.AppendArr(v)
c.Offsets = append(c.Offsets, uint64(c.Data.Rows()))
}
}
// Result for current column.
func (c *ColArr[T]) Result(column string) ResultColumn {
return ResultColumn{Name: column, Data: c}
}
// Results return Results containing single column.
func (c *ColArr[T]) Results(column string) Results {
return Results{c.Result(column)}
}
|