Repository navigation
fix(json): consume v0 SharedData to keep native stream aligned #2004
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
Open
BetterAndBetterII
wants to merge
8
commits into
ClickHouse:main
Choose a base branch
from
BetterAndBetterII:fix/json-v0-shared-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9f373e7
fix(json): consume v0 SharedData to keep native stream aligned
BetterAndBetterII 32584f7
Merge branch 'main' into fix/json-v0-shared-data
kavirajk 3cb33ca
Merge branch 'main' into fix/json-v0-shared-data
kavirajk ee0a887
Merge remote-tracking branch 'origin/main' into fix/json-v0-shared-data
joe-clickhouse 40a6e7e
fix: reject unsupported JSON and Dynamic shared data
joe-clickhouse 9167710
Merge branch 'main' into fix/json-v0-shared-data
joe-clickhouse a348b54
fix: clarify legacy JSON workaround and test truncated offsets
joe-clickhouse 53d2608
Merge branch 'main' into fix/json-v0-shared-data
joe-clickhouse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package column | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "testing" | ||
|
|
||
| "github.com/ClickHouse/ch-go/proto" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ClickHouse/clickhouse-go/v2/lib/chcol" | ||
| ) | ||
|
|
||
| func encodeSharedData(t *testing.T, rows [][][2]string) []byte { | ||
| t.Helper() | ||
| col, err := Type("Array(Tuple(String, String))").Column("", nil) | ||
| require.NoError(t, err) | ||
| for _, row := range rows { | ||
| pairs := make([][]any, len(row)) | ||
| for i, p := range row { | ||
| pairs[i] = []any{p[0], p[1]} | ||
| } | ||
| require.NoError(t, col.AppendRow(pairs)) | ||
| } | ||
| var buf proto.Buffer | ||
| col.Encode(&buf) | ||
| return buf.Buf | ||
| } | ||
|
|
||
| func TestJSONDeprecatedSharedDataError(t *testing.T) { | ||
| for _, rows := range [][][][2]string{ | ||
| {{{"overflow", "\x0a\x0d\x00\x00\x00\x00\x00\x00\x00"}, {"other", "unreadable"}}}, | ||
| {{}, {{"overflow", "unreadable"}}}, | ||
| } { | ||
| payload := encodeSharedData(t, rows) | ||
| col := newTestJSONColumn(t) | ||
| col.serializationVersion = JSONDeprecatedObjectSerializationVersion | ||
| reader := proto.NewReader(bytes.NewReader(payload)) | ||
| err := col.Decode(reader, len(rows)) | ||
| require.ErrorContains(t, err, "cannot decode JSON shared data") | ||
| require.ErrorContains(t, err, "25.6") | ||
| require.ErrorContains(t, err, "output_format_native_use_flattened_dynamic_and_json_serialization=1") | ||
| require.ErrorContains(t, err, "changeable_in_readonly") | ||
| } | ||
| } | ||
|
|
||
| func TestJSONDeprecatedSharedDataEmptyRoundtrip(t *testing.T) { | ||
| src := newTestJSONColumn(t) | ||
| require.NoError(t, src.AppendRow(map[string]any{"kept": int64(7)})) | ||
|
|
||
| var buf proto.Buffer | ||
| require.NoError(t, src.WriteStatePrefix(&buf)) | ||
| src.Encode(&buf) | ||
|
|
||
| dst := newTestJSONColumn(t) | ||
| reader := proto.NewReader(bytes.NewReader(buf.Buf)) | ||
| require.NoError(t, dst.ReadStatePrefix(reader)) | ||
| require.NoError(t, dst.Decode(reader, 1)) | ||
|
|
||
| obj, ok := dst.Row(0, false).(*chcol.JSON) | ||
| require.True(t, ok) | ||
| kept, ok := chcol.ExtractJSONPathAs[int64](obj, "kept") | ||
| require.True(t, ok) | ||
| require.Equal(t, int64(7), kept) | ||
| } | ||
|
|
||
| func TestJSONDeprecatedSharedDataMalformed(t *testing.T) { | ||
| var offsets [16]byte | ||
| binary.LittleEndian.PutUint64(offsets[:], 2) | ||
| binary.LittleEndian.PutUint64(offsets[8:], 1) | ||
| col := newTestJSONColumn(t) | ||
| require.ErrorContains(t, col.decodeSharedData_v1(proto.NewReader(bytes.NewReader(offsets[:])), 2), "invalid shared data offsets") | ||
| for _, payload := range [][]byte{nil, offsets[:7]} { | ||
| require.ErrorContains(t, col.decodeSharedData_v1(proto.NewReader(bytes.NewReader(payload)), 1), "failed to read shared data offsets") | ||
| } | ||
| } | ||
|
|
||
| func TestDynamicDeprecatedSharedVariantError(t *testing.T) { | ||
| for _, raw := range []string{"", "unreadable", "\x0a\x0d\x00\x00\x00\x00\x00\x00\x00"} { | ||
| var buf proto.Buffer | ||
| buf.PutByte(0) | ||
| buf.PutString(raw) | ||
| col := &Dynamic{columns: []Interface{&SharedVariant{}}, columnIndexByType: map[string]int{"SharedVariant": 0}} | ||
| reader := proto.NewReader(bytes.NewReader(buf.Buf)) | ||
| require.ErrorContains(t, col.decodeData_v1(reader, 1), "cannot decode Dynamic SharedVariant") | ||
| } | ||
|
joe-clickhouse marked this conversation as resolved.
Resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package issues | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/ClickHouse/clickhouse-go/v2" | ||
| "github.com/ClickHouse/clickhouse-go/v2/lib/chcol" | ||
| clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests" | ||
| clickhouse_std_tests "github.com/ClickHouse/clickhouse-go/v2/tests/std" | ||
| ) | ||
|
|
||
| func TestIssue1854(t *testing.T) { | ||
| env, err := GetIssuesTestEnvironment() | ||
| require.NoError(t, err) | ||
| clickhouse_tests.TestProtocols(t, func(t *testing.T, protocol clickhouse.Protocol) { | ||
| settings := clickhouse.Settings{ | ||
| "allow_experimental_json_type": 1, | ||
| "allow_experimental_dynamic_type": 1, | ||
| "allow_experimental_variant_type": 1, | ||
| } | ||
| opts := clickhouse_tests.ClientOptionsFromEnv(env, settings, protocol == clickhouse.HTTP) | ||
| opts.MaxOpenConns, opts.MaxIdleConns = 1, 1 | ||
| t.Run("ClickHouse", func(t *testing.T) { | ||
| conn, err := clickhouse.Open(&opts) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { require.NoError(t, conn.Close()) }) | ||
| if !clickhouse_tests.CheckMinServerServerVersion(conn, 24, 8, 0) { | ||
| t.Skip("JSON requires ClickHouse 24.8") | ||
| } | ||
| checkIssue1854(t, clickhouse_tests.CheckMinServerServerVersion(conn, 25, 6, 0), func(ctx context.Context, q string, dest ...any) error { | ||
| return conn.QueryRow(ctx, q).Scan(dest...) | ||
| }) | ||
| }) | ||
| t.Run("database_sql", func(t *testing.T) { | ||
| db := clickhouse.OpenDB(&opts) | ||
| db.SetMaxOpenConns(1) | ||
| t.Cleanup(func() { require.NoError(t, db.Close()) }) | ||
| if !clickhouse_std_tests.CheckMinServerVersion(db, 24, 8, 0) { | ||
| t.Skip("JSON requires ClickHouse 24.8") | ||
| } | ||
| checkIssue1854(t, clickhouse_std_tests.CheckMinServerVersion(db, 25, 6, 0), func(ctx context.Context, q string, dest ...any) error { | ||
| return db.QueryRowContext(ctx, q).Scan(dest...) | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func checkIssue1854(t *testing.T, supportsFlattened bool, scan func(context.Context, string, ...any) error) { | ||
| t.Helper() | ||
| const setting = "output_format_native_use_flattened_dynamic_and_json_serialization" | ||
| legacy := context.Background() | ||
| if supportsFlattened { | ||
| legacy = clickhouse.Context(legacy, clickhouse.WithSettings(clickhouse.Settings{setting: 0})) | ||
| } | ||
| for _, tc := range []struct{ name, expr, storage string }{ | ||
| {"JSON", `'{"x":13,"y":"east"}'::JSON(max_dynamic_paths=0)`, "JSON shared data"}, | ||
| {"JSON_Dynamic", `'{"x":13,"y":"east"}'::JSON(max_dynamic_types=0)`, "Dynamic SharedVariant"}, | ||
| {"Dynamic", `toInt64(13)::Dynamic(max_types=0)`, "Dynamic SharedVariant"}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| query := "SELECT " + tc.expr + ", 'tail'" | ||
| var doc chcol.JSON | ||
| var value chcol.Dynamic | ||
| var dest any = &doc | ||
| if tc.name == "Dynamic" { | ||
| dest = &value | ||
| } | ||
| var tail string | ||
| err := scan(legacy, query, dest, &tail) | ||
| require.ErrorContains(t, err, "cannot decode "+tc.storage) | ||
| require.ErrorContains(t, err, setting+"=1") | ||
| var one uint8 | ||
| require.NoError(t, scan(context.Background(), "SELECT toUInt8(1)", &one)) | ||
| require.Equal(t, uint8(1), one) | ||
| if !supportsFlattened { | ||
| return | ||
| } | ||
| flattened := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{setting: 1})) | ||
| require.NoError(t, scan(flattened, query, dest, &tail)) | ||
| require.Equal(t, "tail", tail) | ||
| if tc.name == "Dynamic" { | ||
| require.Equal(t, int64(13), value.Any()) | ||
| return | ||
| } | ||
| x, ok := chcol.ExtractJSONPathAs[int64](&doc, "x") | ||
| require.True(t, ok) | ||
| require.Equal(t, int64(13), x) | ||
| y, ok := chcol.ExtractJSONPathAs[string](&doc, "y") | ||
| require.True(t, ok) | ||
| require.Equal(t, "east", y) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.