Skip to content

Commit

Permalink
Merge pull request #1465 from ClickHouse/issue_1446
Browse files Browse the repository at this point in the history
Remove forced string conversions for Tuple
  • Loading branch information
SpencerTorres authored Jan 10, 2025
2 parents 017f379 + bbf06d8 commit d792ffc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
7 changes: 0 additions & 7 deletions lib/column/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ func setJSONFieldValue(field reflect.Value, value reflect.Value) error {
}
}

// check if our target is a string
if field.Kind() == reflect.String {
if v := reflect.ValueOf(fmt.Sprint(value.Interface())); v.Type().AssignableTo(field.Type()) {
field.Set(v)
return nil
}
}
if value.CanConvert(field.Type()) {
field.Set(value.Convert(field.Type()))
return nil
Expand Down
62 changes: 62 additions & 0 deletions tests/issues/1446_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package issues

import (
"context"
"testing"

"github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type sampleFailTuple struct {
TupleOne string `ch:"tuple_one"`
TupleTwo string `ch:"tuple_two"`
}

type sampleFailRow struct {
MyId uint64 `ch:"my_id"`
MyTuple sampleFailTuple `ch:"my_tuple"`
}

type sampleOkTuple struct {
TupleOne *string `ch:"tuple_one"`
TupleTwo *string `ch:"tuple_two"`
}

type sampleOkRow struct {
MyId uint64 `ch:"my_id"`
MyTuple sampleOkTuple `ch:"my_tuple"`
}

func TestIssue1446(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 IF NOT EXISTS issue_1446(
my_id UInt64,
my_tuple Tuple(tuple_one Nullable(String), tuple_two Nullable(String))
) ENGINE = MergeTree PRIMARY KEY (my_id) ORDER BY (my_id)
`
err = conn.Exec(ctx, ddl)
require.NoError(t, err)
defer conn.Exec(ctx, "DROP TABLE issue_1446")

err = conn.Exec(ctx, "INSERT INTO issue_1446(my_id, my_tuple) VALUES (1, tuple('one', 'two'))")
require.NoError(t, err)

failRow := sampleFailRow{}
err = conn.QueryRow(ctx, "SELECT * FROM issue_1446 LIMIT 1").ScanStruct(&failRow)
assert.EqualError(t, err, "clickhouse [ScanRow]: (my_tuple) converting *string to string is unsupported")

okRow := sampleOkRow{}
err = conn.QueryRow(ctx, "SELECT * FROM issue_1446 LIMIT 1").ScanStruct(&okRow)
require.NoError(t, err)

assert.Equal(t, "one", *okRow.MyTuple.TupleOne)
assert.Equal(t, "two", *okRow.MyTuple.TupleTwo)
}

0 comments on commit d792ffc

Please sign in to comment.