Skip to content

Commit c72aa10

Browse files
greg-rychlewskiGreg Rychlewski
andauthored
Allow hints on joins (#83)
Co-authored-by: Greg Rychlewski <[email protected]>
1 parent d297021 commit c72aa10

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

integration_test/hints_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule Ecto.Integration.HintsTest do
2+
use Ecto.Integration.Case, async: true
3+
4+
import Ecto.Query, only: [from: 2]
5+
6+
alias Ecto.Integration.Post
7+
alias Ecto.Integration.TestRepo
8+
9+
test "join hints" do
10+
{:ok, _} = TestRepo.query("CREATE INDEX post_id_idx ON posts (id)")
11+
TestRepo.insert!(%Post{id: 1})
12+
13+
results =
14+
from(p in Post,
15+
join: p2 in Post,
16+
on: p.id == p2.id,
17+
hints: ["INDEXED BY post_id_idx"]
18+
)
19+
|> TestRepo.all()
20+
21+
assert [%Post{id: 1}] = results
22+
end
23+
end

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,8 @@ defmodule Ecto.Adapters.SQLite3.Connection do
976976
on: %QueryExpr{expr: expression},
977977
qual: qual,
978978
ix: ix,
979-
source: source
979+
source: source,
980+
hints: hints
980981
} ->
981982
{join, name} = get_source(query, sources, ix, source)
982983

@@ -985,6 +986,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
985986
join,
986987
" AS ",
987988
name,
989+
Enum.map(hints, &[?\s | &1]),
988990
join_on(qual, expression, sources, query)
989991
]
990992
end)

test/ecto/adapters/sqlite3/connection_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,18 +1620,18 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
16201620
"""
16211621
end
16221622

1623-
test "join ignores hints" do
1623+
test "join hints" do
16241624
query =
16251625
Schema
1626-
|> join(:inner, [p], q in Schema2, hints: ["USE INDEX FOO", "USE INDEX BAR"])
1626+
|> join(:inner, [p], q in Schema2, hints: ["INDEXED BY FOO", "INDEXED BY BAR"])
16271627
|> select([], true)
16281628
|> plan()
16291629

16301630
assert all(query) ==
16311631
"""
16321632
SELECT 1 \
16331633
FROM "schema" AS s0 \
1634-
INNER JOIN "schema2" AS s1 ON 1\
1634+
INNER JOIN "schema2" AS s1 INDEXED BY FOO INDEXED BY BAR ON 1\
16351635
"""
16361636
end
16371637

0 commit comments

Comments
 (0)