From ae5723bcb9660f08e7c398ef4f6ff9471619122e Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Thu, 31 Oct 2024 19:30:22 +0700 Subject: [PATCH 1/2] fewer parens --- CHANGELOG.md | 4 ++ lib/ecto/adapters/clickhouse/connection.ex | 54 ++++++++++-------- .../adapters/clickhouse/connection_test.exs | 57 +++++++++++++++++++ 3 files changed, 90 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19332e9..88a085f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- remove unnecessary parens from generated SQL to avoid hitting TOO_DEEP_RECURSION https://github.com/plausible/ecto_ch/pull/206 + ## 0.4.0 (2024-10-15) - use `UNION DISTINCT` instead of `UNION` for `Ecto.Query.union/2` https://github.com/plausible/ecto_ch/pull/204 diff --git a/lib/ecto/adapters/clickhouse/connection.ex b/lib/ecto/adapters/clickhouse/connection.ex index 6d6a0e7..ec43653 100644 --- a/lib/ecto/adapters/clickhouse/connection.ex +++ b/lib/ecto/adapters/clickhouse/connection.ex @@ -307,8 +307,6 @@ defmodule Ecto.Adapters.ClickHouse.Connection do is_nil: " WHERE " ] - @binary_ops Keyword.keys(binary_ops) - for {op, str} <- binary_ops do defp handle_call(unquote(op), 2), do: {:binary_op, unquote(str)} end @@ -594,22 +592,21 @@ defmodule Ecto.Adapters.ClickHouse.Connection do defp boolean(_name, [], _sources, _params, _query), do: [] - defp boolean(name, [%{expr: expr, op: op} | exprs], sources, params, query) do + defp boolean(name, exprs, sources, params, query) do + [%BooleanExpr{expr: first_expr} | other_exprs] = exprs + result = - Enum.reduce(exprs, {op, paren_expr(expr, sources, params, query)}, fn - %BooleanExpr{expr: expr, op: op}, {op, acc} -> - {op, [acc, operator_to_boolean(op) | paren_expr(expr, sources, params, query)]} + Enum.reduce(other_exprs, expr(first_expr, sources, params, query), fn + %BooleanExpr{op: :or, expr: expr}, acc -> + ["((", acc, ?), " OR ", ?(, expr(expr, sources, params, query), "))"] - %BooleanExpr{expr: expr, op: op}, {_, acc} -> - {op, [?(, acc, ?), operator_to_boolean(op) | paren_expr(expr, sources, params, query)]} + %BooleanExpr{op: :and, expr: expr}, acc -> + [acc, " AND ", expr(expr, sources, params, query)] end) - [name | elem(result, 1)] + [name | result] end - defp operator_to_boolean(:and), do: " AND " - defp operator_to_boolean(:or), do: " OR " - defp parens_for_select([first_expr | _] = expression) do if is_binary(first_expr) and String.match?(first_expr, ~r/^\s*select/i) do [?(, expression, ?)] @@ -618,10 +615,6 @@ defmodule Ecto.Adapters.ClickHouse.Connection do end end - defp paren_expr(expr, sources, params, query) do - [?(, expr(expr, sources, params, query), ?)] - end - defp expr({_type, [literal]}, sources, params, query) do expr(literal, sources, params, query) end @@ -782,16 +775,31 @@ defmodule Ecto.Adapters.ClickHouse.Connection do _ -> {[], args} end - case handle_call(fun, length(args)) do - {:binary_op, op} -> + # TODO don't length(args) + case {fun, handle_call(fun, length(args))} do + {:or, {:binary_op, op}} -> [left, right] = args [ + "((", op_to_binary(left, sources, params, query), - op | op_to_binary(right, sources, params, query) + ?), + op, + ?(, + op_to_binary(right, sources, params, query), + "))" ] - {:fun, fun} -> + {_, {:binary_op, op}} -> + [left, right] = args + + [ + op_to_binary(left, sources, params, query), + op, + op_to_binary(right, sources, params, query) + ] + + {_, {:fun, fun}} -> [fun, ?(, modifier, intersperse_map(args, ?,, &expr(&1, sources, params, query)), ?)] end end @@ -835,12 +843,8 @@ defmodule Ecto.Adapters.ClickHouse.Connection do message: "unsupported expression #{inspect(expr)}" end - defp op_to_binary({op, _, [_, _]} = expr, sources, params, query) when op in @binary_ops do - paren_expr(expr, sources, params, query) - end - defp op_to_binary({:is_nil, _, [_]} = expr, sources, params, query) do - paren_expr(expr, sources, params, query) + [?(, expr(expr, sources, params, query), ?)] end defp op_to_binary(expr, sources, params, query) do diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index ddf43bf..6ed5c80 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -410,6 +410,63 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x" = 42) OR (s0."y" != 43)) AND (s0."z" = 44)] end + defmacrop has_key(table, meta_column, key) do + quote do + fragment( + "has(?, ?)", + field(unquote(table), unquote(meta_column)), + unquote(key) + ) + end + end + + defmacrop get_by_key(table, meta_column, key) do + quote do + fragment( + "?[indexOf(?, ?)]", + field(unquote(table), unquote(meta_column)), + field(unquote(table), unquote(meta_column)), + unquote(key) + ) + end + end + + test "dynamic with many conditions" do + filters = [ + dynamic( + [t], + (has_key(t, :"meta.key", ^"content-platform") and + get_by_key(t, :"meta.key", ^"content-platform") in ^["(none)", "web", "app"]) or + (^true and not has_key(t, :"meta.key", ^"content-platform")) + ) + ] + + base_condition = + dynamic( + [e], + e.site_id == ^1 and e.timestamp >= ^~D[2024-01-01] and e.timestamp <= ^~D[2025-01-01] + ) + + dynamic = + Enum.reduce(filters, base_condition, fn condition, acc -> + dynamic([], ^acc and ^condition) + end) + + assert all(from e in "events", where: ^dynamic, select: e.name) == + """ + SELECT e0."name" FROM "events" AS e0 WHERE \ + e0."site_id" = {$0:Int64} \ + AND e0."timestamp" >= {$1:Date} \ + AND e0."timestamp" <= {$2:Date} \ + AND \ + (\ + (has(e0."meta.key", {$3:String}) AND e0."meta.key"[indexOf(e0."meta.key", {$4:String})] IN ({$5:String},{$6:String},{$7:String})) \ + OR \ + ({$8:Bool} AND NOT (has(e0."meta.key", {$9:String})))\ + )\ + """ + end + test "order_by" do query = Schema |> order_by([r], r.x) |> select([r], r.x) assert all(query) == ~s[SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x"] From f59ad1736fb5553de864e08b49cde46eb6eb6963 Mon Sep 17 00:00:00 2001 From: ruslandoga <67764432+ruslandoga@users.noreply.github.com> Date: Thu, 31 Oct 2024 21:40:59 +0700 Subject: [PATCH 2/2] fewER parens --- lib/ecto/adapters/clickhouse.ex | 4 +- lib/ecto/adapters/clickhouse/connection.ex | 23 +--- .../adapters/clickhouse/connection_test.exs | 119 +++++++++--------- .../clickhouse_alter_update_test.exs | 32 ++--- test/ecto/integration/inline_test.exs | 15 +-- test/ecto/integration/union_test.exs | 8 +- test/ecto_ch_test.exs | 21 ++-- 7 files changed, 103 insertions(+), 119 deletions(-) diff --git a/lib/ecto/adapters/clickhouse.ex b/lib/ecto/adapters/clickhouse.ex index 59bb112..663cf06 100644 --- a/lib/ecto/adapters/clickhouse.ex +++ b/lib/ecto/adapters/clickhouse.ex @@ -365,13 +365,13 @@ defmodule Ecto.Adapters.ClickHouse do iex> query = from n in fragment("numbers(10)"), where: n.number == ^-1, select: n.number iex> Ecto.Adapters.ClickHouse.to_inline_sql(:all, query) - ~s{SELECT f0."number" FROM numbers(10) AS f0 WHERE (f0."number" = -1)} + ~s{SELECT f0."number" FROM numbers(10) AS f0 WHERE f0."number" = -1} Compare it with the output of `to_sql/2` iex> query = from n in fragment("numbers(10)"), where: n.number == ^-1, select: n.number iex> Ecto.Adapters.ClickHouse.to_sql(:all, query) - {~s[SELECT f0."number" FROM numbers(10) AS f0 WHERE (f0."number" = {$0:Int64})], [-1]} + {~s[SELECT f0."number" FROM numbers(10) AS f0 WHERE f0."number" = {$0:Int64}], [-1]} """ @spec to_inline_sql(:all | :delete_all | :update_all, Ecto.Queryable.t()) :: String.t() diff --git a/lib/ecto/adapters/clickhouse/connection.ex b/lib/ecto/adapters/clickhouse/connection.ex index ec43653..28b396d 100644 --- a/lib/ecto/adapters/clickhouse/connection.ex +++ b/lib/ecto/adapters/clickhouse/connection.ex @@ -301,10 +301,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do and: " AND ", or: " OR ", ilike: " ILIKE ", - like: " LIKE ", - # TODO these two are not in binary_ops in sqlite3 adapter - in: " IN ", - is_nil: " WHERE " + like: " LIKE " ] for {op, str} <- binary_ops do @@ -676,7 +673,7 @@ defmodule Ecto.Adapters.ClickHouse.Connection do end defp expr({:is_nil, _, [arg]}, sources, params, query) do - [expr(arg, sources, params, query) | " IS NULL"] + [?(, expr(arg, sources, params, query) | " IS NULL)"] end defp expr({:not, _, [expr]}, sources, params, query) do @@ -782,11 +779,11 @@ defmodule Ecto.Adapters.ClickHouse.Connection do [ "((", - op_to_binary(left, sources, params, query), + expr(left, sources, params, query), ?), op, ?(, - op_to_binary(right, sources, params, query), + expr(right, sources, params, query), "))" ] @@ -794,9 +791,9 @@ defmodule Ecto.Adapters.ClickHouse.Connection do [left, right] = args [ - op_to_binary(left, sources, params, query), + expr(left, sources, params, query), op, - op_to_binary(right, sources, params, query) + expr(right, sources, params, query) ] {_, {:fun, fun}} -> @@ -843,14 +840,6 @@ defmodule Ecto.Adapters.ClickHouse.Connection do message: "unsupported expression #{inspect(expr)}" end - defp op_to_binary({:is_nil, _, [_]} = expr, sources, params, query) do - [?(, expr(expr, sources, params, query), ?)] - end - - defp op_to_binary(expr, sources, params, query) do - expr(expr, sources, params, query) - end - defp create_names(%{sources: sources}, as_prefix) do sources |> create_names(0, tuple_size(sources), as_prefix) |> List.to_tuple() end diff --git a/test/ecto/adapters/clickhouse/connection_test.exs b/test/ecto/adapters/clickhouse/connection_test.exs index 6ed5c80..09839ed 100644 --- a/test/ecto/adapters/clickhouse/connection_test.exs +++ b/test/ecto/adapters/clickhouse/connection_test.exs @@ -347,7 +347,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do query = Schema |> select([r], count(r.x) |> filter(r.x > 10 and r.x < 50)) assert all(query) == - ~s[SELECT count(s0."x") FILTER (WHERE (s0."x" > 10) AND (s0."x" < 50)) FROM "schema" AS s0] + ~s[SELECT count(s0."x") FILTER (WHERE s0."x" > 10 AND s0."x" < 50) FROM "schema" AS s0] query = Schema |> select([r], count() |> filter(r.x > 10)) assert all(query) == ~s[SELECT count(*) FILTER (WHERE s0."x" > 10) FROM "schema" AS s0] @@ -383,10 +383,10 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> select([r], r.x) assert all(query) == - ~s[SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x" = 42) AND (s0."y" != 43)] + ~s[SELECT s0."x" FROM "schema" AS s0 WHERE s0."x" = 42 AND s0."y" != 43] query = Schema |> where([r], {r.x, r.y} > {1, 2}) |> select([r], r.x) - assert all(query) == ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x",s0."y") > (1,2))] + assert all(query) == ~s[SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x",s0."y") > (1,2)] end test "or_where" do @@ -396,8 +396,9 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> or_where([r], r.y != 43) |> select([r], r.x) + # TODO assert all(query) == - ~s[SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x" = 42) OR (s0."y" != 43)] + ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x" = 42) OR (s0."y" != 43))] query = Schema @@ -407,7 +408,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> select([r], r.x) assert all(query) == - ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x" = 42) OR (s0."y" != 43)) AND (s0."z" = 44)] + ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x" = 42) OR (s0."y" != 43)) AND s0."z" = 44] end defmacrop has_key(table, meta_column, key) do @@ -658,10 +659,10 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do test "string escape" do query = "schema" |> where(foo: "'\\ ") |> select([], true) - assert all(query) == ~s[SELECT true FROM "schema" AS s0 WHERE (s0."foo" = '''\\\\ ')] + assert all(query) == ~s[SELECT true FROM "schema" AS s0 WHERE s0."foo" = '''\\\\ '] query = "schema" |> where(foo: "'") |> select([], true) - assert all(query) == ~s[SELECT true FROM "schema" AS s0 WHERE (s0."foo" = '''')] + assert all(query) == ~s[SELECT true FROM "schema" AS s0 WHERE s0."foo" = ''''] end test "binary ops" do @@ -698,10 +699,10 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do test "is_nil" do query = Schema |> select([r], is_nil(r.x)) - assert all(query) == ~s[SELECT s0."x" IS NULL FROM "schema" AS s0] + assert all(query) == ~s[SELECT (s0."x" IS NULL) FROM "schema" AS s0] query = Schema |> select([r], not is_nil(r.x)) - assert all(query) == ~s[SELECT NOT (s0."x" IS NULL) FROM "schema" AS s0] + assert all(query) == ~s[SELECT NOT ((s0."x" IS NULL)) FROM "schema" AS s0] query = "schema" |> select([r], r.x == is_nil(r.y)) assert all(query) == ~s[SELECT s0."x" = (s0."y" IS NULL) FROM "schema" AS s0] @@ -733,7 +734,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> select([r], r.x) |> where([], fragment(~s|? = "query\\?"|, ^10)) - assert all(query) == ~s[SELECT s0."x" FROM "schema" AS s0 WHERE ({$0:Int64} = "query?")] + assert all(query) == ~s[SELECT s0."x" FROM "schema" AS s0 WHERE {$0:Int64} = "query?"] value = 13 query = Schema |> select([r], fragment("lcase(?, ?)", r.x, ^value)) @@ -751,21 +752,19 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do test "literals" do query = "schema" |> where(foo: true) |> select([], true) # TODO is true? - assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE (s0."foo" = 1)} + assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE s0."foo" = 1} query = "schema" |> where(foo: false) |> select([], true) - assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE (s0."foo" = 0)} + assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE s0."foo" = 0} query = "schema" |> where(foo: "abc") |> select([], true) - assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE (s0."foo" = 'abc')} + assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE s0."foo" = 'abc'} query = "schema" |> where(foo: 123) |> select([], true) - assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE (s0."foo" = 123)} + assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE s0."foo" = 123} query = "schema" |> where(foo: 123.0) |> select([], true) - - assert all(query) == - ~s{SELECT true FROM "schema" AS s0 WHERE (s0."foo" = 123.0)} + assert all(query) == ~s{SELECT true FROM "schema" AS s0 WHERE s0."foo" = 123.0} name = "y" @@ -774,7 +773,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> where(fragment("? = ?", literal(^name), "Main")) |> select([], true) - assert all(query) == ~s|SELECT true FROM "schema" AS s0 WHERE ("y" = 'Main')| + assert all(query) == ~s|SELECT true FROM "schema" AS s0 WHERE "y" = 'Main'| end test "selected_as" do @@ -846,7 +845,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> select([r], (r.x > 0 and r.y > ^(-z)) or true) assert all(query) == - ~s[SELECT ((s0."x" > 0) AND (s0."y" > {$0:Int64})) OR 1 FROM "schema" AS s0] + ~s[SELECT ((s0."x" > 0 AND s0."y" > {$0:Int64}) OR (1)) FROM "schema" AS s0] end test "in expression" do @@ -865,7 +864,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do query = Schema |> select([e], e.x == ^0 or e.x in ^[1, 2, 3] or e.x == ^4) assert all(query) == - ~s[SELECT ((s0."x" = {$0:Int64}) OR (s0."x" IN ({$1:Int64},{$2:Int64},{$3:Int64}))) OR (s0."x" = {$4:Int64}) FROM "schema" AS s0] + ~s[SELECT ((((s0."x" = {$0:Int64}) OR (s0."x" IN ({$1:Int64},{$2:Int64},{$3:Int64})))) OR (s0."x" = {$4:Int64})) FROM "schema" AS s0] query = Schema |> select([e], e in [1, 2, 3]) @@ -888,7 +887,9 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do assert all(query) == """ SELECT c0."x" FROM "comments" AS c0 \ - WHERE (c0."post_id" IN (SELECT sp0."id" FROM "posts" AS sp0 WHERE (sp0."title" = {$0:String})))\ + WHERE c0."post_id" IN (\ + SELECT sp0."id" FROM "posts" AS sp0 WHERE sp0."title" = {$0:String}\ + )\ """ posts = @@ -906,7 +907,9 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do assert all(query) == """ SELECT c0."x" FROM "comments" AS c0 \ - WHERE (c0."post_id" IN (SELECT sp0."id" FROM "posts" AS sp0 WHERE (sp0."title" = c0."subtitle")))\ + WHERE c0."post_id" IN (\ + SELECT sp0."id" FROM "posts" AS sp0 WHERE sp0."title" = c0."subtitle"\ + )\ """ end @@ -916,7 +919,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> having([p], p.x == p.x) |> select([p], p.x) - assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 HAVING (s0."x" = s0."x")} + assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 HAVING s0."x" = s0."x"} query = Schema @@ -928,8 +931,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do """ SELECT s0."y",s0."x" \ FROM "schema" AS s0 \ - HAVING (s0."x" = s0."x") \ - AND (s0."y" = s0."y")\ + HAVING s0."x" = s0."x" AND s0."y" = s0."y"\ """ end @@ -939,7 +941,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do |> or_having([p], p.x == p.x) |> select([p], p.x) - assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 HAVING (s0."x" = s0."x")} + assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 HAVING s0."x" = s0."x"} query = Schema @@ -951,8 +953,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do """ SELECT s0."y",s0."x" \ FROM "schema" AS s0 \ - HAVING (s0."x" = s0."x") \ - OR (s0."y" = s0."y")\ + HAVING ((s0."x" = s0."x") OR (s0."y" = s0."y"))\ """ end @@ -1026,7 +1027,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do WITH \ "cte1" AS (\ SELECT ss0."id" AS "id",{$0:Bool} AS "smth" FROM "schema1" AS ss0 \ - WHERE ({$1:Int64})\ + WHERE {$1:Int64}\ ),\ "cte2" AS (\ SELECT * FROM schema WHERE {$2:Int64}\ @@ -1034,18 +1035,16 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do SELECT s0."id",{$3:Int64} FROM "schema" AS s0 \ INNER JOIN "schema2" AS s1 ON {$4:Bool} \ INNER JOIN "schema2" AS s2 ON {$5:Bool} \ - WHERE ({$6:Bool}) AND ({$7:Bool}) \ + WHERE {$6:Bool} AND {$7:Bool} \ GROUP BY {$8:Int64},{$9:Int64} \ - HAVING ({$10:Bool}) AND ({$11:Bool}) \ + HAVING {$10:Bool} AND {$11:Bool} \ ORDER BY {$16:Int64} \ LIMIT {$17:Int64} \ OFFSET {$18:Int64} \ UNION DISTINCT \ - (SELECT s0."id",{$12:Bool} FROM "schema1" AS s0 \ - WHERE ({$13:Int64})) \ + (SELECT s0."id",{$12:Bool} FROM "schema1" AS s0 WHERE {$13:Int64}) \ UNION ALL \ - (SELECT s0."id",{$14:Bool} FROM "schema2" AS s0 \ - WHERE ({$15:Int64}))\ + (SELECT s0."id",{$14:Bool} FROM "schema2" AS s0 WHERE {$15:Int64})\ """ end @@ -1057,7 +1056,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do select: true ) - assert all(query) == ~s|SELECT true FROM "schema" AS s0 WHERE (s0."start_time" = "query?")| + assert all(query) == ~s|SELECT true FROM "schema" AS s0 WHERE s0."start_time" = "query?"| end test "update_all" do @@ -1181,7 +1180,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do """ assert alter_update_all(from e in Schema, where: e.x == 123, update: [set: [x: 0]]) == """ - ALTER TABLE "schema" UPDATE "x"=0 WHERE ("x" = 123)\ + ALTER TABLE "schema" UPDATE "x"=0 WHERE "x" = 123\ """ assert alter_update_all(from m in Schema, update: [set: [x: ^0]]) == """ @@ -1274,10 +1273,10 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do assert delete_all(Schema) == ~s{DELETE FROM "schema" WHERE 1} query = from(e in Schema, where: e.x == 123) - assert delete_all(query) == ~s{DELETE FROM "schema" WHERE ("x" = 123)} + assert delete_all(query) == ~s{DELETE FROM "schema" WHERE "x" = 123} query = from(e in Schema, where: e.x == ^123) - assert delete_all(query) == ~s[DELETE FROM "schema" WHERE ("x" = {$0:Int64})] + assert delete_all(query) == ~s[DELETE FROM "schema" WHERE "x" = {$0:Int64}] query = from(e in Schema, where: e.x == 123, select: e.x) @@ -1593,7 +1592,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do INNER JOIN (\ SELECT sp0."x" AS "x",sp0."y" AS "y" \ FROM "posts" AS sp0 \ - WHERE (sp0."title" = {$0:String})\ + WHERE sp0."title" = {$0:String}\ ) AS s1 ON 1\ """ @@ -1614,7 +1613,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do INNER JOIN (\ SELECT sp0."x" AS "x",sp0."y" AS "z" \ FROM "posts" AS sp0 \ - WHERE (sp0."title" = {$0:String})\ + WHERE sp0."title" = {$0:String}\ ) AS s1 ON 1\ """ @@ -1637,7 +1636,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do INNER JOIN (\ SELECT sp0."title" AS "title" \ FROM "posts" AS sp0 \ - WHERE (sp0."title" = c0."subtitle")\ + WHERE sp0."title" = c0."subtitle"\ ) AS s1 ON 1\ """ end @@ -1699,7 +1698,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do FROM schema2 AS s2 \ WHERE s2.id = s0."x" AND s2.field = {$1:Int64}\ ) AS f1 ON 1 \ - WHERE ((s0."id" > 0) AND (s0."id" < {$2:Int64}))\ + WHERE s0."id" > 0 AND s0."id" < {$2:Int64}\ """ end @@ -1806,7 +1805,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do SELECT {$0:Int64} \ FROM "schema" AS s0 \ INNER JOIN (SELECT {$1:Int64} AS "x",{$2:Int64} AS "w" FROM "schema" AS ss0) AS s1 ON 1 \ - WHERE (s0."x" = {$3:Int64})\ + WHERE s0."x" = {$3:Int64}\ """ end end @@ -1886,7 +1885,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do SELECT m0."name" \ FROM "movies" AS m0 \ LEFT JOIN "genres" AS g1 ON m0."id" = g1."movie_id" \ - WHERE (g1."movie_id" = 0) \ + WHERE g1."movie_id" = 0 \ ORDER BY m0."year" DESC,m0."name" \ LIMIT 10\ """ @@ -1922,7 +1921,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do SELECT a0."first_name",a0."last_name" \ FROM "actors" AS a0 \ SEMI LEFT JOIN "roles" AS r1 ON a0."id" = r1."actor_id" \ - WHERE (toYear(created_at) = '2023') \ + WHERE toYear(created_at) = '2023' \ ORDER BY a0."id" \ LIMIT 10\ """ @@ -2027,7 +2026,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do q1."price" AS "quote_price",\ t0."volume" * q1."price" AS "final_price" \ FROM "trades" AS t0 \ - ASOF LEFT JOIN "quotes" AS q1 ON (t0."symbol" = q1."symbol") AND (t0."time" >= q1."time")\ + ASOF LEFT JOIN "quotes" AS q1 ON t0."symbol" = q1."symbol" AND t0."time" >= q1."time"\ """ end @@ -2126,7 +2125,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do insert = insert(nil, "schema", [:foo, :bar], select, {:raise, [], []}, []) assert insert == - ~s{INSERT INTO "schema"("foo","bar") SELECT 3,s0."bar" FROM "schema" AS s0 WHERE (1)} + ~s{INSERT INTO "schema"("foo","bar") SELECT 3,s0."bar" FROM "schema" AS s0 WHERE 1} select = (s in "schema") @@ -3068,7 +3067,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.3Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(1)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(1)}\ """ assert all( @@ -3076,7 +3075,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.38Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(2)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(2)}\ """ assert all( @@ -3084,7 +3083,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.382Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(3)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(3)}\ """ assert all( @@ -3092,7 +3091,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.3827Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(4)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(4)}\ """ assert all( @@ -3100,7 +3099,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.38278Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(5)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(5)}\ """ assert all( @@ -3108,7 +3107,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~U[2024-06-28 20:02:17.382780Z], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(6)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(6)}\ """ end @@ -3119,7 +3118,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.3], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(1)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(1)}\ """ assert all( @@ -3127,7 +3126,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.38], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(2)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(2)}\ """ assert all( @@ -3135,7 +3134,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.382], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(3)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(3)}\ """ assert all( @@ -3143,7 +3142,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.3827], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(4)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(4)}\ """ assert all( @@ -3151,7 +3150,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.38278], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(5)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(5)}\ """ assert all( @@ -3159,7 +3158,7 @@ defmodule Ecto.Adapters.ClickHouse.ConnectionTest do where: e.timestamp > ^~N[2024-06-28 20:02:17.382780], select: e.timestamp ) == """ - SELECT e0."timestamp" FROM "events" AS e0 WHERE (e0."timestamp" > {$0:DateTime64(6)})\ + SELECT e0."timestamp" FROM "events" AS e0 WHERE e0."timestamp" > {$0:DateTime64(6)}\ """ end diff --git a/test/ecto/integration/clickhouse_alter_update_test.exs b/test/ecto/integration/clickhouse_alter_update_test.exs index 45eef9c..d72e7c0 100644 --- a/test/ecto/integration/clickhouse_alter_update_test.exs +++ b/test/ecto/integration/clickhouse_alter_update_test.exs @@ -20,14 +20,14 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do "events" |> where(name: "hello") |> update(set: [i: 1]) - ) == {~s[ALTER TABLE "events" UPDATE "i"=1 WHERE ("name" = 'hello')], []} + ) == {~s[ALTER TABLE "events" UPDATE "i"=1 WHERE "name" = 'hello'], []} assert to_sql( "events" |> where(name: "hello") |> update(set: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"={$0:Int64} WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"={$0:Int64} WHERE "name" = 'hello'], [1] } @@ -36,7 +36,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(set: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"={$0:Int64} WHERE ("name" = {$1:String})], + ~s[ALTER TABLE "events" UPDATE "i"={$0:Int64} WHERE "name" = {$1:String}], [1, "hello"] } @@ -45,7 +45,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(set: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=1 WHERE ("name" = {$0:String})], + ~s[ALTER TABLE "events" UPDATE "i"=1 WHERE "name" = {$0:String}], ["hello"] } end @@ -56,7 +56,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: "hello") |> update(inc: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"="i"+1 WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"="i"+1 WHERE "name" = 'hello'], [] } @@ -65,21 +65,21 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(inc: [i: ^1]) ) == - {~s[ALTER TABLE "events" UPDATE "i"="i"+{$0:Int64} WHERE ("name" = {$1:String})], + {~s[ALTER TABLE "events" UPDATE "i"="i"+{$0:Int64} WHERE "name" = {$1:String}], [1, "hello"]} assert to_sql( "events" |> where(name: "hello") |> update(inc: [i: -1]) - ) == {~s[ALTER TABLE "events" UPDATE "i"="i"+-1 WHERE ("name" = 'hello')], []} + ) == {~s[ALTER TABLE "events" UPDATE "i"="i"+-1 WHERE "name" = 'hello'], []} assert to_sql( "events" |> where(name: ^"hello") |> update(inc: [i: ^(-1)]) ) == - {~s[ALTER TABLE "events" UPDATE "i"="i"+{$0:Int64} WHERE ("name" = {$1:String})], + {~s[ALTER TABLE "events" UPDATE "i"="i"+{$0:Int64} WHERE "name" = {$1:String}], [-1, "hello"]} end @@ -89,7 +89,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: "hello") |> update(push: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",1) WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",1) WHERE "name" = 'hello'], [] } @@ -98,7 +98,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: "hello") |> update(push: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",{$0:Int64}) WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",{$0:Int64}) WHERE "name" = 'hello'], [1] } @@ -107,7 +107,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(push: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",{$0:Int64}) WHERE ("name" = {$1:String})], + ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",{$0:Int64}) WHERE "name" = {$1:String}], [1, "hello"] } @@ -116,7 +116,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(push: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",1) WHERE ("name" = {$0:String})], + ~s[ALTER TABLE "events" UPDATE "i"=arrayPushBack("i",1) WHERE "name" = {$0:String}], ["hello"] } end @@ -127,7 +127,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: "hello") |> update(pull: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!=1,"i") WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!=1,"i") WHERE "name" = 'hello'], [] } @@ -136,7 +136,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: "hello") |> update(pull: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!={$0:Int64},"i") WHERE ("name" = 'hello')], + ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!={$0:Int64},"i") WHERE "name" = 'hello'], [1] } @@ -145,7 +145,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(pull: [i: ^1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!={$0:Int64},"i") WHERE ("name" = {$1:String})], + ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!={$0:Int64},"i") WHERE "name" = {$1:String}], [1, "hello"] } @@ -154,7 +154,7 @@ defmodule Ecto.Integration.ClickHouseAlterUpdateTest do |> where(name: ^"hello") |> update(pull: [i: 1]) ) == { - ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!=1,"i") WHERE ("name" = {$0:String})], + ~s[ALTER TABLE "events" UPDATE "i"=arrayFilter(x->x!=1,"i") WHERE "name" = {$0:String}], ["hello"] } end diff --git a/test/ecto/integration/inline_test.exs b/test/ecto/integration/inline_test.exs index bf0774f..abd572f 100644 --- a/test/ecto/integration/inline_test.exs +++ b/test/ecto/integration/inline_test.exs @@ -53,8 +53,7 @@ defmodule Ecto.Integration.InlineSQLTest do """ WITH \ "cte1" AS (\ - SELECT ss0."id" AS "id",true AS "smth" FROM "schema1" AS ss0 \ - WHERE (1)\ + SELECT ss0."id" AS "id",true AS "smth" FROM "schema1" AS ss0 WHERE 1\ ),\ "cte2" AS (\ SELECT * FROM schema WHERE 2\ @@ -62,24 +61,22 @@ defmodule Ecto.Integration.InlineSQLTest do SELECT s0."id",0 FROM "schema" AS s0 \ INNER JOIN "schema2" AS s1 ON true \ INNER JOIN "schema2" AS s2 ON false \ - WHERE (true) AND (false) \ + WHERE true AND false \ GROUP BY 3,4 \ - HAVING (true) AND (false) \ + HAVING true AND false \ ORDER BY 7 \ LIMIT 8 \ OFFSET 9 \ UNION DISTINCT \ - (SELECT s0."id",true FROM "schema1" AS s0 \ - WHERE (5)) \ + (SELECT s0."id",true FROM "schema1" AS s0 WHERE 5) \ UNION ALL \ - (SELECT s0."id",false FROM "schema2" AS s0 \ - WHERE (6))\ + (SELECT s0."id",false FROM "schema2" AS s0 WHERE 6)\ """ end test "delete all" do assert TestRepo.to_inline_sql(:delete_all, from(e in "schema", where: e.x == ^123)) == - ~s[DELETE FROM "schema" WHERE ("x" = 123)] + ~s[DELETE FROM "schema" WHERE "x" = 123] end end diff --git a/test/ecto/integration/union_test.exs b/test/ecto/integration/union_test.exs index 334d5a5..288f56a 100644 --- a/test/ecto/integration/union_test.exs +++ b/test/ecto/integration/union_test.exs @@ -80,27 +80,27 @@ defmodule Ecto.Integration.UnionTest do assert sql == """ SELECT p0."title" FROM "posts" AS p0 \ - WHERE (p0."public" = {$0:Bool}) \ + WHERE p0."public" = {$0:Bool} \ ORDER BY p0."counter" \ LIMIT {$8:Int64} \ UNION ALL \ (\ SELECT p0."title" FROM "posts" AS p0 \ - WHERE (p0."public" = {$1:Bool}) \ + WHERE p0."public" = {$1:Bool} \ ORDER BY p0."counter" DESC \ LIMIT {$2:Int64}\ ) \ UNION ALL \ (\ SELECT p0."title" FROM "posts" AS p0 \ - WHERE (p0."public" = {$3:Bool}) \ + WHERE p0."public" = {$3:Bool} \ ORDER BY p0."counter" \ LIMIT {$4:Int64}\ ) \ UNION ALL \ (\ SELECT p0."title" FROM "posts" AS p0 \ - WHERE (p0."public" = {$5:Bool}) \ + WHERE p0."public" = {$5:Bool} \ ORDER BY p0."counter" \ LIMIT {$6:Int64} \ OFFSET {$7:Int64}\ diff --git a/test/ecto_ch_test.exs b/test/ecto_ch_test.exs index 8c0ac7b..325bdb6 100644 --- a/test/ecto_ch_test.exs +++ b/test/ecto_ch_test.exs @@ -35,7 +35,7 @@ defmodule EctoCh.Test do |> select([e], e.user_id) assert all(query) == - {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE (e0."name" = {$0:String}) AND (e0."user_id" > {$1:Int64})], + {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE e0."name" = {$0:String} AND e0."user_id" > {$1:Int64}], ["John", 10]} end @@ -48,7 +48,7 @@ defmodule EctoCh.Test do |> select([e], e.user_id) assert all(query) == - {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE (name = {$0:String})], ["John"]} + {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE name = {$0:String}], ["John"]} end test "where in" do @@ -74,12 +74,11 @@ defmodule EctoCh.Test do countIf(e0."type" = 'pageview'),\ countIf(e0."type" != 'pageview') \ FROM "events" AS e0 \ - WHERE (\ - e0."domain" IN ({$0:String},{$1:String})) AND \ - (e0."tags" = {$2:Array(String)}) AND \ - (toDate(e0."inserted_at") >= {$3:Date}) AND \ - (toDate(e0."inserted_at") <= {$4:Date}\ - )\ + WHERE \ + e0."domain" IN ({$0:String},{$1:String}) AND \ + e0."tags" = {$2:Array(String)} AND \ + toDate(e0."inserted_at") >= {$3:Date} AND \ + toDate(e0."inserted_at") <= {$4:Date}\ """, [ "dummy.site", @@ -96,7 +95,7 @@ defmodule EctoCh.Test do query = "events" |> where(domains: ^domains) |> select([e], e.user_id) assert all(query) == - {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE (e0."domains" = {$0:Array(String)})], + {~s[SELECT e0."user_id" FROM "events" AS e0 WHERE e0."domains" = {$0:Array(String)}], [["dummy.site", "dummy2.site"]]} end @@ -105,7 +104,7 @@ defmodule EctoCh.Test do query = Product |> where(tags: ^tags) |> select([p], p.name) assert all(query) == - {~s[SELECT p0."name" FROM "products" AS p0 WHERE (p0."tags" = {$0:Array(String)})], + {~s[SELECT p0."name" FROM "products" AS p0 WHERE p0."tags" = {$0:Array(String)}], [["a", "b"]]} end end @@ -129,7 +128,7 @@ defmodule EctoCh.Test do user_id = 1 query = "example" |> where([e], e.user_id == ^user_id) |> select([e], e.name) assert {sql, params} = Ecto.Integration.TestRepo.to_sql(:all, query) - assert sql == ~s[SELECT e0."name" FROM "example" AS e0 WHERE (e0."user_id" = {$0:Int64})] + assert sql == ~s[SELECT e0."name" FROM "example" AS e0 WHERE e0."user_id" = {$0:Int64}] assert params == [1] end end