ecto_sqlite3 icon indicating copy to clipboard operation
ecto_sqlite3 copied to clipboard

Update query transformation issue

Open LostKobrakai opened this issue 1 year ago • 2 comments

Elixir version

1.17.2

Database and Version

sqlite 3 (as brought by adapter)

Ecto Versions

3.12.1

Database Adapter and Versions (postgrex, myxql, etc)

exqlite 0.23, ecto_sqlite3 0.17.0

Current behavior

The below query doing left joins is turned into multiple FROM clauses, which according to the sqlite docs stating they modeled it after postgres and postgres documenting it to be a cross join, would be a different kind of join.

#Ecto.Query<from p0 in Planner.ProjectsQwer.Project,
 left_join: q1 in subquery(from q0 in "qwer_placeables",
  where: q0.project_id == ^1,
  group_by: [fragment("TRUE")],
  select: %{
  json:
    fragment(
      "json_group_array(?)",
      fragment(
        "json_object(?,?,?,?,?,?,?,?,?,?,?,?,?,?)", […fields]
      )
    )
}),
 as: :placeables, on: true,
 left_join: q2 in subquery(from q0 in "qwer_links",
  where: q0.project_id == ^1,
  group_by: [fragment("TRUE")],
  select: %{
  json:
    fragment(
      "json_group_array(?)",
      fragment(
        "json_object(?,?,?,?,?,?,?,?,?,?,?,?)", […fields]
      )
    )
}),
 as: :links, on: true, where: p0.id == ^1,
 update: [
  set: [
    snapshots:
      fragment(
        "json_insert(?,?,?)",
        fragment(
          "CASE WHEN ? THEN ? ELSE ? END",
          fragment("json_array_length(?)", p0.snapshots) >= 10,
          fragment("json_remove(?,?)", p0.snapshots, "$[0]"),
          p0.snapshots
        ),
        "$[#]",
        fragment("json_object(?,?,?,?)", "placeables", q1.json, "links", q2.json)
      )
  ]
]>
UPDATE "qwer_projects" AS q0 
SET "snapshots" = json_insert(CASE WHEN json_array_length(q0."snapshots") >= 10 THEN json_remove(q0."snapshots",'$[0]') ELSE q0."snapshots" END,'$[#]',json_object('placeables',s1."json",'links',s2."json")) 
FROM 
    (
        SELECT json_group_array(json_object([...fields])) AS "json" 
        FROM "qwer_placeables" AS sq0 
        WHERE (sq0."project_id" = ?) 
        GROUP BY TRUE
    ) AS s1, 
    (
        SELECT json_group_array(json_object([...fields])) AS "json" 
        FROM "qwer_links" AS sq0 
        WHERE (sq0."project_id" = ?) 
        GROUP BY TRUE
    ) AS s2 
    WHERE (q0."id" = ?)

Expected behavior

The join semantics are retained.

Follow up to https://github.com/elixir-ecto/ecto/issues/4502

LostKobrakai avatar Aug 27 '24 09:08 LostKobrakai