Skip to content

Commit 41e5312

Browse files
authored
Revert "RowBinary: de- and encode dynamic JSON (#296)" (#328)
This reverts commit d624ecd.
1 parent 5df1663 commit 41e5312

2 files changed

Lines changed: 15 additions & 146 deletions

File tree

lib/ch/row_binary.ex

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,7 @@ defmodule Ch.RowBinary do
420420
_ when is_integer(value) -> [0x0A | encode(:i64, value)]
421421
_ when is_float(value) -> [0x0E | encode(:f64, value)]
422422
%Date{} -> [0x0F | encode(:date, value)]
423-
%DateTime{} -> [0x11 | encode(:datetime, value)]
424423
%NaiveDateTime{} -> [0x11 | encode(:datetime, value)]
425-
%{} -> [0x30, 0x00, 0x80, 0x08, 0x20, 0x00, 0x00, 0x00 | encode(:json, value)]
426424
[] -> [0x1E, 0x00]
427425
end
428426
end
@@ -1132,74 +1130,6 @@ defmodule Ch.RowBinary do
11321130
decode_dynamic_continue(rest, [:low_cardinality | dynamic], types_rest, row, rows, types)
11331131
end
11341132

1135-
# JSON(max_dynamic_paths=N, max_dynamic_types=M, path Type, SKIP skip_path, SKIP REGEXP skip_path_regexp) 0x30<uint8_serialization_version><var_int_max_dynamic_paths><uint8_max_dynamic_types><var_uint_number_of_typed_paths><var_uint_path_name_size_1><path_name_data_1><encoded_type_1>...<var_uint_number_of_skip_paths><var_uint_skip_path_size_1><skip_path_data_1>...<var_uint_number_of_skip_path_regexps><var_uint_skip_path_regexp_size_1><skip_path_data_regexp_1>...
1136-
defp decode_dynamic(<<0x30, rest::bytes>>, dynamic, types_rest, row, rows, types) do
1137-
# Assert uint8_serialization_version to be 0
1138-
<<0x00, rest::bytes>> = rest
1139-
1140-
# Skip var_int_max_dynamic_paths
1141-
{_paths, rest} = read_varint(rest)
1142-
1143-
# Skip uint8_max_dynamic_types
1144-
<<_val, rest::bytes>> = rest
1145-
1146-
# Read var_uint_number_of_typed_paths
1147-
{typed_paths, rest} = read_varint(rest)
1148-
1149-
# Skip `typed_paths` typed paths
1150-
rest =
1151-
Enum.reduce(1..typed_paths//1, rest, fn _, rest ->
1152-
{count, rest} = read_varint(rest)
1153-
<<_discard::size(count)-bytes, rest::bytes>> = rest
1154-
skip_type(rest)
1155-
end)
1156-
1157-
# Read var_uint_number_of_skip_paths
1158-
{skip_paths, rest} = read_varint(rest)
1159-
1160-
# Skip `skip_paths` skipped paths
1161-
rest =
1162-
Enum.reduce(1..skip_paths//1, rest, fn _, rest ->
1163-
{count, rest} = read_varint(rest)
1164-
<<_discard::size(count)-bytes, rest::bytes>> = rest
1165-
rest
1166-
end)
1167-
1168-
# Read var_uint_number_of_skip_path_regexps
1169-
{skip_path_regexes, rest} = read_varint(rest)
1170-
1171-
# Skip `skip_path_regexes` skipped paths regex
1172-
rest =
1173-
Enum.reduce(1..skip_path_regexes//1, rest, fn _, rest ->
1174-
{count, rest} = read_varint(rest)
1175-
<<_discard::size(count)-bytes, rest::bytes>> = rest
1176-
rest
1177-
end)
1178-
1179-
decode_dynamic_continue(rest, [:json | dynamic], types_rest, row, rows, types)
1180-
end
1181-
1182-
for {pattern, value} <- varints do
1183-
defp read_varint(<<unquote(pattern), rest::bytes>>), do: {unquote(value), rest}
1184-
end
1185-
1186-
other_dynamic_types = [
1187-
datetime: 0x11,
1188-
set: 0x21,
1189-
bfloat16: 0x31,
1190-
time: 0x32
1191-
]
1192-
1193-
# Consume a type header from binary input, returning the rest.
1194-
# TODO: Only supports single-byte type headers for now.
1195-
def skip_type(<<type, rest::bytes>>)
1196-
when type in unquote(Keyword.values(dynamic_types ++ other_dynamic_types)), do: rest
1197-
1198-
def skip_type(<<type, _::bytes>>) do
1199-
raise ArgumentError,
1200-
"Unsupported type definition (starting with 0x#{Base.encode16(<<type>>)}) while decoding dynamic JSON. Only single-byte type identifiers are currently supported."
1201-
end
1202-
12031133
# TODO
12041134
# Enum8 0x17 <var_uint_number_of_elements><var_uint_name_size_1><name_data_1><int8_value_1>...<var_uint_name_size_N><name_data_N><int8_value_N>
12051135
# Enum16 0x18 <var_uint_number_of_elements><var_uint_name_size_1><name_data_1><int16_little_endian_value_1>...><var_uint_name_size_N><name_data_N><int16_little_endian_value_N>
@@ -1215,6 +1145,7 @@ defmodule Ch.RowBinary do
12151145
# Custom type (Ring, Polygon, etc) 0x2C<var_uint_type_name_size><type_name_data>
12161146
# SimpleAggregateFunction(function_name(param_1, ..., param_N), arg_T1, ..., arg_TN) 0x2E<var_uint_function_name_size><function_name_data><var_uint_number_of_parameters><param_1>...<param_N><var_uint_number_of_arguments><argument_type_encoding_1>...<argument_type_encoding_N> (see aggregate function parameter binary encoding)
12171147
# Nested(name1 T1, ..., nameN TN) 0x2F<var_uint_number_of_elements><var_uint_name_size_1><name_data_1><nested_type_encoding_1>...<var_uint_name_size_N><name_data_N><nested_type_encoding_N>
1148+
# JSON(max_dynamic_paths=N, max_dynamic_types=M, path Type, SKIP skip_path, SKIP REGEXP skip_path_regexp) 0x30<uint8_serialization_version><var_int_max_dynamic_paths><uint8_max_dynamic_types><var_uint_number_of_typed_paths><var_uint_path_name_size_1><path_name_data_1><encoded_type_1>...<var_uint_number_of_skip_paths><var_uint_skip_path_size_1><skip_path_data_1>...<var_uint_number_of_skip_path_regexps><var_uint_skip_path_regexp_size_1><skip_path_data_regexp_1>...
12181149

12191150
unsupported_dynamic_types = %{
12201151
"Enum8" => 0x17,
@@ -1230,7 +1161,8 @@ defmodule Ch.RowBinary do
12301161
"Dynamic" => 0x2B,
12311162
"CustomType" => 0x2C,
12321163
"SimpleAggregateFunction" => 0x2E,
1233-
"Nested" => 0x2F
1164+
"Nested" => 0x2F,
1165+
"JSON" => 0x30
12341166
}
12351167

12361168
for {type, code} <- unsupported_dynamic_types do

test/ch/json_test.exs

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -345,81 +345,18 @@ defmodule Ch.JSONTest do
345345
]
346346
]
347347

