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

Remove forced string conversions for Tuple #1465

Merged
merged 2 commits into from
Jan 10, 2025
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
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)
}
Loading