From 70fa856b66f470a7baf6bb4a81d0c3c7e0bba2df Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Wed, 6 Apr 2022 13:07:56 +0300 Subject: [PATCH] fix: correct ArrayOf implementation * Add compile-time assertions * Add test for Array(String) * Fix DecodeColumn, RowAppend --- proto/{col_arr_go118.go => col_arr_of.go} | 13 +++++++++---- ...{col_arr_go118_test.go => col_arr_of_test.go} | 2 -- query_test.go | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) rename proto/{col_arr_go118.go => col_arr_of.go} (84%) rename proto/{col_arr_go118_test.go => col_arr_of_test.go} (94%) diff --git a/proto/col_arr_go118.go b/proto/col_arr_of.go similarity index 84% rename from proto/col_arr_go118.go rename to proto/col_arr_of.go index 7e56cfaa..ba432130 100644 --- a/proto/col_arr_go118.go +++ b/proto/col_arr_of.go @@ -1,9 +1,14 @@ -//go:build go1.18 - package proto import "github.com/go-faster/errors" +// Compile-time assertions for ArrayOf. +var ( + _ ColInput = ArrayOf[string]((*ColStr)(nil)) + _ ColResult = ArrayOf[string]((*ColStr)(nil)) + _ Column = ArrayOf[string]((*ColStr)(nil)) +) + // ColumnOf is generic Column(T) constraint. type ColumnOf[T any] interface { Column @@ -42,7 +47,7 @@ func (c ColArrOf[T]) RowAppend(i int, target []T) []T { start = int(c.Offsets[i-1]) } for idx := start; idx < end; idx++ { - target = append(target, c.Data.Row(i)) + target = append(target, c.Data.Row(idx)) } return target @@ -52,7 +57,7 @@ func (c ColArrOf[T]) Row(i int) []T { return c.RowAppend(i, nil) } -func (c ColArrOf[T]) DecodeColumn(r *Reader, rows int) error { +func (c *ColArrOf[T]) DecodeColumn(r *Reader, rows int) error { if err := c.Offsets.DecodeColumn(r, rows); err != nil { return errors.Wrap(err, "read offsets") } diff --git a/proto/col_arr_go118_test.go b/proto/col_arr_of_test.go similarity index 94% rename from proto/col_arr_go118_test.go rename to proto/col_arr_of_test.go index 4b306a6e..7fbafb63 100644 --- a/proto/col_arr_go118_test.go +++ b/proto/col_arr_of_test.go @@ -1,5 +1,3 @@ -//go:build go1.18 - package proto import "testing" diff --git a/query_test.go b/query_test.go index 8dfe7ca8..bce5d0e6 100644 --- a/query_test.go +++ b/query_test.go @@ -649,6 +649,22 @@ func TestClient_Query(t *testing.T) { require.NoError(t, Conn(t).Do(ctx, selectArr)) require.Equal(t, proto.ColUInt8{1, 2, 3, 4}, data) }) + t.Run("SelectArrayOf", func(t *testing.T) { + t.Parallel() + arr := proto.ArrayOf[string](new(proto.ColStr)) + selectArr := Query{ + Body: "SELECT ['foo', 'bar', 'baz']::Array(String) as v", + Result: proto.Results{ + { + Name: "v", + Data: arr, + }, + }, + } + require.NoError(t, Conn(t).Do(ctx, selectArr)) + require.Equal(t, 1, arr.Rows()) + require.Equal(t, []string{"foo", "bar", "baz"}, arr.Row(0)) + }) t.Run("SelectRand", func(t *testing.T) { t.Parallel() const numbers = 15_249_611