diff --git a/CHANGELOG.md b/CHANGELOG.md index 703bde9..81aa128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - raise on `validate: true` in `CHECK` constraints https://github.com/plausible/ecto_ch/pull/124 - add support for unsafe hints https://github.com/plausible/ecto_ch/pull/102 +- leave type name validation to ClickHouse ## 0.3.0 (2023-09-13) diff --git a/lib/ecto/adapters/clickhouse/connection.ex b/lib/ecto/adapters/clickhouse/connection.ex index efce6bf..71b4045 100644 --- a/lib/ecto/adapters/clickhouse/connection.ex +++ b/lib/ecto/adapters/clickhouse/connection.ex @@ -704,7 +704,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do end defp expr(%Tagged{value: value, type: type}, sources, params, query) do - ["CAST(", expr(value, sources, params, query), " AS ", ecto_to_db(type, query), ?)] + ["CAST(", expr(value, sources, params, query), " AS ", ecto_to_db(type), ?)] end defp expr(nil, _sources, _params, _query), do: "NULL" @@ -880,20 +880,16 @@ defmodule Ecto.Adapters.ClickHouse.Connection do # when ecto migrator queries for versions in schema_versions it uses type(version, :integer) # so we need :integer to be the same as :bigint which is used for schema_versions table definition # this is why :integer is Int64 and not Int32 - defp ecto_to_db(:integer, _query), do: "Int64" - defp ecto_to_db(:binary, _query), do: "String" - defp ecto_to_db({:parameterized, Ch, type}, _query), do: Ch.Types.encode(type) - defp ecto_to_db({:array, type}, query), do: ["Array(", ecto_to_db(type, query), ?)] + defp ecto_to_db(:integer), do: "Int64" + defp ecto_to_db(:binary), do: "String" + defp ecto_to_db({:parameterized, Ch, type}), do: Ch.Types.encode(type) + defp ecto_to_db({:array, type}), do: ["Array(", ecto_to_db(type), ?)] - defp ecto_to_db(type, _query) when type in [:uuid, :string, :date, :boolean] do + defp ecto_to_db(type) when type in [:uuid, :string, :date, :boolean] do Ch.Types.encode(type) end - defp ecto_to_db(type, query) do - raise Ecto.QueryError, - query: query, - message: "unknown or ambiguous (for ClickHouse) Ecto type #{inspect(type)}" - end + defp ecto_to_db(type), do: String.Chars.Atom.to_string(type) defp param_type_at(params, ix) do value = Enum.at(params, ix) diff --git a/lib/ecto/adapters/clickhouse/migration.ex b/lib/ecto/adapters/clickhouse/migration.ex index 38eb1ba..444b26e 100644 --- a/lib/ecto/adapters/clickhouse/migration.ex +++ b/lib/ecto/adapters/clickhouse/migration.ex @@ -430,35 +430,23 @@ defmodule Ecto.Adapters.ClickHouse.Migration do raise ArgumentError, "type :id is ambiguous, use a literal (e.g. :Int64 or :UInt64) instead" end - defp column_type(:numeric) do - raise ArgumentError, "type :numeric is not supported" - end - - defp column_type(:time) do - raise ArgumentError, "type :time is not supported" - end - defp column_type(:map) do raise ArgumentError, ~s[type :map is ambiguous, use a literal (e.g. :JSON or :"Map(String, UInt8)") instead] end - defp column_type(:decimal) do + defp column_type(type) when type in [:decimal, :numeric] do raise ArgumentError, - ~s[type :decimal is ambiguous, use a literal (e.g. :"Decimal(p, s)") instead] + ~s[type #{inspect(type)} is ambiguous, use a literal (e.g. :"Decimal(p, s)") instead] end defp column_type(:uuid), do: "UUID" defp column_type(:boolean), do: "Bool" - defp column_type(:integer), do: "Int32" - defp column_type(:bigint), do: "Int64" defp column_type(type) when type in [:string, :binary, :binary_id] do "String" end - defp column_type(:float), do: "Float64" - defp column_type({:array, type}) do ["Array(", column_type(type), ?)] end diff --git a/lib/ecto/adapters/clickhouse/schema.ex b/lib/ecto/adapters/clickhouse/schema.ex index bf6de07..51f82cc 100644 --- a/lib/ecto/adapters/clickhouse/schema.ex +++ b/lib/ecto/adapters/clickhouse/schema.ex @@ -278,11 +278,9 @@ defmodule Ecto.Adapters.ClickHouse.Schema do do: {a, remap_type(t, original, schema, field)} defp remap_type(:integer, _original, Ecto.Migration.SchemaMigration, :version), do: :i64 - - defp remap_type(time, _original, _schema, _field) when time in [:time, :time_usec] do - raise ArgumentError, - "`#{inspect(time)}` type is not supported as there is no `Time` type in ClickHouse." - end + defp remap_type(:integer, _original, _schema, _field), do: :i32 + defp remap_type(:float, _original, _schema, _field), do: :f32 + defp remap_type(:time, _original, _schema, _field), do: :i64 defp remap_type(other, original, schema, field) do ch_type = ch_type_hint(original) @@ -305,8 +303,6 @@ defmodule Ecto.Adapters.ClickHouse.Schema do # https://hexdocs.pm/ecto/Ecto.Schema.html#module-primitive-types defp ch_type_hint(:id), do: "Int64" - defp ch_type_hint(:integer), do: "Int64" - defp ch_type_hint(:float), do: "Float32" defp ch_type_hint({:array, type}), do: "{:array, #{ch_type_hint(type)}}" defp ch_type_hint(:map), do: "Map(String, Int64)" defp ch_type_hint({:map, type}), do: "Map(String, #{ch_type_hint(type)})"