Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic and releasing in batch column #1055

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions conn_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ func (b *batch) IsSent() bool {

func (b *batch) Column(idx int) driver.BatchColumn {
if len(b.block.Columns) <= idx {
b.release(nil)
err := &OpError{
Op: "batch.Column",
Err: fmt.Errorf("invalid column index %d", idx),
}

b.release(err)

return &batchColumn{
err: &OpError{
Op: "batch.Column",
Err: fmt.Errorf("invalid column index %d", idx),
},
err: err,
}
}
return &batchColumn{
Expand Down Expand Up @@ -229,13 +232,12 @@ type batchColumn struct {
}

func (b *batchColumn) Append(v any) (err error) {
if b.batch.IsSent() {
return ErrBatchAlreadySent
}
if b.err != nil {
b.release(b.err)
return b.err
}
if b.batch.IsSent() {
return ErrBatchAlreadySent
}
if _, err = b.column.Append(v); err != nil {
b.release(err)
return err
Expand All @@ -244,13 +246,12 @@ func (b *batchColumn) Append(v any) (err error) {
}

func (b *batchColumn) AppendRow(v any) (err error) {
if b.batch.IsSent() {
return ErrBatchAlreadySent
}
if b.err != nil {
b.release(b.err)
return b.err
}
if b.batch.IsSent() {
return ErrBatchAlreadySent
}
if err = b.column.AppendRow(v); err != nil {
b.release(err)
return err
Expand Down
38 changes: 38 additions & 0 deletions tests/issues/1053_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package issues

import (
"context"
"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"github.com/stretchr/testify/require"
"testing"
)

func Test1053(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_1053 (
Col1 UInt64
) Engine MergeTree() ORDER BY tuple()
`
defer func() {
conn.Exec(ctx, "DROP TABLE IF EXISTS test_1053")
}()
require.NoError(t, conn.Exec(ctx, ddl))

batch, err := conn.PrepareBatch(ctx, `INSERT INTO test_1053`)
require.NoError(t, err)

column := batch.Column(1000) // doesn't exist column

require.Error(t, column.Append(uint64(1)))
require.Error(t, column.AppendRow(uint64(1)))
}