Skip to content

Commit

Permalink
Fix support custom serialization in Nested type (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkaflik authored Aug 22, 2024
1 parent 2a5fcd1 commit fae3668
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
20 changes: 20 additions & 0 deletions lib/column/nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"strings"
"time"

"github.com/ClickHouse/ch-go/proto"
)

type Nested struct {
Expand Down Expand Up @@ -86,4 +88,22 @@ func nestedColumns(raw string) (columns []namedCol) {
return
}

func (col *Nested) ReadStatePrefix(reader *proto.Reader) error {
if serialize, ok := col.Interface.(CustomSerialization); ok {
if err := serialize.ReadStatePrefix(reader); err != nil {
return err
}
}
return nil
}

func (col *Nested) WriteStatePrefix(buffer *proto.Buffer) error {
if serialize, ok := col.Interface.(CustomSerialization); ok {
if err := serialize.WriteStatePrefix(buffer); err != nil {
return err
}
}
return nil
}

var _ Interface = (*Nested)(nil)
41 changes: 41 additions & 0 deletions tests/issues/1297_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package issues

import (
"context"
"testing"

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

func Test1297(t *testing.T) {
testEnv, err := clickhouse_tests.GetTestEnvironment("issues")
require.NoError(t, err)
conn, err := clickhouse_tests.TestClientWithDefaultOptions(testEnv, clickhouse.Settings{
"flatten_nested": "0",
})
require.NoError(t, err)

require.NoError(t, conn.Exec(context.Background(), `CREATE TABLE test_1297
(
Id UInt8,
Device LowCardinality(String),
Nestme Nested(
Id UInt32,
TestLC LowCardinality(String),
Test String
)
)
ENGINE = MergeTree
ORDER BY Id;`), "Create table failed")
t.Cleanup(func() {
conn.Exec(context.Background(), "DROP TABLE IF EXISTS test_1297")
})

batch, err := conn.PrepareBatch(context.Background(), "INSERT INTO test_1297")
require.NoError(t, err, "PrepareBatch failed")

require.NoError(t, batch.Append(uint8(1), "pc", []any{[]any{1, "test LC 1", "test"}, []any{2, "test LC 2", "test"}}), "Append failed")
require.NoError(t, batch.Send())
}
5 changes: 3 additions & 2 deletions tests/read_only_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package tests

import (
"context"
"testing"

"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestReadOnlyUser(t *testing.T) {
Expand Down Expand Up @@ -95,7 +96,7 @@ func TestReadOnlyUser(t *testing.T) {
roEnv.Username = username
roEnv.Password = password

roClient, err := testClientWithDefaultOptions(roEnv, nil)
roClient, err := TestClientWithDefaultOptions(roEnv, nil)
require.NoError(t, err)
defer roClient.Close()

Expand Down
4 changes: 2 additions & 2 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func ClientOptionsFromEnv(env ClickHouseTestEnvironment, settings clickhouse.Set
}
}

func testClientWithDefaultOptions(env ClickHouseTestEnvironment, settings clickhouse.Settings) (driver.Conn, error) {
func TestClientWithDefaultOptions(env ClickHouseTestEnvironment, settings clickhouse.Settings) (driver.Conn, error) {
opts := ClientOptionsFromEnv(env, settings, false)
return clickhouse.Open(&opts)
}
Expand All @@ -346,7 +346,7 @@ func TestClientDefaultSettings(env ClickHouseTestEnvironment) clickhouse.Setting
}

func TestClientWithDefaultSettings(env ClickHouseTestEnvironment) (driver.Conn, error) {
return testClientWithDefaultOptions(env, TestClientDefaultSettings(env))
return TestClientWithDefaultOptions(env, TestClientDefaultSettings(env))
}

func TestDatabaseSQLClientWithDefaultOptions(env ClickHouseTestEnvironment, settings clickhouse.Settings) (*sql.DB, error) {
Expand Down

0 comments on commit fae3668

Please sign in to comment.