Skip to content

Join hints #83

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

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions integration_test/hints_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Ecto.Integration.HintsTest do
use Ecto.Integration.Case, async: true

import Ecto.Query, only: [from: 2]

alias Ecto.Integration.Post
alias Ecto.Integration.TestRepo

test "join hints" do
{:ok, _} = TestRepo.query("CREATE INDEX post_id_idx ON posts (id)")
TestRepo.insert!(%Post{id: 1})

results =
from(p in Post,
join: p2 in Post,
on: p.id == p2.id,
hints: ["INDEXED BY post_id_idx"]
)
|> TestRepo.all()

assert [%Post{id: 1}] = results
end
end
4 changes: 3 additions & 1 deletion lib/ecto/adapters/sqlite3/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,8 @@ defmodule Ecto.Adapters.SQLite3.Connection do
on: %QueryExpr{expr: expression},
qual: qual,
ix: ix,
source: source
source: source,
hints: hints
} ->
{join, name} = get_source(query, sources, ix, source)

Expand All @@ -985,6 +986,7 @@ defmodule Ecto.Adapters.SQLite3.Connection do
join,
" AS ",
name,
Enum.map(hints, &[?\s | &1]),
join_on(qual, expression, sources, query)
]
end)
Expand Down
6 changes: 3 additions & 3 deletions test/ecto/adapters/sqlite3/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,18 +1620,18 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do
"""
end

test "join ignores hints" do
test "join hints" do
query =
Schema
|> join(:inner, [p], q in Schema2, hints: ["USE INDEX FOO", "USE INDEX BAR"])
|> join(:inner, [p], q in Schema2, hints: ["INDEXED BY FOO", "INDEXED BY BAR"])
|> select([], true)
|> plan()

assert all(query) ==
"""
SELECT 1 \
FROM "schema" AS s0 \
INNER JOIN "schema2" AS s1 ON 1\
INNER JOIN "schema2" AS s1 INDEXED BY FOO INDEXED BY BAR ON 1\
"""
end

Expand Down