From 3d72aa92e9b8572e7ddab7a708287f464eb61fa7 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 14:58:03 +0200 Subject: [PATCH 01/17] Support sql.Scanner for Bool, IPv4 and IPv6 types Co-authored-by: Marco Gazerro --- lib/column/bool.go | 3 +++ lib/column/ipv4.go | 7 ++++++- lib/column/ipv6.go | 7 ++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/column/bool.go b/lib/column/bool.go index 3699a3cfdf..c4b568fc05 100644 --- a/lib/column/bool.go +++ b/lib/column/bool.go @@ -68,6 +68,9 @@ func (col *Bool) ScanRow(dest any, row int) error { case sql.Scanner: return d.Scan(col.row(row)) default: + if scan, ok := dest.(sql.Scanner); ok { + return scan.Scan(col.row(row)) + } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), diff --git a/lib/column/ipv4.go b/lib/column/ipv4.go index 3d4c252833..102a55bbf9 100644 --- a/lib/column/ipv4.go +++ b/lib/column/ipv4.go @@ -18,13 +18,15 @@ package column import ( + "database/sql" "database/sql/driver" "encoding/binary" "fmt" - "github.com/ClickHouse/ch-go/proto" "net" "net/netip" "reflect" + + "github.com/ClickHouse/ch-go/proto" ) type IPv4 struct { @@ -99,6 +101,9 @@ func (col *IPv4) ScanRow(dest any, row int) error { *d = new(uint32) **d = binary.BigEndian.Uint32(ipV4[:]) default: + if scan, ok := dest.(sql.Scanner); ok { + return scan.Scan(col.row(row)) + } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), diff --git a/lib/column/ipv6.go b/lib/column/ipv6.go index a67d17abc4..3fe0322401 100644 --- a/lib/column/ipv6.go +++ b/lib/column/ipv6.go @@ -18,12 +18,14 @@ package column import ( + "database/sql" "database/sql/driver" "fmt" - "github.com/ClickHouse/ch-go/proto" "net" "net/netip" "reflect" + + "github.com/ClickHouse/ch-go/proto" ) type IPv6 struct { @@ -92,6 +94,9 @@ func (col *IPv6) ScanRow(dest any, row int) error { *d = new([16]byte) **d = col.col.Row(row) default: + if scan, ok := dest.(sql.Scanner); ok { + return scan.Scan(col.row(row)) + } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), From 1eb9678df87d71ed48f1a1b5b24d9250e27ebea4 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 14:58:54 +0200 Subject: [PATCH 02/17] Support sql.Scanner for Array and Map types Co-authored-by: Marco Gazerro --- lib/column/array.go | 9 +++++++++ lib/column/map.go | 3 +++ 2 files changed, 12 insertions(+) diff --git a/lib/column/array.go b/lib/column/array.go index 2a0c17d40d..54dab78209 100644 --- a/lib/column/array.go +++ b/lib/column/array.go @@ -25,6 +25,8 @@ import ( "time" ) +var scanTypeAny = reflect.TypeOf((*interface{})(nil)).Elem() + type offset struct { values UInt64 scanType reflect.Type @@ -268,6 +270,13 @@ func (col *Array) WriteStatePrefix(buffer *proto.Buffer) error { } func (col *Array) ScanRow(dest any, row int) error { + if scanner, ok := dest.(interface{ Scan(any) error }); ok { + value, err := col.scan(scanTypeAny, row) + if err != nil { + return err + } + return scanner.Scan(value.Interface()) + } elem := reflect.Indirect(reflect.ValueOf(dest)) value, err := col.scan(elem.Type(), row) if err != nil { diff --git a/lib/column/map.go b/lib/column/map.go index a33514634d..1a3d9e09ea 100644 --- a/lib/column/map.go +++ b/lib/column/map.go @@ -114,6 +114,9 @@ func (col *Map) Row(i int, ptr bool) any { } func (col *Map) ScanRow(dest any, i int) error { + if scanner, ok := dest.(interface{ Scan(any) error }); ok { + return scanner.Scan(col.row(i).Interface()) + } value := reflect.Indirect(reflect.ValueOf(dest)) if value.Type() == col.scanType { value.Set(col.row(i)) From 00f2dc2812fa1d54aba3d716bc6b824174b6aefd Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 14:59:45 +0200 Subject: [PATCH 03/17] Use sql.Scanner to refer to the sql.Scanner interface Co-authored-by: Marco Gazerro --- lib/column/array.go | 3 ++- lib/column/map.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/column/array.go b/lib/column/array.go index 54dab78209..f510bfb10d 100644 --- a/lib/column/array.go +++ b/lib/column/array.go @@ -18,6 +18,7 @@ package column import ( + "database/sql" "fmt" "github.com/ClickHouse/ch-go/proto" "reflect" @@ -270,7 +271,7 @@ func (col *Array) WriteStatePrefix(buffer *proto.Buffer) error { } func (col *Array) ScanRow(dest any, row int) error { - if scanner, ok := dest.(interface{ Scan(any) error }); ok { + if scanner, ok := dest.(sql.Scanner); ok { value, err := col.scan(scanTypeAny, row) if err != nil { return err diff --git a/lib/column/map.go b/lib/column/map.go index 1a3d9e09ea..dc87a4663d 100644 --- a/lib/column/map.go +++ b/lib/column/map.go @@ -19,6 +19,7 @@ package column import ( "database/sql/driver" + "database/sql" "fmt" "reflect" "strings" @@ -114,7 +115,7 @@ func (col *Map) Row(i int, ptr bool) any { } func (col *Map) ScanRow(dest any, i int) error { - if scanner, ok := dest.(interface{ Scan(any) error }); ok { + if scanner, ok := dest.(sql.Scanner); ok { return scanner.Scan(col.row(i).Interface()) } value := reflect.Indirect(reflect.ValueOf(dest)) From 0904d4f076e41f6d9392b732c6028b0f5fe9fa95 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 15:00:44 +0200 Subject: [PATCH 04/17] Run `go fmt ./...` --- lib/column/map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/column/map.go b/lib/column/map.go index dc87a4663d..70da38e62e 100644 --- a/lib/column/map.go +++ b/lib/column/map.go @@ -18,8 +18,8 @@ package column import ( - "database/sql/driver" "database/sql" + "database/sql/driver" "fmt" "reflect" "strings" From 21ed6f4a44a726d61b358fa633232a9f1cb50e78 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:00:06 +0200 Subject: [PATCH 05/17] Add test `TestSQLScannerArray` --- tests/array_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/array_test.go b/tests/array_test.go index 3f8617a18f..7e6cd2be53 100644 --- a/tests/array_test.go +++ b/tests/array_test.go @@ -21,10 +21,11 @@ import ( "context" "database/sql/driver" "fmt" - "github.com/stretchr/testify/require" "testing" "time" + "github.com/stretchr/testify/require" + "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) @@ -153,6 +154,56 @@ func TestInterfaceArray(t *testing.T) { require.NoError(t, rows.Err()) } +func TestSQLScannerArray(t *testing.T) { + conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ctx := context.Background() + require.NoError(t, err) + const ddl = ` + CREATE TABLE test_array ( + Col1 Array(String) + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE test_array") + }() + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_array") + require.NoError(t, err) + var ( + col1Data = []string{"A", "b", "c"} + ) + for i := 0; i < 10; i++ { + require.NoError(t, batch.Append(col1Data)) + } + require.Equal(t, 10, batch.Rows()) + require.Nil(t, batch.Send()) + rows, err := conn.Query(ctx, "SELECT * FROM test_array") + require.NoError(t, err) + for rows.Next() { + var ( + col1 = sqlScannerArray{} + ) + require.NoError(t, rows.Scan(&col1)) + ok := assert.ObjectsAreEqual(col1Data, col1.value) + if !ok { + t.Fatalf("expected %#v, got %#v", col1Data, col1.value) + } + } + require.NoError(t, rows.Close()) + require.NoError(t, rows.Err()) +} + +type sqlScannerArray struct { + value any +} + +func (s *sqlScannerArray) Scan(src any) error { + s.value = src + return nil +} + func TestArray(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, From e8d3a0cf3967cc01f68a3830df29699ce31b0aa1 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:14:04 +0200 Subject: [PATCH 06/17] Add test `TestSQLScannerIPv4` --- tests/ipv4_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/tests/ipv4_test.go b/tests/ipv4_test.go index ea67633b81..a4607bba2e 100644 --- a/tests/ipv4_test.go +++ b/tests/ipv4_test.go @@ -22,12 +22,13 @@ import ( "database/sql/driver" "encoding/binary" "fmt" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" - "github.com/stretchr/testify/require" "net" "net/netip" "testing" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" + "github.com/stretchr/testify/require" + "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) @@ -64,6 +65,47 @@ func TestSimpleIPv4(t *testing.T) { assert.Equal(t, col1Data.To4(), col1) } +func TestSQLScannerIPv4(t *testing.T) { + conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ctx := context.Background() + require.NoError(t, err) + const ddl = ` + CREATE TABLE test_ipv4 ( + Col1 IPv4 + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE test_ipv4") + }() + + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_ipv4") + require.NoError(t, err) + + var ( + col1Data = net.ParseIP("127.0.0.1") + ) + require.NoError(t, batch.Append(col1Data)) + require.Equal(t, 1, batch.Rows()) + require.NoError(t, batch.Send()) + var ( + col1 sqlScannerIPv4 + ) + require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_ipv4").Scan(&col1)) + assert.Equal(t, col1Data.To4(), col1.value) +} + +type sqlScannerIPv4 struct { + value any +} + +func (s *sqlScannerIPv4) Scan(src any) error { + s.value = src + return nil +} + func TestIPv4(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, From 9dd2e38f223ed9949457f3b47ed4dd9698c6ec21 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:14:19 +0200 Subject: [PATCH 07/17] Add comment --- lib/column/bool.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/column/bool.go b/lib/column/bool.go index c4b568fc05..83cafd441c 100644 --- a/lib/column/bool.go +++ b/lib/column/bool.go @@ -21,8 +21,9 @@ import ( "database/sql" "database/sql/driver" "fmt" - "github.com/ClickHouse/ch-go/proto" "reflect" + + "github.com/ClickHouse/ch-go/proto" ) type Bool struct { @@ -68,6 +69,8 @@ func (col *Bool) ScanRow(dest any, row int) error { case sql.Scanner: return d.Scan(col.row(row)) default: + // REVIEW: questo è già stato gestito? Pare esserci già anche un test + // sopra, 'TestCustomBool'. Indaga un attimo. if scan, ok := dest.(sql.Scanner); ok { return scan.Scan(col.row(row)) } From f043fb54a26c3827125811f96247ce7733dfea55 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:20:45 +0200 Subject: [PATCH 08/17] Add a comment --- lib/column/ipv6.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/column/ipv6.go b/lib/column/ipv6.go index 3fe0322401..7bcf8916ea 100644 --- a/lib/column/ipv6.go +++ b/lib/column/ipv6.go @@ -94,6 +94,8 @@ func (col *IPv6) ScanRow(dest any, row int) error { *d = new([16]byte) **d = col.col.Row(row) default: + // REVIEW: valutare di inserire queste type assertion direttamente tra i + // case dello switch sul tipo. if scan, ok := dest.(sql.Scanner); ok { return scan.Scan(col.row(row)) } From da1333f748f989a311637db89bf3b1de1cc92bbc Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:24:56 +0200 Subject: [PATCH 09/17] Add test `TestSQLScannerIPv6` --- tests/ipv6_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/tests/ipv6_test.go b/tests/ipv6_test.go index f8b0f8df25..cd450ca47c 100644 --- a/tests/ipv6_test.go +++ b/tests/ipv6_test.go @@ -21,13 +21,14 @@ import ( "context" "database/sql/driver" "fmt" - "github.com/ClickHouse/ch-go/proto" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" - "github.com/stretchr/testify/require" "net" "net/netip" "testing" + "github.com/ClickHouse/ch-go/proto" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" + "github.com/stretchr/testify/require" + "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) @@ -520,3 +521,46 @@ func TestIPv6Valuer(t *testing.T) { } require.Equal(t, 1000, i) } + +// REVIEW: move the tests in a consistent position. + +func TestSQLScannerIPv6(t *testing.T) { + conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ctx := context.Background() + require.NoError(t, err) + const ddl = ` + CREATE TABLE test_ipv6 ( + Col1 IPv6 + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE test_ipv6") + }() + + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_ipv6") + require.NoError(t, err) + + var ( + col1Data = net.ParseIP("2001:44c8:129:2632:33:0:252:2") + ) + require.NoError(t, batch.Append(col1Data)) + require.Equal(t, 1, batch.Rows()) + require.NoError(t, batch.Send()) + var ( + col1 sqlScannerIPv6 + ) + require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_ipv6").Scan(&col1)) + assert.Equal(t, col1Data, col1.value) +} + +type sqlScannerIPv6 struct { + value any +} + +func (s *sqlScannerIPv6) Scan(src any) error { + s.value = src + return nil +} From f96741af1bf8b526db62388de5437b35ed39b4a8 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 16:48:26 +0200 Subject: [PATCH 10/17] Add test `TestSQLScannerMap` --- tests/map_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/map_test.go b/tests/map_test.go index f3be3105dc..040c05c97f 100644 --- a/tests/map_test.go +++ b/tests/map_test.go @@ -21,10 +21,11 @@ import ( "context" "database/sql/driver" "fmt" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" "reflect" "testing" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" + "github.com/stretchr/testify/require" "github.com/ClickHouse/clickhouse-go/v2" @@ -459,6 +460,52 @@ func (i *mapIter) Value() any { return i.om.valuesIter[i.iterIndex] } +func TestSQLScannerMap(t *testing.T) { + conn, err := GetNativeConnection(clickhouse.Settings{}, 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 ( + Col1 Map(String, UInt64) + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE IF EXISTS test_map") + }() + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_map") + require.NoError(t, err) + var ( + col1Data = map[string]uint64{ + "key_col_1_1": 1, + "key_col_1_2": 2, + } + ) + require.NoError(t, batch.Append(col1Data)) + require.Equal(t, 1, batch.Rows()) + require.NoError(t, batch.Send()) + var ( + col1 sqlScannerMap + ) + require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_map").Scan(&col1)) + assert.Equal(t, col1Data, col1.value) +} + +type sqlScannerMap struct { + value any +} + +func (s *sqlScannerMap) Scan(src any) error { + s.value = src + return nil +} + func BenchmarkOrderedMapUseChanGo(b *testing.B) { m := NewOrderedMap() for i := 0; i < 10; i++ { From 2823415bf9c69565d347dc2ac3fb30bff30ddc08 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 17:19:18 +0200 Subject: [PATCH 11/17] Remove code that handle bool values, as already implemented in upstream --- lib/column/bool.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/column/bool.go b/lib/column/bool.go index 83cafd441c..46d437945f 100644 --- a/lib/column/bool.go +++ b/lib/column/bool.go @@ -69,11 +69,6 @@ func (col *Bool) ScanRow(dest any, row int) error { case sql.Scanner: return d.Scan(col.row(row)) default: - // REVIEW: questo è già stato gestito? Pare esserci già anche un test - // sopra, 'TestCustomBool'. Indaga un attimo. - if scan, ok := dest.(sql.Scanner); ok { - return scan.Scan(col.row(row)) - } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), From 16837424401349a43d8f37e15bdb4114ed2f7175 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Thu, 5 Jun 2025 17:19:54 +0200 Subject: [PATCH 12/17] Use consistent style in type assertions --- lib/column/ipv4.go | 5 ++--- lib/column/ipv6.go | 7 ++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/column/ipv4.go b/lib/column/ipv4.go index 102a55bbf9..612f075995 100644 --- a/lib/column/ipv4.go +++ b/lib/column/ipv4.go @@ -100,10 +100,9 @@ func (col *IPv4) ScanRow(dest any, row int) error { } *d = new(uint32) **d = binary.BigEndian.Uint32(ipV4[:]) + case sql.Scanner: + return d.Scan(col.row(row)) default: - if scan, ok := dest.(sql.Scanner); ok { - return scan.Scan(col.row(row)) - } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), diff --git a/lib/column/ipv6.go b/lib/column/ipv6.go index 7bcf8916ea..6948739418 100644 --- a/lib/column/ipv6.go +++ b/lib/column/ipv6.go @@ -93,12 +93,9 @@ func (col *IPv6) ScanRow(dest any, row int) error { case **[16]byte: *d = new([16]byte) **d = col.col.Row(row) + case sql.Scanner: + return d.Scan(col.row(row)) default: - // REVIEW: valutare di inserire queste type assertion direttamente tra i - // case dello switch sul tipo. - if scan, ok := dest.(sql.Scanner); ok { - return scan.Scan(col.row(row)) - } return &ColumnConverterError{ Op: "ScanRow", To: fmt.Sprintf("%T", dest), From c7580c55ea1404b48a6455ff00db677d8ece37c8 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Fri, 6 Jun 2025 08:46:17 +0200 Subject: [PATCH 13/17] Restore import position and grouping --- lib/column/bool.go | 3 +-- lib/column/ipv4.go | 3 +-- lib/column/ipv6.go | 3 +-- tests/array_test.go | 3 +-- tests/ipv4_test.go | 5 ++--- tests/ipv6_test.go | 7 +++---- tests/map_test.go | 3 +-- 7 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/column/bool.go b/lib/column/bool.go index 46d437945f..3699a3cfdf 100644 --- a/lib/column/bool.go +++ b/lib/column/bool.go @@ -21,9 +21,8 @@ import ( "database/sql" "database/sql/driver" "fmt" - "reflect" - "github.com/ClickHouse/ch-go/proto" + "reflect" ) type Bool struct { diff --git a/lib/column/ipv4.go b/lib/column/ipv4.go index 612f075995..e0a869cc9a 100644 --- a/lib/column/ipv4.go +++ b/lib/column/ipv4.go @@ -22,11 +22,10 @@ import ( "database/sql/driver" "encoding/binary" "fmt" + "github.com/ClickHouse/ch-go/proto" "net" "net/netip" "reflect" - - "github.com/ClickHouse/ch-go/proto" ) type IPv4 struct { diff --git a/lib/column/ipv6.go b/lib/column/ipv6.go index 6948739418..0544e7931d 100644 --- a/lib/column/ipv6.go +++ b/lib/column/ipv6.go @@ -21,11 +21,10 @@ import ( "database/sql" "database/sql/driver" "fmt" + "github.com/ClickHouse/ch-go/proto" "net" "net/netip" "reflect" - - "github.com/ClickHouse/ch-go/proto" ) type IPv6 struct { diff --git a/tests/array_test.go b/tests/array_test.go index 7e6cd2be53..9b7916fe2c 100644 --- a/tests/array_test.go +++ b/tests/array_test.go @@ -21,11 +21,10 @@ import ( "context" "database/sql/driver" "fmt" + "github.com/stretchr/testify/require" "testing" "time" - "github.com/stretchr/testify/require" - "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) diff --git a/tests/ipv4_test.go b/tests/ipv4_test.go index a4607bba2e..9275a0696b 100644 --- a/tests/ipv4_test.go +++ b/tests/ipv4_test.go @@ -22,13 +22,12 @@ import ( "database/sql/driver" "encoding/binary" "fmt" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" + "github.com/stretchr/testify/require" "net" "net/netip" "testing" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" - "github.com/stretchr/testify/require" - "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) diff --git a/tests/ipv6_test.go b/tests/ipv6_test.go index cd450ca47c..7ce721db34 100644 --- a/tests/ipv6_test.go +++ b/tests/ipv6_test.go @@ -21,13 +21,12 @@ import ( "context" "database/sql/driver" "fmt" - "net" - "net/netip" - "testing" - "github.com/ClickHouse/ch-go/proto" "github.com/ClickHouse/clickhouse-go/v2/lib/column" "github.com/stretchr/testify/require" + "net" + "net/netip" + "testing" "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" diff --git a/tests/map_test.go b/tests/map_test.go index 040c05c97f..cecf53e11c 100644 --- a/tests/map_test.go +++ b/tests/map_test.go @@ -21,11 +21,10 @@ import ( "context" "database/sql/driver" "fmt" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" "reflect" "testing" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" - "github.com/stretchr/testify/require" "github.com/ClickHouse/clickhouse-go/v2" From 31f59cbe7d0222d703a530f670c90dce5611de6b Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Fri, 6 Jun 2025 08:47:17 +0200 Subject: [PATCH 14/17] Move tests at the end of respective files --- tests/array_test.go | 100 ++++++++++++++++++++++---------------------- tests/ipv4_test.go | 81 +++++++++++++++++------------------ 2 files changed, 91 insertions(+), 90 deletions(-) diff --git a/tests/array_test.go b/tests/array_test.go index 9b7916fe2c..46a0ad2acd 100644 --- a/tests/array_test.go +++ b/tests/array_test.go @@ -153,56 +153,6 @@ func TestInterfaceArray(t *testing.T) { require.NoError(t, rows.Err()) } -func TestSQLScannerArray(t *testing.T) { - conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ - Method: clickhouse.CompressionLZ4, - }) - ctx := context.Background() - require.NoError(t, err) - const ddl = ` - CREATE TABLE test_array ( - Col1 Array(String) - ) Engine MergeTree() ORDER BY tuple() - ` - defer func() { - conn.Exec(ctx, "DROP TABLE test_array") - }() - require.NoError(t, conn.Exec(ctx, ddl)) - batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_array") - require.NoError(t, err) - var ( - col1Data = []string{"A", "b", "c"} - ) - for i := 0; i < 10; i++ { - require.NoError(t, batch.Append(col1Data)) - } - require.Equal(t, 10, batch.Rows()) - require.Nil(t, batch.Send()) - rows, err := conn.Query(ctx, "SELECT * FROM test_array") - require.NoError(t, err) - for rows.Next() { - var ( - col1 = sqlScannerArray{} - ) - require.NoError(t, rows.Scan(&col1)) - ok := assert.ObjectsAreEqual(col1Data, col1.value) - if !ok { - t.Fatalf("expected %#v, got %#v", col1Data, col1.value) - } - } - require.NoError(t, rows.Close()) - require.NoError(t, rows.Err()) -} - -type sqlScannerArray struct { - value any -} - -func (s *sqlScannerArray) Scan(src any) error { - s.value = src - return nil -} - func TestArray(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, @@ -423,3 +373,53 @@ func TestSimpleArrayValuer(t *testing.T) { require.NoError(t, rows.Close()) require.NoError(t, rows.Err()) } + +func TestSQLScannerArray(t *testing.T) { + conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ctx := context.Background() + require.NoError(t, err) + const ddl = ` + CREATE TABLE test_array ( + Col1 Array(String) + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE test_array") + }() + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_array") + require.NoError(t, err) + var ( + col1Data = []string{"A", "b", "c"} + ) + for i := 0; i < 10; i++ { + require.NoError(t, batch.Append(col1Data)) + } + require.Equal(t, 10, batch.Rows()) + require.Nil(t, batch.Send()) + rows, err := conn.Query(ctx, "SELECT * FROM test_array") + require.NoError(t, err) + for rows.Next() { + var ( + col1 = sqlScannerArray{} + ) + require.NoError(t, rows.Scan(&col1)) + ok := assert.ObjectsAreEqual(col1Data, col1.value) + if !ok { + t.Fatalf("expected %#v, got %#v", col1Data, col1.value) + } + } + require.NoError(t, rows.Close()) + require.NoError(t, rows.Err()) +} + +type sqlScannerArray struct { + value any +} + +func (s *sqlScannerArray) Scan(src any) error { + s.value = src + return nil +} diff --git a/tests/ipv4_test.go b/tests/ipv4_test.go index 9275a0696b..985cac7937 100644 --- a/tests/ipv4_test.go +++ b/tests/ipv4_test.go @@ -64,46 +64,6 @@ func TestSimpleIPv4(t *testing.T) { assert.Equal(t, col1Data.To4(), col1) } -func TestSQLScannerIPv4(t *testing.T) { - conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ - Method: clickhouse.CompressionLZ4, - }) - ctx := context.Background() - require.NoError(t, err) - const ddl = ` - CREATE TABLE test_ipv4 ( - Col1 IPv4 - ) Engine MergeTree() ORDER BY tuple() - ` - defer func() { - conn.Exec(ctx, "DROP TABLE test_ipv4") - }() - - require.NoError(t, conn.Exec(ctx, ddl)) - batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_ipv4") - require.NoError(t, err) - - var ( - col1Data = net.ParseIP("127.0.0.1") - ) - require.NoError(t, batch.Append(col1Data)) - require.Equal(t, 1, batch.Rows()) - require.NoError(t, batch.Send()) - var ( - col1 sqlScannerIPv4 - ) - require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_ipv4").Scan(&col1)) - assert.Equal(t, col1Data.To4(), col1.value) -} - -type sqlScannerIPv4 struct { - value any -} - -func (s *sqlScannerIPv4) Scan(src any) error { - s.value = src - return nil -} func TestIPv4(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ @@ -597,3 +557,44 @@ func TestIPv4Valuer(t *testing.T) { } require.Equal(t, 1000, i) } + +func TestSQLScannerIPv4(t *testing.T) { + conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ + Method: clickhouse.CompressionLZ4, + }) + ctx := context.Background() + require.NoError(t, err) + const ddl = ` + CREATE TABLE test_ipv4 ( + Col1 IPv4 + ) Engine MergeTree() ORDER BY tuple() + ` + defer func() { + conn.Exec(ctx, "DROP TABLE test_ipv4") + }() + + require.NoError(t, conn.Exec(ctx, ddl)) + batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_ipv4") + require.NoError(t, err) + + var ( + col1Data = net.ParseIP("127.0.0.1") + ) + require.NoError(t, batch.Append(col1Data)) + require.Equal(t, 1, batch.Rows()) + require.NoError(t, batch.Send()) + var ( + col1 sqlScannerIPv4 + ) + require.NoError(t, conn.QueryRow(ctx, "SELECT * FROM test_ipv4").Scan(&col1)) + assert.Equal(t, col1Data.To4(), col1.value) +} + +type sqlScannerIPv4 struct { + value any +} + +func (s *sqlScannerIPv4) Scan(src any) error { + s.value = src + return nil +} \ No newline at end of file From c6ce79ebc73813ce1f48f8df56c65f8c5d0df99a Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Fri, 6 Jun 2025 08:48:06 +0200 Subject: [PATCH 15/17] Remove review comment --- tests/ipv6_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/ipv6_test.go b/tests/ipv6_test.go index 7ce721db34..6aee2ae130 100644 --- a/tests/ipv6_test.go +++ b/tests/ipv6_test.go @@ -21,13 +21,14 @@ import ( "context" "database/sql/driver" "fmt" - "github.com/ClickHouse/ch-go/proto" - "github.com/ClickHouse/clickhouse-go/v2/lib/column" - "github.com/stretchr/testify/require" "net" "net/netip" "testing" + "github.com/ClickHouse/ch-go/proto" + "github.com/ClickHouse/clickhouse-go/v2/lib/column" + "github.com/stretchr/testify/require" + "github.com/ClickHouse/clickhouse-go/v2" "github.com/stretchr/testify/assert" ) @@ -521,8 +522,6 @@ func TestIPv6Valuer(t *testing.T) { require.Equal(t, 1000, i) } -// REVIEW: move the tests in a consistent position. - func TestSQLScannerIPv6(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, From c88fb1750e3a84e3969dfa9101696fb8af31eefa Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Fri, 6 Jun 2025 08:48:50 +0200 Subject: [PATCH 16/17] Run 'go fmt ./...' --- tests/ipv4_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/ipv4_test.go b/tests/ipv4_test.go index 985cac7937..ceda75cf43 100644 --- a/tests/ipv4_test.go +++ b/tests/ipv4_test.go @@ -64,7 +64,6 @@ func TestSimpleIPv4(t *testing.T) { assert.Equal(t, col1Data.To4(), col1) } - func TestIPv4(t *testing.T) { conn, err := GetNativeConnection(nil, nil, &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, @@ -597,4 +596,4 @@ type sqlScannerIPv4 struct { func (s *sqlScannerIPv4) Scan(src any) error { s.value = src return nil -} \ No newline at end of file +} From 574280cc5f995c267b66caba79b6a812b54a4351 Mon Sep 17 00:00:00 2001 From: Gianluca Mondini Date: Mon, 9 Jun 2025 16:10:58 +0200 Subject: [PATCH 17/17] Use `assert.Equal` instead of `assert.ObjectsAreEqual` --- tests/array_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/array_test.go b/tests/array_test.go index 1c09bb0537..014ff33fb2 100644 --- a/tests/array_test.go +++ b/tests/array_test.go @@ -405,10 +405,7 @@ func TestSQLScannerArray(t *testing.T) { col1 = sqlScannerArray{} ) require.NoError(t, rows.Scan(&col1)) - ok := assert.ObjectsAreEqual(col1Data, col1.value) - if !ok { - t.Fatalf("expected %#v, got %#v", col1Data, col1.value) - } + assert.Equal(t, col1Data, col1.value) } require.NoError(t, rows.Close()) require.NoError(t, rows.Err())