Skip to content

Commit

Permalink
Batch Columns() []column.Interface method (#1277)
Browse files Browse the repository at this point in the history
* impl Batch.Block() method

* replace Batch.Block() => Batch.Columns()

* add TestBatchColumns
  • Loading branch information
egsam98 committed Jun 6, 2024
1 parent 3a49215 commit 8ad6ec6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions conn_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -319,6 +320,10 @@ func (b *batch) Rows() int {
return b.block.Rows()
}

func (b *batch) Columns() []column.Interface {
return slices.Clone(b.block.Columns)
}

func (b *batch) closeQuery() error {
if err := b.conn.sendData(&proto.Block{}, ""); err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions conn_http_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"regexp"
"slices"
"strings"

"github.com/ClickHouse/clickhouse-go/v2/lib/column"
Expand Down Expand Up @@ -240,4 +241,8 @@ func (b *httpBatch) Rows() int {
return b.block.Rows()
}

func (b *httpBatch) Columns() []column.Interface {
return slices.Clone(b.block.Columns)
}

var _ driver.Batch = (*httpBatch)(nil)
2 changes: 2 additions & 0 deletions lib/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"time"

"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)

Expand Down Expand Up @@ -85,6 +86,7 @@ type (
Send() error
IsSent() bool
Rows() int
Columns() []column.Interface
}
BatchColumn interface {
Append(any) error
Expand Down
31 changes: 30 additions & 1 deletion tests/batch_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package tests
import (
"testing"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
)

// TestBatchAppendRows tests experimental batch rows blocks append feature.
Expand Down Expand Up @@ -64,3 +66,30 @@ func TestBatchAppendRows(t *testing.T) {
require.NoError(t, row.Scan(&count))
assert.Equal(t, 1000000, int(count))
}

// TestBatchColumns tests Batch.Columns() method functionality
func TestBatchColumns(t *testing.T) {
ctx := context.Background()
conn, err := GetNativeConnection(nil, nil, nil)
require.NoError(t, err)
// Prepare test table
require.NoError(t, conn.Exec(ctx, `
CREATE TABLE test_table (
Col1 Int,
Col2 String
) Engine MergeTree() ORDER BY tuple()
`))
defer func() {
conn.Exec(ctx, "DROP TABLE IF EXISTS test_table")
}()

batch, err := conn.PrepareBatch(ctx, "INSERT INTO test_table")
require.NoError(t, err)
columns := batch.Columns()
if assert.Len(t, columns, 2) {
assert.IsType(t, new(column.Int32), columns[0])
assert.Equal(t, "Col1", columns[0].Name())
assert.IsType(t, new(column.String), columns[1])
assert.Equal(t, "Col2", columns[1].Name())
}
}

0 comments on commit 8ad6ec6

Please sign in to comment.