From 59052afbd4e5d1a729b871428563501fc789e99d Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Fri, 10 May 2024 13:04:13 +0700 Subject: [PATCH] raise on table select --- lib/ecto/adapters/clickhouse/connection.ex | 28 +++-- .../adapters/clickhouse/connection_test.exs | 113 +++++++++++++----- 2 files changed, 101 insertions(+), 40 deletions(-) diff --git a/lib/ecto/adapters/clickhouse/connection.ex b/lib/ecto/adapters/clickhouse/connection.ex index a113fb5..cd7877f 100644 --- a/lib/ecto/adapters/clickhouse/connection.ex +++ b/lib/ecto/adapters/clickhouse/connection.ex @@ -249,16 +249,22 @@ defmodule Ecto.Adapters.ClickHouse.Connection do defp select_fields(fields, sources, params, query) do intersperse_map(fields, ?,, fn - # TODO raise - # this is useful in array joins lie - # - # "arrays_test" - # |> join(:array, [a], r in "arr") - # |> select([a, r], {a.s, fragment("?", r)}) - # {:&, _, [idx]} -> - {_, source, _} = elem(sources, idx) - source + {_source, name, schema} = elem(sources, idx) + + if is_nil(schema) do + name = IO.iodata_to_binary(name) + + raise Ecto.QueryError, + query: query, + message: + "Ecto.Adapters.ClickHouse requires a schema module when using selector " <> + "#{inspect(name)} but none was given. " <> + "Please specify a schema or specify exactly which fields from " <> + "#{inspect(name)} you desire" + end + + name {k, v} -> [expr(v, sources, params, query), " AS " | quote_name(k)] @@ -601,7 +607,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do raise Ecto.QueryError, query: query, message: - "ClickHouse requires a schema module when using selector " <> + "Ecto.Adapters.ClickHouse requires a schema module when using selector " <> "#{inspect(name)} but none was given. " <> "Please specify a schema or specify exactly which fields from " <> "#{inspect(name)} you desire" @@ -649,7 +655,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do when is_list(kw) or tuple_size(kw) == 3 do raise Ecto.QueryError, query: query, - message: "ClickHouse adapter does not support keyword or interpolated fragments" + message: "Ecto.Adapters.ClickHouse does not support keyword or interpolated fragments" end defp expr({:fragment, _, parts}, sources, params, query) do diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index 8b37779..ff3f278 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -678,7 +678,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do assert all(query) == ~s[SELECT lcase(s0."x", {$0:Int64}) FROM "schema" AS s0] assert_raise Ecto.QueryError, - ~r/ClickHouse adapter does not support keyword or interpolated fragments/, + ~r/Ecto.Adapters.ClickHouse does not support keyword or interpolated fragments/, fn -> Schema |> select([], fragment(title: 2)) @@ -1317,68 +1317,123 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do @tag :capture_log test "lateral (but really array) join" do - query = - "arrays_test" - |> join(:inner_lateral, [a], r in "arr", on: true) - |> select([a, r], {a.s, r}) + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + "arrays_test" + |> join(:inner_lateral, [a], r in "arr", on: true) + |> select([a, r], {a.s, r}) + ) + end - assert all(query) == """ + assert all( + "arrays_test" + |> join(:inner_lateral, [a], r in "arr", on: true) + |> select([a, r], {a.s, fragment("?", r)}) + ) == """ SELECT a0."s",a1 FROM "arrays_test" AS a0 ARRAY JOIN "arr" AS a1\ """ end @tag :capture_log test "left lateral (but really left array) join" do - query = - "arrays_test" - |> join(:left_lateral, [a], r in "arr", on: true) - |> select([a, r], {a.s, r}) + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + "arrays_test" + |> join(:left_lateral, [a], r in "arr", on: true) + |> select([a, r], {a.s, r}) + ) + end - assert all(query) == """ + assert all( + "arrays_test" + |> join(:left_lateral, [a], r in "arr", on: true) + |> select([a, r], {a.s, fragment("?", r)}) + ) == """ SELECT a0."s",a1 FROM "arrays_test" AS a0 LEFT ARRAY JOIN "arr" AS a1\ """ end @tag :capture_log test "array join" do - query = - from at in "arrays_test", - array_join: a in "arr", - select: [at.s, a] + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + from at in "arrays_test", + array_join: a in "arr", + select: [at.s, a] + ) + end - assert all(query) == """ - SELECT a0."s",a1 FROM "arrays_test" AS a0 ARRAY JOIN "arr" AS a1\ - """ + assert all( + from at in "arrays_test", + array_join: a in "arr", + select: [at.s, fragment("?", a)] + ) == ~s{SELECT a0."s",a1 FROM "arrays_test" AS a0 ARRAY JOIN "arr" AS a1} + + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + from at in "arrays_test", + join: a in "arr", + on: true, + hints: "ARRAY", + select: [at.s, a] + ) + end assert all( from at in "arrays_test", join: a in "arr", on: true, hints: "ARRAY", - select: [at.s, a] - ) == - """ - SELECT a0."s",a1 FROM "arrays_test" AS a0 ARRAY JOIN "arr" AS a1\ - """ + select: [at.s, fragment("?", a)] + ) == ~s{SELECT a0."s",a1 FROM "arrays_test" AS a0 ARRAY JOIN "arr" AS a1} end @tag :capture_log test "left array join" do - query = - from at in "arrays_test", - left_array_join: a in "arr", - select: [at.s, a] + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + from at in "arrays_test", + left_array_join: a in "arr", + select: [at.s, a] + ) + end - assert all(query) == """ + assert all( + from at in "arrays_test", + left_array_join: a in "arr", + select: [at.s, fragment("?", a)] + ) == """ SELECT a0."s",a1 FROM "arrays_test" AS a0 LEFT ARRAY JOIN "arr" AS a1\ """ + assert_raise Ecto.QueryError, + ~r/Ecto.Adapters.ClickHouse requires a schema module when using selector "a1" but none was given./, + fn -> + all( + from at in "arrays_test", + left_join: a in "arr", + on: true, + hints: "ARRAY", + select: [at.s, a] + ) + end + assert all( from at in "arrays_test", left_join: a in "arr", on: true, hints: "ARRAY", - select: [at.s, a] + select: [at.s, fragment("?", a)] ) == """ SELECT a0."s",a1 FROM "arrays_test" AS a0 LEFT ARRAY JOIN "arr" AS a1\