Skip to content

Commit 92efa55

Browse files
committed
Fix precedence on type with binary op, closes #305
1 parent f2c3143 commit 92efa55

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

lib/ecto/adapters/postgres/connection.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ if Code.ensure_loaded?(Postgrex) do
687687
case handle_call(fun, length(args)) do
688688
{:binary_op, op} ->
689689
[left, right] = args
690-
[op_to_binary(left, sources, query), op | op_to_binary(right, sources, query)]
690+
[maybe_paren(left, sources, query), op | maybe_paren(right, sources, query)]
691691
{:fun, fun} ->
692692
[fun, ?(, modifier, intersperse_map(args, ", ", &expr(&1, sources, query)), ?)]
693693
end
@@ -707,7 +707,7 @@ if Code.ensure_loaded?(Postgrex) do
707707
end
708708

709709
defp expr(%Ecto.Query.Tagged{value: other, type: type}, sources, query) do
710-
[expr(other, sources, query), ?:, ?: | tagged_to_db(type)]
710+
[maybe_paren(other, sources, query), ?:, ?: | tagged_to_db(type)]
711711
end
712712

713713
defp expr(nil, _sources, _query), do: "NULL"
@@ -749,13 +749,13 @@ if Code.ensure_loaded?(Postgrex) do
749749
interval(1, interval, sources, query), ?)]
750750
end
751751

752-
defp op_to_binary({op, _, [_, _]} = expr, sources, query) when op in @binary_ops,
752+
defp maybe_paren({op, _, [_, _]} = expr, sources, query) when op in @binary_ops,
753753
do: paren_expr(expr, sources, query)
754754

755-
defp op_to_binary({:is_nil, _, [_]} = expr, sources, query),
755+
defp maybe_paren({:is_nil, _, [_]} = expr, sources, query),
756756
do: paren_expr(expr, sources, query)
757757

758-
defp op_to_binary(expr, sources, query),
758+
defp maybe_paren(expr, sources, query),
759759
do: expr(expr, sources, query)
760760

761761
defp returning(%{select: nil}, _sources),

test/ecto/adapters/postgres_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ defmodule Ecto.Adapters.PostgresTest do
531531
end
532532

533533
test "tagged type" do
534+
query = Schema |> select([t], type(t.x + t.y, :integer)) |> plan()
535+
assert all(query) == ~s{SELECT (s0."x" + s0."y")::bigint FROM "schema" AS s0}
536+
534537
query = Schema |> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID)) |> plan()
535538
assert all(query) == ~s{SELECT $1::uuid FROM "schema" AS s0}
536539

0 commit comments

Comments
 (0)