fix: use synonymous_relationship_path when looking up ref bindings

This commit is contained in:
Zach Daniel 2023-11-16 11:02:24 -05:00
parent af8d8dbcac
commit ef96cd541e
4 changed files with 33 additions and 5 deletions

View file

@ -1492,13 +1492,23 @@ defmodule AshPostgres.Expr do
defp ref_binding(%{attribute: %Ash.Resource.Attribute{}} = ref, bindings) do
Enum.find_value(bindings.bindings, fn {binding, data} ->
data.path == ref.relationship_path && data.type in [:inner, :left, :root] && binding
data.type in [:inner, :left, :root] &&
Ash.SatSolver.synonymous_relationship_paths?(
bindings.resource,
data.path,
ref.relationship_path
) && binding
end)
end
defp ref_binding(%{attribute: %Ash.Query.Aggregate{}} = ref, bindings) do
Enum.find_value(bindings.bindings, fn {binding, data} ->
data.path == ref.relationship_path && data.type in [:inner, :left, :root] && binding
data.type in [:inner, :left, :root] &&
Ash.SatSolver.synonymous_relationship_paths?(
bindings.resource,
data.path,
ref.relationship_path
) && binding
end)
end

View file

@ -30,6 +30,21 @@ defmodule AshPostgres.Join do
relationship_paths \\ nil,
path \\ [],
source \\ nil
)
# simple optimization for common cases
def join_all_relationships(query, filter, _opts, relationship_paths, _path, _source)
when is_nil(relationship_paths) and filter in [nil, true, false] do
{:ok, query}
end
def join_all_relationships(
query,
filter,
opts,
relationship_paths,
path,
source
) do
relationship_paths =
cond do
@ -49,6 +64,8 @@ defmodule AshPostgres.Join do
|> to_joins(filter, query.__ash_bindings__.resource)
true ->
id = System.unique_integer([:monotonic, :positive])
filter
|> Ash.Filter.relationship_paths()
|> to_joins(filter, query.__ash_bindings__.resource)
@ -130,6 +147,7 @@ defmodule AshPostgres.Join do
defp to_joins(paths, filter, resource) do
paths
|> Enum.reject(&(&1 == []))
|> Enum.map(fn path ->
if can_inner_join?(path, filter) do
{:inner,

View file

@ -204,7 +204,7 @@ defmodule AshPostgres.MixProject do
{:ecto, "~> 3.9"},
{:jason, "~> 1.0"},
{:postgrex, ">= 0.0.0"},
{:ash, ash_version("~> 2.17 and >= 2.17.2")},
{:ash, ash_version("~> 2.17 and >= 2.17.3")},
{:benchee, "~> 1.1", only: [:dev, :test]},
{:git_ops, "~> 2.5", only: [:dev, :test]},
{:ex_doc, github: "elixir-lang/ex_doc", only: [:dev, :test], runtime: false},

View file

@ -49,10 +49,10 @@ defmodule AshPostgres.Test.ComplexCalculations.Channel do
expr(
cond do
first_member.user_id == ^actor(:id) ->
second_member_name
first_member_name
second_member.user_id == ^actor(:id) ->
first_member_name
second_member_name
true ->
first_member_name <> ", " <> second_member_name