Skip to content

Commit 5df1663

Browse files
authored
Revert "Add support for JSON type options (#309)" (#327)
This reverts commit a1f1ead.
1 parent 13ed2b3 commit 5df1663

5 files changed

Lines changed: 3 additions & 116 deletions

File tree

lib/ch.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ defmodule Ch do
156156
def cast(value, {:datetime64, _p, "UTC"}), do: Ecto.Type.cast(:utc_datetime_usec, value)
157157
def cast(value, {:fixed_string, _s}), do: Ecto.Type.cast(:string, value)
158158
def cast(value, :json), do: Ecto.Type.cast(:map, value)
159-
def cast(value, {:json, _opts}), do: Ecto.Type.cast(:map, value)
160159
def cast(value, :dynamic), do: {:ok, value}
161160

162161
for size <- [8, 16, 32, 64, 128, 256] do

lib/ch/row_binary.ex

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ defmodule Ch.RowBinary do
173173
defp encoding_type(:ring), do: {:array, :point}
174174
defp encoding_type(:polygon), do: {:array, {:array, :point}}
175175
defp encoding_type(:multipolygon), do: {:array, {:array, {:array, :point}}}
176-
defp encoding_type({:json, opts}), do: {:json, opts}
177176

178177
defp encoding_type(type) do
179178
raise ArgumentError, "unsupported type for encoding: #{inspect(type)}"
@@ -200,13 +199,6 @@ defmodule Ch.RowBinary do
200199
encode(:string, Jason.encode_to_iodata!(json))
201200
end
202201

203-
def encode({:json, _opts}, json) do
204-
# assuming it can be sent as text and not "native" binary JSON
205-
# i.e. assumes `settings: [input_format_binary_read_json_as_string: 1]`
206-
# TODO
207-
encode(:string, Jason.encode_to_iodata!(json))
208-
end
209-
210202
def encode({:fixed_string, size}, str) when byte_size(str) == size do
211203
str
212204
end
@@ -777,7 +769,6 @@ defmodule Ch.RowBinary do
777769
defp decoding_type(:ring), do: {:array, :point}
778770
defp decoding_type(:polygon), do: {:array, {:array, :point}}
779771
defp decoding_type(:multipolygon), do: {:array, {:array, {:array, :point}}}
780-
defp decoding_type({:json, _opts}), do: :json
781772

782773
defp decoding_type(type) do
783774
raise ArgumentError, "unsupported type for decoding: #{inspect(type)}"

lib/ch/types.ex

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ defmodule Ch.Types do
6868
def unquote(name)(), do: unquote(name)
6969
end
7070

71-
def json(opts), do: {:json, opts}
72-
7371
@doc """
7472
Helper for `DateTime` ClickHouse type:
7573
@@ -361,21 +359,7 @@ defmodule Ch.Types do
361359

362360
def decode("DateTime"), do: :datetime
363361
def decode("Dynamic"), do: :dynamic
364-
365-
def decode("JSON" <> options) do
366-
opts =
367-
options
368-
|> String.trim()
369-
|> String.trim_leading("(")
370-
|> String.trim_trailing(")")
371-
|> String.split(",")
372-
|> Enum.map(&String.trim/1)
373-
374-
{settings, rest} = Enum.split_with(opts, &String.contains?(&1, "="))
375-
{skips, type_hints} = Enum.split_with(rest, &String.match?(&1, ~r/^SKIP\s+/i))
376-
377-
{:json, [settings: settings, type_hints: Enum.sort(type_hints), skips: skips]}
378-
end
362+
def decode("JSON" <> _options), do: :json
379363

380364
def decode(type) do
381365
try do
@@ -594,19 +578,6 @@ defmodule Ch.Types do
594578
def encode(unquote(decoded)), do: unquote(encoded)
595579
end
596580

597-
def encode({:json, opts}) do
598-
opts =
599-
[
600-
opts[:settings],
601-
Enum.sort(opts[:type_hints] || []),
602-
opts[:skips] || []
603-
]
604-
|> List.flatten()
605-
|> Enum.intersperse(", ")
606-
607-
["JSON(", opts, ?)]
608-
end
609-
610581
def encode(:datetime), do: "DateTime"
611582
def encode({:time64, p}), do: ["Time64(", String.Chars.Integer.to_string(p), ?)]
612583
def encode({:nullable, type}), do: ["Nullable(", encode(type), ?)]

test/ch/json_test.exs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
defmodule Ch.JSONTest do
22
use ExUnit.Case, parameterize: [%{query_options: []}, %{query_options: [multipart: true]}]
33

4-
import Ch.Test,
5-
only: [
6-
parameterize_query: 4
7-
]
8-
94
@moduletag :json
105

116
setup ctx do
@@ -427,50 +422,4 @@ defmodule Ch.JSONTest do
427422
[%{"json_obj" => 42}]
428423
]
429424
end
430-
431-
test "insert with settings, type hints and skip directives", %{
432-
conn: conn,
433-
query_options: query_options
434-
} do
435-
Ch.query!(
436-
conn,
437-
"CREATE TABLE json_test
438-
(
439-
`json` JSON(
440-
max_dynamic_types=254,
441-
max_dynamic_paths=10000,
442-
name String,
443-
age UInt8,
444-
SKIP pass.body_part,
445-
SKIP REGEXP 't.*'
446-
)
447-
) ENGINE = Memory",
448-
[],
449-
query_options
450-
)
451-
452-
stmt = "INSERT INTO json_test FORMAT RowBinaryWithNamesAndTypes"
453-
rows = [[%{name: "John", age: 30, pass: %{body_part: "none"}, t: %{foo: "bar"}}]]
454-
names = ["json"]
455-
456-
opts = [
457-
names: names,
458-
types: [
459-
Ch.Types.decode("""
460-
JSON(
461-
max_dynamic_types=254,
462-
max_dynamic_paths=10000,
463-
age UInt8, name String,
464-
SKIP `pass.body_part`,
465-
SKIP REGEXP 't.*'
466-
)
467-
""")
468-
]
469-
]
470-
471-
assert {:ok, %Ch.Result{}} = parameterize_query(%{conn: conn}, stmt, rows, opts)
472-
473-
[[value]] = Ch.query!(conn, "select * from json_test", [], query_options).rows
474-
assert value == %{"age" => 30, "name" => "John"}
475-
end
476425
end

test/ch/types_test.exs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Ch.TypesTest do
22
use ExUnit.Case, async: true
3-
import Ch.Types, only: [decode: 1, encode: 1, json: 1]
3+
import Ch.Types, only: [decode: 1]
44
doctest Ch.Types, import: true
55

66
describe "decode/1" do
@@ -108,30 +108,7 @@ defmodule Ch.TypesTest do
108108
assert decode("JSON") == :json
109109
assert decode(" JSON ") == :json
110110

111-
assert decode("JSON(max_dynamic_types=10)") ==
112-
{:json, [settings: ["max_dynamic_types=10"], type_hints: [], skips: []]}
113-
114-
assert decode("JSON(age UInt8, name String)") ==
115-
{:json, [settings: [], type_hints: ["age UInt8", "name String"], skips: []]}
116-
117-
assert decode("JSON(name String, age UInt8)") ==
118-
{:json, [settings: [], type_hints: ["age UInt8", "name String"], skips: []]}
119-
120-
assert decode("JSON(SKIP a.e)") ==
121-
{:json, [settings: [], type_hints: [], skips: ["SKIP a.e"]]}
122-
123-
assert decode("JSON(max_dynamic_types=10, name String, age UInt8, SKIP a.e)") ==
124-
{:json,
125-
[
126-
settings: ["max_dynamic_types=10"],
127-
type_hints: ["age UInt8", "name String"],
128-
skips: ["SKIP a.e"]
129-
]}
130-
131-
opts = [settings: ["max_dynamic_types=10"], type_hints: ["age UInt8"], skips: []]
132-
assert json(opts) == {:json, opts}
133-
134-
assert to_string(encode(json(opts))) == "JSON(max_dynamic_types=10, age UInt8)"
111+
# TODO JSON(...)
135112
end
136113

137114
test "datetime" do

0 commit comments

Comments
 (0)