From e38243734a7b937e023ba629c6de5715d66f769d Mon Sep 17 00:00:00 2001 From: Kuba Kaflik Date: Thu, 12 Oct 2023 11:37:33 +0200 Subject: [PATCH] Fix zero/nil values for net.IP (#1118) --- lib/column/ipv4.go | 7 ++++++- lib/column/ipv6.go | 4 ++++ tests/issues/1106_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/issues/1106_test.go diff --git a/lib/column/ipv4.go b/lib/column/ipv4.go index d510a68438..f7b78945bd 100644 --- a/lib/column/ipv4.go +++ b/lib/column/ipv4.go @@ -242,7 +242,12 @@ func (col *IPv4) AppendRow(v any) (err error) { col.col.Append(0) } case net.IP: - col.col.Append(proto.ToIPv4(netIPToNetIPAddr(v))) + switch { + case len(v) == 0: + col.col.Append(0) + default: + col.col.Append(proto.ToIPv4(netIPToNetIPAddr(v))) + } case *net.IP: switch { case v != nil: diff --git a/lib/column/ipv6.go b/lib/column/ipv6.go index 34f051972a..0002e07308 100644 --- a/lib/column/ipv6.go +++ b/lib/column/ipv6.go @@ -333,6 +333,10 @@ func (col *IPv6) Encode(buffer *proto.Buffer) { } func IPv6ToBytes(ip net.IP) [16]byte { + if ip == nil { + return [16]byte{} + } + if len(ip) == 4 { ip = ip.To16() } diff --git a/tests/issues/1106_test.go b/tests/issues/1106_test.go new file mode 100644 index 0000000000..41f2f62dab --- /dev/null +++ b/tests/issues/1106_test.go @@ -0,0 +1,38 @@ +package issues + +import ( + "context" + "net" + "testing" + + "github.com/ClickHouse/clickhouse-go/v2" + clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" + "github.com/stretchr/testify/require" +) + +func Test1106(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_1106 (col1 IPv6, col2 IPv6, col3 IPv4, col4 IPv4) Engine MergeTree() ORDER BY tuple()" + require.NoError(t, conn.Exec(ctx, ddl)) + defer func() { + conn.Exec(ctx, "DROP TABLE IF EXISTS test_1106") + }() + + batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO test_1106") + require.NoError(t, err) + + var ip net.IP + var ipPtr *net.IP + + require.NoError(t, batch.Append(ip, ipPtr, ip, ipPtr)) + require.NoError(t, batch.Send()) +}