Skip to content

Commit

Permalink
decode nested prefixes in variant
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerTorres committed Feb 20, 2025
1 parent 1a4c73f commit 7a8a132
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/column/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ func (c *Variant) decodeHeader(reader *proto.Reader) error {
return fmt.Errorf("unsupported variant discriminator version: %d", variantSerializationVersion)
}

for _, col := range c.columns {
if serialize, ok := col.(CustomSerialization); ok {
if err := serialize.ReadStatePrefix(reader); err != nil {
return fmt.Errorf("failed to read prefix for type %s in variant: %w", col.Type(), err)
}
}
}

return nil
}

Expand Down
27 changes: 25 additions & 2 deletions tests/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func setupJSONTest(t *testing.T) driver.Conn {
SkipOnCloud(t, "cannot modify JSON settings on cloud")

conn, err := GetNativeConnection(clickhouse.Settings{
"max_execution_time": 60,
"allow_experimental_json_type": true,
"max_execution_time": 60,
"allow_experimental_variant_type": true,
"allow_experimental_dynamic_type": true,
"allow_experimental_json_type": true,
}, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
Expand Down Expand Up @@ -415,3 +417,24 @@ func TestJSON_BatchFlush(t *testing.T) {
i++
}
}

// https://github.com/grafana/clickhouse-datasource/issues/1168
func TestJSONArrayDynamic(t *testing.T) {
ctx := context.Background()
conn := setupJSONTest(t)

rows, err := conn.Query(ctx, `SELECT ['{"x":5}','{"y":6}']::Array(JSON)::Dynamic AS c`)
require.NoError(t, err)

require.True(t, rows.Next())
}

func TestJSONArrayVariant(t *testing.T) {
ctx := context.Background()
conn := setupJSONTest(t)

rows, err := conn.Query(ctx, `SELECT ['{"x":5}','{"y":6}']::Array(JSON)::Variant(Array(JSON)) AS c`)
require.NoError(t, err)

require.True(t, rows.Next())
}

0 comments on commit 7a8a132

Please # to comment.