Skip to content

Conversation

@dkuku
Copy link
Contributor

@dkuku dkuku commented Dec 10, 2025

Problem: Boolean operations (and, or) inside fragment expressions were not being
wrapped in parentheses, causing SQL precedence issues. For example:
fragment("?::integer", r.x and r.y) generated s0."x" AND s0."y"::integer
But should be (s0."x" AND s0."y")::integer

closes #4688

Test output before the implementation, now the test are passing.

  1) test fragments (Ecto.Adapters.TdsTest)
     test/ecto/adapters/tds_test.exs:690
     Assertion with == failed
     code:  assert all(query) == ~s{SELECT CAST((s0.[x] AND s0.[y]) AS INT) FROM [schema] AS s0}
     left:  "SELECT CAST(s0.[x] AND s0.[y] AS INT) FROM [schema] AS s0"
     right: "SELECT CAST((s0.[x] AND s0.[y]) AS INT) FROM [schema] AS s0"
     stacktrace:
       test/ecto/adapters/tds_test.exs:724: (test)

...............................................................................................................................................................................................

  2) test fragments (Ecto.Adapters.MyXQLTest)
     test/ecto/adapters/myxql_test.exs:645
     Assertion with == failed
     code:  assert all(query) == ~s{SELECT CAST((s0.`x` AND s0.`y`) AS INT) FROM `schema` AS s0}
     left:  "SELECT CAST(s0.`x` AND s0.`y` AS INT) FROM `schema` AS s0"
     right: "SELECT CAST((s0.`x` AND s0.`y`) AS INT) FROM `schema` AS s0"
     stacktrace:
       test/ecto/adapters/myxql_test.exs:686: (test)

....................................

  3) test fragments (Ecto.Adapters.PostgresTest)
     test/ecto/adapters/postgres_test.exs:828
     Assertion with == failed
     code:  assert all(query) == ~s{SELECT (s0."x" AND s0."y")::integer FROM "schema" AS s0}
     left:  "SELECT s0.\"x\" AND s0.\"y\"::integer FROM \"schema\" AS s0"
     right: "SELECT (s0.\"x\" AND s0.\"y\")::integer FROM \"schema\" AS s0"
     stacktrace:
       test/ecto/adapters/postgres_test.exs:868: (test)
       ```

@josevalim josevalim merged commit 271b544 into elixir-ecto:master Dec 11, 2025
11 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants