Skip to content

Commit

Permalink
Fix type match condition using reflection for autogen types (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik committed Oct 20, 2023
1 parent e382437 commit c329393
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/column/codegen/column.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (col *{{ .ChType }}) AppendRow(v any) error {
col.col.Append(val)
{{- end }}
default:
if rv := reflect.ValueOf(v); rv.Kind() == col.ScanType().Kind() && rv.CanConvert(col.ScanType()) {
if rv := reflect.ValueOf(v); rv.Kind() == col.ScanType().Kind() || rv.CanConvert(col.ScanType()) {
col.col.Append(rv.Convert(col.ScanType()).Interface().({{ .GoType }}))
} else {
return &ColumnConverterError{
Expand Down
20 changes: 10 additions & 10 deletions lib/column/column_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/base_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func TestSimpleInt(t *testing.T) {
require.NoError(t, conn.Exec(ctx, ddl))
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_int")
require.NoError(t, err)
require.Error(t, batch.Append(222))
require.NoError(t, batch.Append(222))
require.NoError(t, batch.Send())
}

func TestNullableInt(t *testing.T) {
Expand Down
39 changes: 39 additions & 0 deletions tests/issues/1119_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package issues

import (
"context"
"testing"

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

func Test1119(t *testing.T) {
var (
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
"max_execution_time": 60,
"allow_experimental_object_type": true,
}, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
)
ctx := context.Background()
require.NoError(t, err)
const ddl = "CREATE TABLE test_1119 (col JSON) Engine MergeTree() ORDER BY tuple()"
require.NoError(t, conn.Exec(ctx, ddl))
defer func() {
conn.Exec(ctx, "DROP TABLE IF EXISTS test_1119")
}()

batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO test_1119")
require.NoError(t, err)

v := map[string]any{
"str": "foo",
"int": 12,
}

require.NoError(t, batch.Append(v))
require.NoError(t, batch.Send())
}

0 comments on commit c329393

Please sign in to comment.