Skip to content

Commit 37afb6f

Browse files
authored
Fix panic on nil map append (#1383)
1 parent 94623b9 commit 37afb6f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

lib/column/map.go

+9
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ func (col *Map) Append(v any) (nulls []uint8, err error) {
169169
}
170170

171171
func (col *Map) AppendRow(v any) error {
172+
if v == nil {
173+
return &ColumnConverterError{
174+
Op: "Append",
175+
To: string(col.chType),
176+
From: fmt.Sprintf("%T", v),
177+
Hint: fmt.Sprintf("try using %s", col.scanType),
178+
}
179+
}
180+
172181
value := reflect.Indirect(reflect.ValueOf(v))
173182
if value.Type() == col.scanType {
174183
var (

tests/map_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,31 @@ func TestOrderedMap(t *testing.T) {
338338
require.Equal(t, 1000, i)
339339
}
340340

341+
func TestInsertMapNil(t *testing.T) {
342+
conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{
343+
Method: clickhouse.CompressionLZ4,
344+
})
345+
ctx := context.Background()
346+
require.NoError(t, err)
347+
if !CheckMinServerServerVersion(conn, 21, 9, 0) {
348+
t.Skip(fmt.Errorf("unsupported clickhouse version"))
349+
return
350+
}
351+
const ddl = `
352+
CREATE TABLE test_map_nil (
353+
Col1 Map(String, UInt64)
354+
) Engine MergeTree() ORDER BY tuple()
355+
`
356+
defer func() {
357+
conn.Exec(ctx, "DROP TABLE IF EXISTS test_map_nil")
358+
}()
359+
require.NoError(t, conn.Exec(ctx, ddl))
360+
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_map_nil")
361+
require.NoError(t, err)
362+
363+
assert.ErrorContains(t, batch.Append(nil), " converting <nil> to Map(String, UInt64) is unsupported")
364+
}
365+
341366
type testMapSerializer struct {
342367
val map[string]uint64
343368
}

tests/std/map_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,35 @@ func TestStdMap(t *testing.T) {
9898
})
9999
}
100100
}
101+
102+
func TestStdInsertNilMap(t *testing.T) {
103+
dsns := map[string]clickhouse.Protocol{"Native": clickhouse.Native, "Http": clickhouse.HTTP}
104+
useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
105+
require.NoError(t, err)
106+
for name, protocol := range dsns {
107+
t.Run(fmt.Sprintf("%s Protocol", name), func(t *testing.T) {
108+
conn, err := GetStdDSNConnection(protocol, useSSL, url.Values{})
109+
require.NoError(t, err)
110+
if !CheckMinServerVersion(conn, 21, 9, 0) {
111+
t.Skip(fmt.Errorf("unsupported clickhouse version"))
112+
return
113+
}
114+
const ddl = `
115+
CREATE TABLE test_map_nil (
116+
Col1 Map(String, UInt64)
117+
) Engine MergeTree() ORDER BY tuple()
118+
`
119+
defer func() {
120+
conn.Exec("DROP TABLE test_map_nil")
121+
}()
122+
_, err = conn.Exec(ddl)
123+
require.NoError(t, err)
124+
scope, err := conn.Begin()
125+
require.NoError(t, err)
126+
batch, err := scope.Prepare("INSERT INTO test_map_nil")
127+
require.NoError(t, err)
128+
_, err = batch.Exec(nil)
129+
assert.ErrorContains(t, err, " converting <nil> to Map(String, UInt64) is unsupported")
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)