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

Raise on table select #162

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
28 changes: 17 additions & 11 deletions lib/ecto/adapters/clickhouse/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
113 changes: 84 additions & 29 deletions test/ecto/adapters/clickhouse/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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\
Expand Down
Loading