Skip to content

Update query transformation issue #4502

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

Closed
LostKobrakai opened this issue Aug 27, 2024 · 1 comment
Closed

Update query transformation issue #4502

LostKobrakai opened this issue Aug 27, 2024 · 1 comment
Labels

Comments

@LostKobrakai
Copy link
Contributor

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.

This could be a bug in the sqlite adapter as well, but it's hard to judge from the outside.

@josevalim
Copy link
Member

Can you please file this in ecto_sqlite? For Postgres/MySQL/etc we literally generate a LEFT JOIN, so I don't believe the issue is ecto_sql itself but further transformations:

test "update all with left join" do
    query =
      from(m in Schema,
        join: x in assoc(m, :comments),
        left_join: p in assoc(m, :permalink),
        update: [set: [w: m.list2]]
      )
      |> plan(:update_all)

    assert update_all(query) ==
             ~s{UPDATE "schema" AS s0 SET "w" = s0."list2" FROM "schema2" AS s1 LEFT OUTER JOIN "schema3" AS s2 ON s2."id" = s0."y" WHERE (s1."z" = s0."x")}
  end

Unfortunately we cannot transfer issues across orgs.

@josevalim josevalim closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants