-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #774 from mdonkers/batch-intert-nullable-pointers
Handle double pointers for Nullable columns when batch inserting
- Loading branch information
Showing
4 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package issues | ||
|
||
import ( | ||
"context" | ||
"github.com/ClickHouse/clickhouse-go/v2" | ||
"github.com/ClickHouse/clickhouse-go/v2/lib/driver" | ||
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" | ||
"github.com/stretchr/testify/require" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestInsertNullableString(t *testing.T) { | ||
var ( | ||
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{ | ||
"max_execution_time": 60, | ||
}, nil, &clickhouse.Compression{ | ||
Method: clickhouse.CompressionLZ4, | ||
}) | ||
) | ||
ctx := context.Background() | ||
require.NoError(t, err) | ||
const ddl = ` | ||
CREATE TABLE test_nullable_string_insert ( | ||
Col1 String | ||
, Col2 Nullable(String) | ||
) Engine Memory | ||
` | ||
defer func() { | ||
conn.Exec(ctx, "DROP TABLE IF EXISTS test_nullable_string_insert") | ||
}() | ||
require.NoError(t, conn.Exec(ctx, ddl)) | ||
const baseValues = ` | ||
INSERT INTO test_nullable_string_insert (Col1, Col2) VALUES ('Val1', 'Val2'), ('Val11', NULL) | ||
` | ||
require.NoError(t, conn.Exec(ctx, baseValues)) | ||
|
||
rows, err := conn.Query(ctx, "SELECT * FROM test_nullable_string_insert") | ||
require.NoError(t, err) | ||
defer func(rows driver.Rows) { | ||
_ = rows.Close() | ||
}(rows) | ||
|
||
records := make([][]any, 0) | ||
for rows.Next() { | ||
record := make([]any, 0, len(rows.ColumnTypes())) | ||
for _, ct := range rows.ColumnTypes() { | ||
record = append(record, reflect.New(ct.ScanType()).Interface()) | ||
} | ||
err = rows.Scan(record...) | ||
require.NoError(t, err) | ||
|
||
records = append(records, record) | ||
} | ||
require.Greater(t, len(records), 0) | ||
|
||
// Try to insert records in the same format as queried above | ||
batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_nullable_string_insert") | ||
require.NoError(t, err) | ||
|
||
for _, r := range records { | ||
err = batch.Append(r...) | ||
require.NoError(t, err) | ||
} | ||
|
||
err = batch.Send() | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,9 +155,12 @@ func CreateClickHouseTestEnvironment(testSet string) (ClickHouseTestEnvironment, | |
Image: fmt.Sprintf("clickhouse/clickhouse-server:%s", GetClickHouseTestVersion()), | ||
Name: fmt.Sprintf("clickhouse-go-%s-%d", strings.ToLower(testSet), time.Now().UnixNano()), | ||
ExposedPorts: []string{"9000/tcp", "8123/tcp", "9440/tcp", "8443/tcp"}, | ||
WaitingFor: wait.ForAll(wait.ForLog("Ready for connections").WithStartupTimeout(time.Second*time.Duration(120)), wait.ForSQL("9000/tcp", "clickhouse", func(port nat.Port) string { | ||
return fmt.Sprintf("clickhouse://default:ClickHouse@localhost:%s", port.Port()) | ||
})).WithStartupTimeout(time.Second * time.Duration(120)), | ||
WaitingFor: wait.ForAll( | ||
wait.ForLog("Ready for connections").WithStartupTimeout(time.Second*time.Duration(120)), | ||
wait.ForSQL("9000/tcp", "clickhouse", func(port nat.Port) string { | ||
return fmt.Sprintf("clickhouse://default:[email protected]:%s", port.Port()) | ||
}), | ||
).WithStartupTimeout(time.Second * time.Duration(120)), | ||
Mounts: []testcontainers.ContainerMount{ | ||
testcontainers.BindMount(path.Join(basePath, "./resources/custom.xml"), "/etc/clickhouse-server/config.d/custom.xml"), | ||
testcontainers.BindMount(path.Join(basePath, "./resources/admin.xml"), "/etc/clickhouse-server/users.d/admin.xml"), | ||
|
@@ -185,7 +188,7 @@ func CreateClickHouseTestEnvironment(testSet string) (ClickHouseTestEnvironment, | |
HttpPort: hp.Int(), | ||
SslPort: sslPort.Int(), | ||
HttpsPort: hps.Int(), | ||
Host: "localhost", | ||
Host: "127.0.0.1", | ||
// we set this explicitly - note its also set in the /etc/clickhouse-server/users.d/admin.xml | ||
Username: "default", | ||
Password: "ClickHouse", | ||
|