348-
assert Ch.query!(conn, "SELECT json.a.b FROM json_test;", [], query_options).rows == [
349-
[
350-
[
351-
%{"c" => 42, "d" => "Hello", "f" => [[%{"g" => 42.42}]], "k" => %{"j" => 1000}},
352-
%{"c" => 43},
353-
%{
354-
"d" => "My",
355-
"e" => [1, 2, 3],
356-
"f" => [[%{"g" => 43.43, "h" => "2020-01-01"}]],
357-
"k" => %{"j" => 2000}
358-
}
359-
]
360-
],
361-
[[1, 2, 3]],
362-
[
363-
[
364-
%{"c" => 44, "f" => [[%{"h" => "2020-01-02"}]]},
365-
%{
366-
"d" => "World",
367-
"e" => [4, 5, 6],
368-
"f" => [[%{"g" => 44.44}]],
369-
"k" => %{"j" => 3000}
370-
}
371-
]
372-
]
373-
]
374-
375-
assert Ch.query!(
376-
conn,
377-
"SELECT json.a.b[].c, json.a.b[].f, json.a.b[].d FROM json_test;",
378-
[],
379-
query_options
380-
).rows == [
381-
[
382-
[42, 43, nil],
383-
[[[%{"g" => 42.42}]], nil, [[%{"g" => 43.43, "h" => "2020-01-01"}]]],
384-
["Hello", nil, "My"]
385-
],
386-
[[], [], []],
387-
[[44, nil], [[[%{"h" => "2020-01-02"}]], [[%{"g" => 44.44}]]], [nil, "World"]]
388-
]
389-
390-
enable_time_time64_type = [enable_time_time64_type: 1]
391-
392-
query_options =
393-
Keyword.update(query_options, :settings, enable_time_time64_type, fn prev ->
394-
Keyword.merge(prev, enable_time_time64_type)
395-
end)
396-
397-
assert_raise ArgumentError,
398-
"Unsupported type definition (starting with 0x34) while decoding dynamic JSON. Only single-byte type identifiers are currently supported.",
399-
fn ->
400-
Ch.query!(
401-
conn,
402-
~s|SELECT '{"a": "10:00:00.050"}'::JSON(a Time64)::Dynamic;|,
403-
[],
404-
query_options
405-
)
406-
end
407-
end
408-
409-
test "encode JSON in dynamic column", %{conn: conn, query_options: query_options} do
410-
Ch.query!(conn, "CREATE TABLE json_test (value Dynamic) ENGINE = Memory;", [], query_options)
411-
412-
query_options = Keyword.put(query_options, :types, [:dynamic])
413-
414-
Ch.query!(
415-
conn,
416-
"INSERT INTO json_test (value) FORMAT RowBinary",
417-
[[%{"json_obj" => 42}]],
418-
query_options
419-
)
348+
# TODO
349+
assert_raise ArgumentError, "unsupported dynamic type JSON", fn ->
350+
Ch.query!(conn, "SELECT json.a.b, dynamicType(json.a.b) FROM json_test;", [], query_options)
351+
end
420352

421-
assert Ch.query!(conn, "SELECT value FROM json_test").rows == [
422-
[%{"json_obj" => 42}]
423-
]
353+
assert_raise ArgumentError, "unsupported dynamic type JSON", fn ->
354+
Ch.query!(
355+
conn,
356+
"SELECT json.a.b.:`Array(JSON)`.c, json.a.b.:`Array(JSON)`.f, json.a.b.:`Array(JSON)`.d FROM json_test;",
357+
[],
358+
query_options
359+
)
360+
end
424361
end
425362
end

0 commit comments

Comments
 (0)