Skip to content

Commit

Permalink
feat(proto): add ColArr.AppendLowCardinality
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
ernado committed Jan 29, 2022
1 parent c9269f2 commit c32172b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions proto/_golden/col_arr_low_cardinality_u8_str.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
00000000 03 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 |................|
00000010 06 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 |................|
00000020 0c 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 |................|
00000030 00 06 00 00 00 00 00 00 03 00 00 00 00 00 00 00 |................|
00000040 03 66 6f 6f 03 62 61 72 03 62 61 7a 0c 00 00 00 |.foo.bar.baz....|
00000050 00 00 00 00 00 01 02 00 01 01 00 00 01 01 01 01 |................|
Binary file added proto/_golden/col_arr_low_cardinality_u8_str.raw
Binary file not shown.
8 changes: 8 additions & 0 deletions proto/col_low_cardinality.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (c *ColLowCardinality) AppendKey(i int) {
}
}

func (c *ColArr) AppendLowCardinality(data []int) {
d := c.Data.(*ColLowCardinality)
for _, v := range data {
d.AppendKey(v)
}
c.Offsets = append(c.Offsets, uint64(d.Keys().Rows()))
}

func (c *ColLowCardinality) Keys() Column {
switch c.Key {
case KeyUInt8:
Expand Down
69 changes: 69 additions & 0 deletions proto/col_low_cardinality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,75 @@ import (
"github.com/go-faster/ch/internal/gold"
)

func TestArrLowCardinalityStr(t *testing.T) {
// Array(LowCardinality(String))
data := [][]string{
{"foo", "bar", "baz"},
{"foo"},
{"bar", "bar"},
{"foo", "foo"},
{"bar", "bar", "bar", "bar"},
}
str := &ColStr{}
idx := &ColLowCardinality{Index: str, Key: KeyUInt8}
col := &ColArr{Data: idx}
rows := len(data)

kv := map[string]int{} // creating index
for _, v := range data {
for _, s := range v {
if _, ok := kv[s]; ok {
continue
}
kv[s] = str.Rows()
str.Append(s)
}
}
for _, v := range data {
var keys []int
for _, k := range v {
keys = append(keys, kv[k]) // mapping indexes
}
col.AppendLowCardinality(keys) // adding row
}

var buf Buffer
col.EncodeColumn(&buf)
t.Run("Golden", func(t *testing.T) {
gold.Bytes(t, buf.Buf, "col_arr_low_cardinality_u8_str")
})
t.Run("Ok", func(t *testing.T) {
br := bytes.NewReader(buf.Buf)
r := NewReader(br)

strDec := &ColStr{}
idxDec := &ColLowCardinality{Index: strDec}
dec := &ColArr{Data: idxDec}

require.NoError(t, dec.DecodeColumn(r, rows))
require.Equal(t, col, dec)
require.Equal(t, str, strDec)
require.Equal(t, idx, idxDec)
require.Equal(t, rows, dec.Rows())
dec.Reset()
require.Equal(t, 0, dec.Rows())
require.Equal(t, ColumnType("Array(LowCardinality(String))"), dec.Type())
})
t.Run("ErrUnexpectedEOF", func(t *testing.T) {
r := NewReader(bytes.NewReader(nil))
strDec := &ColStr{}
idxDec := &ColLowCardinality{Index: strDec}
dec := &ColArr{Data: idxDec}
require.ErrorIs(t, dec.DecodeColumn(r, rows), io.ErrUnexpectedEOF)
})
t.Run("NoShortRead", func(t *testing.T) {
strDec := &ColStr{}
idxDec := &ColLowCardinality{Index: strDec}
dec := &ColArr{Data: idxDec}
requireNoShortRead(t, buf.Buf, colAware(dec, rows))
})
}

func TestColLowCardinality_DecodeColumn(t *testing.T) {
t.Run("Str", func(t *testing.T) {
const rows = 25
Expand Down

0 comments on commit c32172b

Please sign in to comment.