Skip to content

Commit

Permalink
Merge pull request #1043 from ClickHouse/fix_json_array
Browse files Browse the repository at this point in the history
fix(json): proper encoding for json array
  • Loading branch information
SpencerTorres authored Feb 16, 2025
2 parents 0e83566 + c419d9c commit d7f188d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
11 changes: 5 additions & 6 deletions proto/_golden/col_json_str.hex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
00000000 01 00 00 00 00 00 00 00 11 7b 22 78 22 3a 20 35 |.........{"x": 5|
00000010 2c 20 22 79 22 3a 20 31 30 7d 1b 7b 22 61 22 3a |, "y": 10}.{"a":|
00000020 20 22 74 65 73 74 22 2c 20 22 62 22 3a 20 22 74 | "test", "b": "t|
00000030 65 73 74 32 22 7d 21 7b 22 61 22 3a 20 22 6f 62 |est2"}!{"a": "ob|
00000040 6a 20 74 65 73 74 22 2c 20 22 62 22 3a 20 7b 22 |j test", "b": {"|
00000050 63 22 3a 20 32 30 7d 7d |c": 20}}|
00000000 11 7b 22 78 22 3a 20 35 2c 20 22 79 22 3a 20 31 |.{"x": 5, "y": 1|
00000010 30 7d 1b 7b 22 61 22 3a 20 22 74 65 73 74 22 2c |0}.{"a": "test",|
00000020 20 22 62 22 3a 20 22 74 65 73 74 32 22 7d 21 7b | "b": "test2"}!{|
00000030 22 61 22 3a 20 22 6f 62 6a 20 74 65 73 74 22 2c |"a": "obj test",|
00000040 20 22 62 22 3a 20 7b 22 63 22 3a 20 32 30 7d 7d | "b": {"c": 20}}|
Binary file modified proto/_golden/col_json_str.raw
Binary file not shown.
11 changes: 5 additions & 6 deletions proto/_golden/col_json_str_bytes.hex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
00000000 01 00 00 00 00 00 00 00 11 7b 22 78 22 3a 20 35 |.........{"x": 5|
00000010 2c 20 22 79 22 3a 20 31 30 7d 1b 7b 22 61 22 3a |, "y": 10}.{"a":|
00000020 20 22 74 65 73 74 22 2c 20 22 62 22 3a 20 22 74 | "test", "b": "t|
00000030 65 73 74 32 22 7d 21 7b 22 61 22 3a 20 22 6f 62 |est2"}!{"a": "ob|
00000040 6a 20 74 65 73 74 22 2c 20 22 62 22 3a 20 7b 22 |j test", "b": {"|
00000050 63 22 3a 20 32 30 7d 7d |c": 20}}|
00000000 11 7b 22 78 22 3a 20 35 2c 20 22 79 22 3a 20 31 |.{"x": 5, "y": 1|
00000010 30 7d 1b 7b 22 61 22 3a 20 22 74 65 73 74 22 2c |0}.{"a": "test",|
00000020 20 22 62 22 3a 20 22 74 65 73 74 32 22 7d 21 7b | "b": "test2"}!{|
00000030 22 61 22 3a 20 22 6f 62 6a 20 74 65 73 74 22 2c |"a": "obj test",|
00000040 20 22 62 22 3a 20 7b 22 63 22 3a 20 32 30 7d 7d | "b": {"c": 20}}|
Binary file modified proto/_golden/col_json_str_bytes.raw
Binary file not shown.
11 changes: 5 additions & 6 deletions proto/_golden/column_of_json_bytes.hex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
00000000 01 00 00 00 00 00 00 00 11 7b 22 78 22 3a 20 35 |.........{"x": 5|
00000010 2c 20 22 79 22 3a 20 31 30 7d 1b 7b 22 61 22 3a |, "y": 10}.{"a":|
00000020 20 22 74 65 73 74 22 2c 20 22 62 22 3a 20 22 74 | "test", "b": "t|
00000030 65 73 74 32 22 7d 21 7b 22 61 22 3a 20 22 6f 62 |est2"}!{"a": "ob|
00000040 6a 20 74 65 73 74 22 2c 20 22 62 22 3a 20 7b 22 |j test", "b": {"|
00000050 63 22 3a 20 32 30 7d 7d |c": 20}}|
00000000 11 7b 22 78 22 3a 20 35 2c 20 22 79 22 3a 20 31 |.{"x": 5, "y": 1|
00000010 30 7d 1b 7b 22 61 22 3a 20 22 74 65 73 74 22 2c |0}.{"a": "test",|
00000020 20 22 62 22 3a 20 22 74 65 73 74 32 22 7d 21 7b | "b": "test2"}!{|
00000030 22 61 22 3a 20 22 6f 62 6a 20 74 65 73 74 22 2c |"a": "obj test",|
00000040 20 22 62 22 3a 20 7b 22 63 22 3a 20 32 30 7d 7d | "b": {"c": 20}}|
Binary file modified proto/_golden/column_of_json_bytes.raw
Binary file not shown.
22 changes: 14 additions & 8 deletions proto/col_json_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
_ Column = (*ColJSONStr)(nil)
_ ColumnOf[string] = (*ColJSONStr)(nil)
_ Arrayable[string] = (*ColJSONStr)(nil)
_ StateEncoder = (*ColJSONStr)(nil)
_ StateDecoder = (*ColJSONStr)(nil)
)

// Type returns ColumnType of JSON.
Expand All @@ -51,19 +53,18 @@ func (c *ColJSONStr) Reset() {
c.Str.Reset()
}

// EncodeColumn encodes String rows to *Buffer.
func (c ColJSONStr) EncodeColumn(b *Buffer) {
// EncodeState encodes the JSON serialization version
func (c *ColJSONStr) EncodeState(b *Buffer) {
b.PutUInt64(JSONStringSerializationVersion)
}

// EncodeColumn encodes String rows to *Buffer.
func (c ColJSONStr) EncodeColumn(b *Buffer) {
c.Str.EncodeColumn(b)
}

// WriteColumn writes JSON rows to *Writer.
func (c ColJSONStr) WriteColumn(w *Writer) {
w.ChainBuffer(func(b *Buffer) {
b.PutUInt64(JSONStringSerializationVersion)
})

c.Str.WriteColumn(w)
}

Expand Down Expand Up @@ -92,8 +93,8 @@ func (c ColJSONStr) ForEachBytes(f func(i int, b []byte) error) error {
return c.Str.ForEachBytes(f)
}

// DecodeColumn decodes String rows from *Reader.
func (c *ColJSONStr) DecodeColumn(r *Reader, rows int) error {
// DecodeState decodes the JSON serialization version
func (c *ColJSONStr) DecodeState(r *Reader) error {
jsonSerializationVersion, err := r.UInt64()
if err != nil {
return errors.Wrap(err, "failed to read json serialization version")
Expand All @@ -103,6 +104,11 @@ func (c *ColJSONStr) DecodeColumn(r *Reader, rows int) error {
return errors.Errorf("received invalid JSON string serialization version %d. Setting \"output_format_native_write_json_as_string\" must be enabled.", jsonSerializationVersion)
}

return nil
}

// DecodeColumn decodes String rows from *Reader.
func (c *ColJSONStr) DecodeColumn(r *Reader, rows int) error {
return c.Str.DecodeColumn(r, rows)
}

Expand Down

0 comments on commit d7f188d

Please # to comment.