From 78047bace0e38e8a92af237fa7db812c16478f03 Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Tue, 26 Aug 2025 16:01:45 +0900 Subject: [PATCH 1/4] todos --- lib/ecto/adapters/clickhouse/migration.ex | 1 + lib/ecto/adapters/clickhouse/schema.ex | 1 + test/ecto/adapters/clickhouse/connection_test.exs | 2 ++ 3 files changed, 4 insertions(+) diff --git a/lib/ecto/adapters/clickhouse/migration.ex b/lib/ecto/adapters/clickhouse/migration.ex index 38eb1bad..a33c8e22 100644 --- a/lib/ecto/adapters/clickhouse/migration.ex +++ b/lib/ecto/adapters/clickhouse/migration.ex @@ -434,6 +434,7 @@ defmodule Ecto.Adapters.ClickHouse.Migration do raise ArgumentError, "type :numeric is not supported" end + # TODO defp column_type(:time) do raise ArgumentError, "type :time is not supported" end diff --git a/lib/ecto/adapters/clickhouse/schema.ex b/lib/ecto/adapters/clickhouse/schema.ex index af6217a0..53ce857b 100644 --- a/lib/ecto/adapters/clickhouse/schema.ex +++ b/lib/ecto/adapters/clickhouse/schema.ex @@ -291,6 +291,7 @@ defmodule Ecto.Adapters.ClickHouse.Schema do defp remap_type(:integer, _original, Ecto.Migration.SchemaMigration, :version), do: :i64 + # TODO 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." diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index b23bca67..1da55464 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -962,6 +962,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do assert all(query) == ~s[SELECT CAST(s0."x" + 1 AS UInt16) FROM "schema" AS s0] end + # TODO test "tagged unknown type" do query = from e in "events", select: type(e.count + 1, :time) @@ -2621,6 +2622,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do ] end + # TODO test "create table with time columns" do create = {:create, table(:posts), From 3218c362e860343c8ff7387970897e5e9ba5823e Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Tue, 26 Aug 2025 16:25:20 +0900 Subject: [PATCH 2/4] continue --- lib/ecto/adapters/clickhouse/migration.ex | 5 ----- lib/ecto/adapters/clickhouse/schema.ex | 10 +++------- test/ecto/adapters/clickhouse/connection_test.exs | 7 +++---- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/lib/ecto/adapters/clickhouse/migration.ex b/lib/ecto/adapters/clickhouse/migration.ex index a33c8e22..b1c643c8 100644 --- a/lib/ecto/adapters/clickhouse/migration.ex +++ b/lib/ecto/adapters/clickhouse/migration.ex @@ -434,11 +434,6 @@ defmodule Ecto.Adapters.ClickHouse.Migration do raise ArgumentError, "type :numeric is not supported" end - # TODO - 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] diff --git a/lib/ecto/adapters/clickhouse/schema.ex b/lib/ecto/adapters/clickhouse/schema.ex index 53ce857b..43e54f3e 100644 --- a/lib/ecto/adapters/clickhouse/schema.ex +++ b/lib/ecto/adapters/clickhouse/schema.ex @@ -264,7 +264,7 @@ defmodule Ecto.Adapters.ClickHouse.Schema do defp remap_type({:parameterized, {Ch, t}}, _original, _schema, _field), do: t defp remap_type(t, _original, _schema, _field) - when t in [:string, :date, :uuid, :boolean], + when t in [:string, :date, :time, :uuid, :boolean], do: t defp remap_type(dt, _original, _schema, _field) @@ -275,6 +275,8 @@ defmodule Ecto.Adapters.ClickHouse.Schema do when usec in [:naive_datetime_usec, :utc_datetime_usec], do: {:datetime64, _precision = 6} + defp remap_type(:time_usec, _original, _schema, _field), do: {:time64, _precision = 6} + # TODO remove defp remap_type(t, _original, _schema, _field) when t in [:binary, :binary_id], @@ -291,12 +293,6 @@ defmodule Ecto.Adapters.ClickHouse.Schema do defp remap_type(:integer, _original, Ecto.Migration.SchemaMigration, :version), do: :i64 - # TODO - 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(other, original, schema, field) do ch_type = ch_type_hint(original) diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index 1da55464..307357f7 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -2622,15 +2622,14 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do ] end - # TODO test "create table with time columns" do create = {:create, table(:posts), [{:add, :published_at, :time, []}, {:add, :submitted_at, :time, []}]} - assert_raise ArgumentError, "type :time is not supported", fn -> - execute_ddl(create) - end + assert execute_ddl(create) == [ + ~s[CREATE TABLE "posts" ("published_at" time,"submitted_at" time) ENGINE=TinyLog] + ] end test "create table with utc_datetime columns" do From 6c56bdb32c912fce980e1d8e5bfdebc9d6968ae8 Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Tue, 26 Aug 2025 16:33:20 +0900 Subject: [PATCH 3/4] continue --- lib/ecto/adapters/clickhouse/connection.ex | 2 +- test/ecto/adapters/clickhouse/connection_test.exs | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/ecto/adapters/clickhouse/connection.ex b/lib/ecto/adapters/clickhouse/connection.ex index 35e7f9e3..fbb27f50 100644 --- a/lib/ecto/adapters/clickhouse/connection.ex +++ b/lib/ecto/adapters/clickhouse/connection.ex @@ -1101,7 +1101,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do 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(type, _query) when type in [:uuid, :string, :date, :boolean] do + defp ecto_to_db(type, _query) when type in [:uuid, :string, :date, :time, :boolean] do Ch.Types.encode(type) end diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index 307357f7..941ad6f2 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -960,16 +960,12 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do test "tagged column type" do query = from s in Schema, select: type(s.x + 1, s.y) assert all(query) == ~s[SELECT CAST(s0."x" + 1 AS UInt16) FROM "schema" AS s0] - end - # TODO - test "tagged unknown type" do query = from e in "events", select: type(e.count + 1, :time) + assert all(query) == nil + end - assert_raise Ecto.QueryError, - ~r/unknown or ambiguous \(for ClickHouse\) Ecto type :time in query/, - fn -> all(query) end - + test "tagged unknown type" do query = from e in "events", select: type(e.count + 1, :decimal) assert_raise Ecto.QueryError, From a21de36ca2bf49969c84cd7ee28f1388535f3f8c Mon Sep 17 00:00:00 2001 From: ruslandoga Date: Tue, 26 Aug 2025 16:34:06 +0900 Subject: [PATCH 4/4] tag time test --- test/ecto/adapters/clickhouse/connection_test.exs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index 941ad6f2..abbe86d7 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -960,9 +960,12 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do test "tagged column type" do query = from s in Schema, select: type(s.x + 1, s.y) assert all(query) == ~s[SELECT CAST(s0."x" + 1 AS UInt16) FROM "schema" AS s0] + end + @tag :time + test "tagged as time" do query = from e in "events", select: type(e.count + 1, :time) - assert all(query) == nil + assert all(query) == ~s[SELECT CAST(e0."count" + CAST(1 AS Time) AS Time) FROM "events" AS e0] end test "tagged unknown type" do