Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization for slices of OrderedMap/IterableOrderedMap (#1365) #1418

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions lib/column/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,17 @@ func appendNullableRowPlain[T any](col *Array, arr []*T) error {
func (col *Array) append(elem reflect.Value, level int) error {
if level < col.depth {
switch elem.Kind() {
// allows to traverse pointers to slices and slices cast to `any`
case reflect.Interface, reflect.Ptr:
if !elem.IsNil() {
return col.append(elem.Elem(), level)
}
// reflect.Value.Len() & reflect.Value.Index() is called in `append` method which is only valid for
// Slice, Array and String that make sense here.
case reflect.Slice, reflect.Array, reflect.String:
col.appendOffset(level, uint64(elem.Len()))
for i := 0; i < elem.Len(); i++ {
el := elem.Index(i)

if el.Kind() == reflect.Interface && !el.IsNil() {
el = el.Elem()
}

if el.Kind() == reflect.Ptr && !el.IsNil() {
el = el.Elem()
}

if err := col.append(el, level+1); err != nil {
if err := col.append(elem.Index(i), level+1); err != nil {
return err
}
}
Expand Down
61 changes: 61 additions & 0 deletions tests/issues/1365_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package issues

import (
"context"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
"testing"
)

type T1365OrderedMap int

func (t *T1365OrderedMap) Put(k any, v any) {
if k == "K" && v == "V" {
*t = 0xDEDEDEAD
}
}
func (t *T1365OrderedMap) Iterator() column.MapIterator { return t }
func (t *T1365OrderedMap) Next() bool { *t++; return *t == 1 }
func (t *T1365OrderedMap) Key() any { return "K" }
func (t *T1365OrderedMap) Value() any { return "V" }

func TestIssue1365(t *testing.T) {
ctx := context.Background()

conn, err := tests.GetConnection("issues", nil, nil, nil)
require.NoError(t, err)
defer conn.Close()

const ddl = `
CREATE TABLE test_1365 (
Col1 Array(Map(String,String))
) Engine MergeTree() ORDER BY tuple()
`
err = conn.Exec(ctx, ddl)
require.NoError(t, err)
defer conn.Exec(ctx, "DROP TABLE test_1365")

batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_1365")
require.NoError(t, err)

var writeMaps []column.IterableOrderedMap
writeMaps = append(writeMaps, new(T1365OrderedMap))
writeMaps = append(writeMaps, new(T1365OrderedMap))

err = batch.Append(writeMaps)
require.NoError(t, err)

err = batch.Send()
require.NoError(t, err)

rows, err := conn.Query(ctx, "SELECT * FROM test_1365")
require.NoError(t, err)

require.True(t, rows.Next())

//var readMaps []*T1365OrderedMap
//
//err = rows.Scan(&readMaps)
//require.NoError(t, err)
Comment on lines +57 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel this is not required, let's remove it.

Suggested change
//var readMaps []*T1365OrderedMap
//
//err = rows.Scan(&readMaps)
//require.NoError(t, err)

}
Loading