Skip to content

Commit

Permalink
Fix zero/nil values for net.IP (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik committed Oct 12, 2023
1 parent dcf2b3f commit e382437
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/column/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions lib/column/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
38 changes: 38 additions & 0 deletions tests/issues/1106_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit e382437

Please sign in to comment.