Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more types #126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 7 additions & 11 deletions lib/ecto/adapters/clickhouse/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 2 additions & 14 deletions lib/ecto/adapters/clickhouse/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions lib/ecto/adapters/clickhouse/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)})"
Expand Down