ClickHouse/ch-go

proto.ColArr[proto.ColFixedStr]

Closed this issue · 4 comments

jootd commented

Hi, Why ColFixedStr struct does not implements Arrayable interface ?

Hi, you can still use NewArray:

v := proto.NewArray[[]byte](&proto.ColFixedStr{Size: 10})

v.Append([][]byte{
	bytes.Repeat([]byte("a"), 10),
	bytes.Repeat([]byte("b"), 10),
	bytes.Repeat([]byte("c"), 10),
})
v.Append([][]byte{
	bytes.Repeat([]byte("d"), 10),
	bytes.Repeat([]byte("e"), 10),
	bytes.Repeat([]byte("f"), 10),
})

Your type will be

proto.ColArr[[]byte]

Not

proto.ColArr[proto.ColFixedStr]

Like this:

// Array returns new Array(FixedString).
func (c *ColFixedStr) Array() *ColArr[[]byte] {
	return &ColArr[[]byte]{
		Data: c,
	}
}
jootd commented

Yes, I see . And it will work , If DB column type is Array(FixedString(64)) , am I right ?

This test is for Array(FixedString(10)):

ch-go/query_test.go

Lines 557 to 583 in 20b7501

t.Run("ArrayFixedStr", func(t *testing.T) {
t.Parallel()
conn := Conn(t)
require.NoError(t, conn.Do(ctx, Query{
Body: "CREATE TABLE test_table (v Array(FixedString(10))) ENGINE = Memory",
}), "create table")
v := (&proto.ColFixedStr{Size: 10}).Array()
v.Append([][]byte{
bytes.Repeat([]byte("a"), 10),
bytes.Repeat([]byte("b"), 10),
bytes.Repeat([]byte("c"), 10),
})
v.Append([][]byte{
bytes.Repeat([]byte("d"), 10),
bytes.Repeat([]byte("e"), 10),
bytes.Repeat([]byte("f"), 10),
})
require.NoError(t, conn.Do(ctx, Query{
Body: "INSERT INTO test_table VALUES",
Input: []proto.InputColumn{
{Name: "v", Data: v},
},
}), "insert")
})

Note that you still instantiate &proto.ColFixedStr{Size: 10} with Size: 10, ColFixedStr does not support size inference.

Also you can try ColRawOf[[10]byte] and it should work.