diff --git a/CHANGELOG.md b/CHANGELOG.md index 027840f..9a00b68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- fix array casts with `Ch` subtype https://github.com/plausible/ch/pull/118 + ## 0.2.0 (2023-07-28) - move loading and dumping from `Ch` type to the adapter https://github.com/plausible/ch/pull/112 diff --git a/lib/ch.ex b/lib/ch.ex index 6313c79..a027102 100644 --- a/lib/ch.ex +++ b/lib/ch.ex @@ -127,7 +127,7 @@ defmodule Ch do end end - def cast(value, {:array, _type} = array), do: Ecto.Type.cast(array, value) + def cast(value, {:array, type}), do: Ecto.Type.cast({:array, type(type)}, value) def cast(value, {:nullable, type}), do: cast(value, type) def cast(value, {:low_cardinality, type}), do: cast(value, type) def cast(value, {:simple_aggregate_function, _name, type}), do: cast(value, type) diff --git a/test/ch/ecto_type_test.exs b/test/ch/ecto_type_test.exs index 91fde41..0c14533 100644 --- a/test/ch/ecto_type_test.exs +++ b/test/ch/ecto_type_test.exs @@ -330,6 +330,33 @@ defmodule Ch.EctoTypeTest do assert {:ok, "something"} = Ecto.Type.load(type, "something") end + test "SimpleAggregateFunction(groupArrayArray, Array(DateTime('UTC')))" do + assert {:parameterized, Ch, + {:simple_aggregate_function, "groupArrayArray", {:array, {:datetime, "UTC"}}}} = + type = + Ecto.ParameterizedType.init(Ch, + type: "SimpleAggregateFunction(groupArrayArray, Array(DateTime('UTC')))" + ) + + assert Ecto.Type.type(type) == type + + assert Ecto.Type.format(type) == + "#Ch" + + assert {:ok, [~U[2022-11-24 11:57:23Z]]} = Ecto.Type.cast(type, [~U[2022-11-24 11:57:23Z]]) + assert {:ok, [~U[2022-11-24 11:57:23Z]]} = Ecto.Type.cast(type, ["2022-11-24 11:57:23Z"]) + assert {:ok, []} = Ecto.Type.cast(type, []) + assert {:ok, nil} = Ecto.Type.cast(type, nil) + + assert :error = Ecto.Type.cast(type, [~c"something"]) + assert :error = Ecto.Type.cast(type, [:something]) + assert :error = Ecto.Type.cast(type, [123]) + assert :error = Ecto.Type.cast(type, 123) + + assert {:ok, "no-op"} = Ecto.Type.dump(type, "no-op") + assert {:ok, "no-op"} = Ecto.Type.load(type, "no-op") + end + # TODO check size? test "FixedString(3)" do assert {:parameterized, Ch, {:fixed_string, 3}} =