Skip to content

Commit

Permalink
Fix panic on nil map append (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik authored Aug 23, 2024
1 parent 94623b9 commit 37afb6f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/column/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func (col *Map) Append(v any) (nulls []uint8, err error) {
}

func (col *Map) AppendRow(v any) error {
if v == nil {
return &ColumnConverterError{
Op: "Append",
To: string(col.chType),
From: fmt.Sprintf("%T", v),
Hint: fmt.Sprintf("try using %s", col.scanType),
}
}

value := reflect.Indirect(reflect.ValueOf(v))
if value.Type() == col.scanType {
var (
Expand Down
25 changes: 25 additions & 0 deletions tests/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,31 @@ func TestOrderedMap(t *testing.T) {
require.Equal(t, 1000, i)
}

func TestInsertMapNil(t *testing.T) {
conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
ctx := context.Background()
require.NoError(t, err)
if !CheckMinServerServerVersion(conn, 21, 9, 0) {
t.Skip(fmt.Errorf("unsupported clickhouse version"))
return
}
const ddl = `
CREATE TABLE test_map_nil (
Col1 Map(String, UInt64)
) Engine MergeTree() ORDER BY tuple()
`
defer func() {
conn.Exec(ctx, "DROP TABLE IF EXISTS test_map_nil")
}()
require.NoError(t, conn.Exec(ctx, ddl))
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_map_nil")
require.NoError(t, err)

assert.ErrorContains(t, batch.Append(nil), " converting <nil> to Map(String, UInt64) is unsupported")
}

type testMapSerializer struct {
val map[string]uint64
}
Expand Down
32 changes: 32 additions & 0 deletions tests/std/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,35 @@ func TestStdMap(t *testing.T) {
})
}
}

func TestStdInsertNilMap(t *testing.T) {
dsns := map[string]clickhouse.Protocol{"Native": clickhouse.Native, "Http": clickhouse.HTTP}
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
for name, protocol := range dsns {
t.Run(fmt.Sprintf("%s Protocol", name), func(t *testing.T) {
conn, err := GetStdDSNConnection(protocol, useSSL, url.Values{})
require.NoError(t, err)
if !CheckMinServerVersion(conn, 21, 9, 0) {
t.Skip(fmt.Errorf("unsupported clickhouse version"))
return
}
const ddl = `
CREATE TABLE test_map_nil (
Col1 Map(String, UInt64)
) Engine MergeTree() ORDER BY tuple()
`
defer func() {
conn.Exec("DROP TABLE test_map_nil")
}()
_, err = conn.Exec(ddl)
require.NoError(t, err)
scope, err := conn.Begin()
require.NoError(t, err)
batch, err := scope.Prepare("INSERT INTO test_map_nil")
require.NoError(t, err)
_, err = batch.Exec(nil)
assert.ErrorContains(t, err, " converting <nil> to Map(String, UInt64) is unsupported")
})
}
}

0 comments on commit 37afb6f

Please sign in to comment.