Skip to content
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

Update query transformation issue #150

Open
LostKobrakai opened this issue Aug 27, 2024 · 2 comments
Open

Update query transformation issue #150

LostKobrakai opened this issue Aug 27, 2024 · 2 comments

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.

Follow up to elixir-ecto/ecto#4502

@warmwaffles
Copy link
Member

I think I kind of understand what you are asking.

Do you have a stub schema, some example rows, and the expected results update results?

In the mean time, I'll see if I can come up with a simpler reproduction of this problem without the json stuff. (although having that in the regression test wouldn't be a bad thing either)

@LostKobrakai
Copy link
Contributor Author

Repo.insert_all("table", [%{number: 0}])
a = from x in fragment("json_each(?)", "[]"), select: %{value: x.value}

u = 
  from x in "table",
    left_join: a in subquery(a), on: true,
    update: [set: [number: coalesce(a.value, 1)]]
 
{1, _} = Repo.update_all(u, [])

This should be a quote simple example. Just needs a table with a number column. You could replace the subquery with a table having a value column (int), but being empty. Currently the update would result in no row updated, where it should.

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

No branches or pull requests

2 participants