Skip to content

Commit

Permalink
feat(proto): implement WriteColumn for non-generated types
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Aug 16, 2024
1 parent e0e9309 commit 16fbe2d
Show file tree
Hide file tree
Showing 23 changed files with 167 additions and 0 deletions.
6 changes: 6 additions & 0 deletions proto/col_arr.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func (c ColArr[T]) EncodeColumn(b *Buffer) {
c.Data.EncodeColumn(b)
}

// WriteColumn implements ColInput.
func (c ColArr[T]) WriteColumn(w *Writer) {
c.Offsets.WriteColumn(w)
c.Data.WriteColumn(w)
}

// Append appends new row to column.
func (c *ColArr[T]) Append(v []T) {
c.Data.AppendArr(v)
Expand Down
3 changes: 3 additions & 0 deletions proto/col_arr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func testColumn[T any](t *testing.T, name string, f func() ColumnOf[T], values .
t.Run("Golden", func(t *testing.T) {
gold.Bytes(t, buf.Buf, "column_of_"+name)
})
t.Run("WriteColumn", checkWriteColumn(data))
t.Run("Ok", func(t *testing.T) {
br := bytes.NewReader(buf.Buf)
r := NewReader(br)
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestColArrOfStr(t *testing.T) {
dec := (&ColStr{}).Array()
requireNoShortRead(t, buf.Buf, colAware(dec, col.Rows()))
})
t.Run("WriteColumn", checkWriteColumn(col))
}

func TestArrOfLowCordStr(t *testing.T) {
Expand Down Expand Up @@ -118,6 +120,7 @@ func TestArrOfLowCordStr(t *testing.T) {
dec := NewArray[string](new(ColStr).LowCardinality())
requireNoShortRead(t, buf.Buf, colAware(dec, col.Rows()))
})
t.Run("WriteColumn", checkWriteColumn(col))
}

func TestColArr_DecodeColumn(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions proto/col_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@ func (c ColAuto) Reset() {
func (c ColAuto) EncodeColumn(b *Buffer) {
c.Data.EncodeColumn(b)
}

func (c ColAuto) WriteColumn(w *Writer) {
c.Data.WriteColumn(w)
}
1 change: 1 addition & 0 deletions proto/col_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestColBool_DecodeColumn(t *testing.T) {
var dec ColBool
requireNoShortRead(t, buf.Buf, colAware(&dec, rows))
})
t.Run("WriteColumn", checkWriteColumn(data))
}

func BenchmarkColBool_DecodeColumn(b *testing.B) {
Expand Down
10 changes: 10 additions & 0 deletions proto/col_bool_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ func (c *ColBool) DecodeColumn(r *Reader, rows int) error {
}
return nil
}

// WriteColumn writes Bool rows to *Writer.
func (c ColBool) WriteColumn(w *Writer) {
if len(c) == 0 {
return
}
s := *(*slice)(unsafe.Pointer(&c)) // #nosec G103
src := *(*[]byte)(unsafe.Pointer(&s)) // #nosec G103
w.ChainWrite(src)
}
4 changes: 4 additions & 0 deletions proto/col_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,8 @@ func (e *ColEnum) EncodeColumn(b *Buffer) {
e.raw().EncodeColumn(b)
}

func (e *ColEnum) WriteColumn(w *Writer) {
e.raw().WriteColumn(w)
}

func (e *ColEnum) Type() ColumnType { return e.t }
5 changes: 5 additions & 0 deletions proto/col_fixed_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func (c *ColFixedStr) DecodeColumn(r *Reader, rows int) error {
return nil
}

// WriteColumn writes ColFixedStr rows to *Writer.
func (c ColFixedStr) WriteColumn(w *Writer) {
w.ChainWrite(c.Buf)
}

// Array returns new Array(FixedString).
func (c *ColFixedStr) Array() *ColArr[[]byte] {
return &ColArr[[]byte]{
Expand Down
1 change: 1 addition & 0 deletions proto/col_fixed_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestColFixedStr_EncodeColumn(t *testing.T) {
require.Equal(t, 10, v.Size)
})
})
t.Run("WriteColumn", checkWriteColumn(data))
}

