Non-indexed JSONb operations
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
Direzione di ricerca
Inizia in lib/ecto/adapters/postgres/connection.ex intorno alla riga 1054 e riproduci i due esempi di Repo.to_sql che confrontano valori JSONB letterali e parametrizzati. Esamina come vengono rappresentati i percorsi JSONB di PostgreSQL, i valori parametrizzati, l’uguaglianza e il contenimento; il lavoro è completato quando i casi non letterali proposti generano SQL compatibile con gli indici GIN senza introdurre regressioni nelle query letterali esistenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Elixir version
Erlang/OTP 27 [erts-15.2.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] Elixir 1.18.3 (compiled with Erlang/OTP 27)
Database and Version
PostgreSQL 17.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3), 64-bit
Ecto Versions
3.12.5
Database Adapter and Versions (postgrex, myxql, etc)
0.20.0
Current behavior
When comparing jsonb paths with literals in where, Ecto generates efficient query using @>:
iex(1)> q = from j in Oban.Job, where: j.args["event_id"] == "123"
iex(2)> Repo.to_sql(:all, q) |> elem(0) |> IO.puts()
SELECT ... FROM "oban_jobs" AS o0 WHERE ((o0."args"@>'{"event_id": "123"}'))
However, if doing the same, but interpolating the value, e.g.:
iex(1)> q = from j in Oban.Job, where: j.args["event_id"] == ^"123"
iex(2)> Repo.to_sql(:all, q) |> elem(0) |> IO.puts()
SELECT ... FROM "oban_jobs" AS o0 WHERE ((o0."args"#>'{"event_id"}') = $1)
Ecto generates the query using #> operator, which isn't supported by GIN indexes
Expected behavior
It seems that this inefficiency comes from https://github.com/elixir-ecto/ecto_sql/blob/ad5e31c13b034564ff8bf3783bfb2e8ab7c0e6d0/lib/ecto/adapters/postgres/connection.ex#L1054, which is restricted only to literals, and not interpolations.
My proposal is to add support for pinned vars and other db columns with following cases:
-
data known in elixir:
SELECT ... FROM "oban_jobs" AS o0 WHERE (o0."args" @@ '$.event_id #{operator} #{Jason.encode value}') -
equality comparison:
SELECT ... FROM "oban_jobs" AS o0 WHERE (o0."args" @> jsonb_build_object($1, jsonb_build_object($2, ...)))where $1..$n-1 is path items and $n is the value.
Maybe it would be more optimal case for elixir-data equality comparisons since the query can be prepared -
containment check
iex(1)> q = from j in Oban.Job, where: %{event_id: "123"} in j.args iex(2)> Repo.to_sql(:all, q) |> elem(0) |> IO.puts() SELECT ... FROM "oban_jobs" AS o0 WHERE (o0."args" @> jsonb_build_object('event_id', '123'))The idea is to re-build the object using
jsonb_build_objectandjsonb_build_arraywhich would allow for specific values to be non-literals
- Lingua principale
- Elixir
- Stelle
- 657
- Fork
- 345
- Merge medio
- 1h 59m
- PR unite (30g)
- 2
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di elixir-ecto/ecto_sql
-
Introduce Ecto.SQLRepo ApertaKind:Enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
elixir-ecto/ecto_sql#637 · 3 reazioni ·
-
Kind:Bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
elixir-ecto/ecto_sql#602 · 4 commenti ·
Tutte le issue di elixir-ecto/ecto_sql
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
carverauto/serviceradar#4596 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
agentjido/jido_harness#80 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
sevenseacat/cinder#235 ·