Skip to content

Commit

Permalink
Fix panic and releasing in batch column (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicStep authored Aug 2, 2023
1 parent 421a61c commit 99725c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
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)))
}

0 comments on commit 99725c4

Please sign in to comment.