Skip to content

Commit

Permalink
fix: correct ArrayOf implementation
Browse files Browse the repository at this point in the history
* Add compile-time assertions
* Add test for Array(String)
* Fix DecodeColumn, RowAppend
  • Loading branch information
ernado committed Apr 6, 2022
1 parent 30ebb81 commit 70fa856
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 9 additions & 4 deletions proto/col_arr_go118.go → proto/col_arr_of.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
Expand Down
2 changes: 0 additions & 2 deletions proto/col_arr_go118_test.go → proto/col_arr_of_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build go1.18

package proto

import "testing"
Expand Down
16 changes: 16 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70fa856

Please sign in to comment.