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 support custom serialization in Nested type #1381

Merged
merged 1 commit into from
Aug 22, 2024
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
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 @@ -324,7 +324,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 @@ -347,7 +347,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
Loading