func BenchmarkColFixedStr_DecodeColumn(b *testing.B) {
Expand Down
4 changes: 4 additions & 0 deletions proto/col_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,7 @@ func (c *ColInterval) Reset() {
func (c ColInterval) EncodeColumn(b *Buffer) {
c.Values.EncodeColumn(b)
}

func (c ColInterval) WriteColumn(w *Writer) {
c.Values.WriteColumn(w)
}
1 change: 1 addition & 0 deletions proto/col_interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ func TestColInterval(t *testing.T) {
var v ColInterval
v.EncodeColumn(nil) // should be no-op
})
t.Run("WriteColumn", checkWriteColumn(data))
}
35 changes: 35 additions & 0 deletions proto/col_low_cardinality.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,41 @@ func (c *ColLowCardinality[T]) EncodeColumn(b *Buffer) {
}
}

func (c *ColLowCardinality[T]) WriteColumn(w *Writer) {
// Using pointer receiver as Prepare() is expected to be called before
// encoding.

if c.Rows() == 0 {
// Skipping encoding entirely.
return
}

w.ChainBuffer(func(b *Buffer) {
// Meta encodes whether reader should update
// low cardinality metadata and keys column type.
meta := cardinalityUpdateAll | int64(c.key)
b.PutInt64(meta)

// Writing index (dictionary).
b.PutInt64(int64(c.index.Rows()))
})
c.index.WriteColumn(w)

w.ChainBuffer(func(b *Buffer) {
b.PutInt64(int64(c.Rows()))
})
switch c.key {
case KeyUInt8:
c.keys8.WriteColumn(w)
case KeyUInt16:
c.keys16.WriteColumn(w)
case KeyUInt32:
c.keys32.WriteColumn(w)
case KeyUInt64:
c.keys64.WriteColumn(w)
}
}

func (c *ColLowCardinality[T]) Reset() {
for k := range c.kv {
delete(c.kv, k)
Expand Down
25 changes: 25 additions & 0 deletions proto/col_low_cardinality_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,28 @@ func (c ColLowCardinalityRaw) EncodeColumn(b *Buffer) {
b.PutInt64(int64(k.Rows()))
k.EncodeColumn(b)
}

func (c ColLowCardinalityRaw) WriteColumn(w *Writer) {
if c.Rows() == 0 {
// Skipping encoding entirely.
return
}

w.ChainBuffer(func(b *Buffer) {
// Meta encodes whether reader should update
// low cardinality metadata and keys column type.
meta := cardinalityUpdateAll | int64(c.Key)
b.PutInt64(meta)

// Writing index (dictionary).
b.PutInt64(int64(c.Index.Rows()))
})
c.Index.WriteColumn(w)

// Sequence of values as indexes in dictionary.
k := c.Keys()
w.ChainBuffer(func(b *Buffer) {
b.PutInt64(int64(k.Rows()))
})
k.WriteColumn(w)
}
1 change: 1 addition & 0 deletions proto/col_low_cardinality_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestColLowCardinalityRaw_DecodeColumn(t *testing.T) {
}
requireNoShortRead(t, buf.Buf, colAware(dec, rows))
})
t.Run("WriteColumn", checkWriteColumn(col))
})
t.Run("Blank", func(t *testing.T) {
// Blank columns (i.e. row count is zero) are not encoded.
Expand Down
1 change: 1 addition & 0 deletions proto/col_low_cardinality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestArrLowCardinalityStr(t *testing.T) {
dec := new(ColStr).LowCardinality().Array()
requireNoShortRead(t, buf.Buf, colAware(dec, rows))
})
t.Run("WriteColumn", checkWriteColumn(col))
}

