From bc93ae9362c1caa71395bbf7b7f955cc51ee0e34 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Thu, 4 Aug 2022 01:40:10 -0400 Subject: [PATCH 1/3] allow hints on joins --- lib/ecto/adapters/sqlite3/connection.ex | 4 +++- test/ecto/adapters/sqlite3/connection_test.exs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ecto/adapters/sqlite3/connection.ex b/lib/ecto/adapters/sqlite3/connection.ex index cecec6a..6e4c48c 100644 --- a/lib/ecto/adapters/sqlite3/connection.ex +++ b/lib/ecto/adapters/sqlite3/connection.ex @@ -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) @@ -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) diff --git a/test/ecto/adapters/sqlite3/connection_test.exs b/test/ecto/adapters/sqlite3/connection_test.exs index 32af2c3..4730f02 100644 --- a/test/ecto/adapters/sqlite3/connection_test.exs +++ b/test/ecto/adapters/sqlite3/connection_test.exs @@ -1623,7 +1623,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do test "join ignores 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() From b8442369458e6376b13456d9a9788b3350266bba Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Thu, 4 Aug 2022 01:49:00 -0400 Subject: [PATCH 2/3] improve tests --- integration_test/hints_test.exs | 23 +++++++++++++++++++ .../ecto/adapters/sqlite3/connection_test.exs | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 integration_test/hints_test.exs diff --git a/integration_test/hints_test.exs b/integration_test/hints_test.exs new file mode 100644 index 0000000..2c448de --- /dev/null +++ b/integration_test/hints_test.exs @@ -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 diff --git a/test/ecto/adapters/sqlite3/connection_test.exs b/test/ecto/adapters/sqlite3/connection_test.exs index 4730f02..5a39e65 100644 --- a/test/ecto/adapters/sqlite3/connection_test.exs +++ b/test/ecto/adapters/sqlite3/connection_test.exs @@ -1620,7 +1620,7 @@ 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: ["INDEXED BY FOO", "INDEXED BY BAR"]) @@ -1631,7 +1631,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do """ 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 From 6a396b652da7155c998f9e0969e7882f5f19a013 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Thu, 4 Aug 2022 11:09:48 -0400 Subject: [PATCH 3/3] remove extra space --- test/ecto/adapters/sqlite3/connection_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ecto/adapters/sqlite3/connection_test.exs b/test/ecto/adapters/sqlite3/connection_test.exs index 5a39e65..4559697 100644 --- a/test/ecto/adapters/sqlite3/connection_test.exs +++ b/test/ecto/adapters/sqlite3/connection_test.exs @@ -1620,7 +1620,7 @@ defmodule Ecto.Adapters.SQLite3.ConnectionTest do """ end - test "join hints" do + test "join hints" do query = Schema |> join(:inner, [p], q in Schema2, hints: ["INDEXED BY FOO", "INDEXED BY BAR"])