func TestColLowCardinality_DecodeColumn(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions proto/col_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ func (c ColMap[K, V]) EncodeColumn(b *Buffer) {
c.Values.EncodeColumn(b)
}

func (c ColMap[K, V]) WriteColumn(w *Writer) {
if c.Rows() == 0 {
return
}

c.Offsets.WriteColumn(w)
c.Keys.WriteColumn(w)
c.Values.WriteColumn(w)
}

// Prepare ensures Preparable column propagation.
func (c ColMap[K, V]) Prepare() error {
if v, ok := c.Keys.(Preparable); ok {
Expand Down
4 changes: 4 additions & 0 deletions proto/col_nothing.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ func (c ColNothing) EncodeColumn(b *Buffer) {
}
b.PutRaw(make([]byte, c))
}

func (c ColNothing) WriteColumn(w *Writer) {
w.ChainBuffer(c.EncodeColumn)
}
1 change: 1 addition & 0 deletions proto/col_nothing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ func TestColNothing(t *testing.T) {
var v ColNothing
v.EncodeColumn(nil) // should be no-op
})
t.Run("WriteColumn", checkWriteColumn(ColNothing(0)))
}
5 changes: 5 additions & 0 deletions proto/col_nullable.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (c ColNullable[T]) EncodeColumn(b *Buffer) {
c.Values.EncodeColumn(b)
}

func (c ColNullable[T]) WriteColumn(w *Writer) {
c.Nulls.WriteColumn(w)
c.Values.WriteColumn(w)
}

func (c ColNullable[T]) IsElemNull(i int) bool {
if i < c.Rows() {
return c.Nulls[i] == boolTrue
Expand Down
5 changes: 5 additions & 0 deletions proto/col_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ func (c ColPoint) EncodeColumn(b *Buffer) {
c.X.EncodeColumn(b)
c.Y.EncodeColumn(b)
}

func (c ColPoint) WriteColumn(w *Writer) {
c.X.WriteColumn(w)
c.Y.WriteColumn(w)
}
14 changes: 14 additions & 0 deletions proto/col_raw_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ func (c *ColRawOf[X]) DecodeColumn(r *Reader, rows int) error {
}
return nil
}

// WriteColumn write ColRawOf rows to *Writer.
func (c ColRawOf[X]) WriteColumn(w *Writer) {
if len(c) == 0 {
return
}
var x X
size := unsafe.Sizeof(x) // #nosec G103
s := *(*slice)(unsafe.Pointer(&c)) // #nosec G103
s.Len *= size
s.Cap *= size
src := *(*[]byte)(unsafe.Pointer(&s)) // #nosec G103
w.ChainWrite(src)
}
12 changes: 12 additions & 0 deletions proto/col_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func (c ColStr) EncodeColumn(b *Buffer) {
}
}

// WriteColumn writes String rows to *Writer.
func (c ColStr) WriteColumn(w *Writer) {
buf := make([]byte, binary.MaxVarintLen64)
for _, p := range c.Pos {
w.ChainBuffer(func(b *Buffer) {
n := binary.PutUvarint(buf, uint64(p.End-p.Start))
b.PutRaw(buf[:n])
})
w.ChainWrite(c.Buf[p.Start:p.End])
}
}

// ForEach calls f on each string from column.
func (c ColStr) ForEach(f func(i int, s string) error) error {
return c.ForEachBytes(func(i int, b []byte) error {
Expand Down
6 changes: 6 additions & 0 deletions proto/col_tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,9 @@ func (c ColTuple) EncodeColumn(b *Buffer) {
v.EncodeColumn(b)
}
}

func (c ColTuple) WriteColumn(w *Writer) {
for _, v := range c {
v.WriteColumn(w)
}
}
9 changes: 9 additions & 0 deletions proto/col_uuid_unsafe.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ func (c ColUUID) EncodeColumn(b *Buffer) {
copy(dst, src)
bswap.Swap64(dst) // BE <-> LE
}

// WriteColumn encodes ColUUID rows to *Writer.
func (c ColUUID) WriteColumn(w *Writer) {
if len(c) == 0 {
return
}
// Can't write UUID as-is: bswap is required.
w.ChainBuffer(c.EncodeColumn)
}

0 comments on commit 16fbe2d

Please sign in to comment.