Skip to content
Open
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
7 changes: 6 additions & 1 deletion lib/aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@
result =
remaining_aggregates
|> Enum.group_by(fn aggregate ->
{aggregate.relationship_path || [], aggregate.resource, aggregate.join_filters || %{},
expanded_path =
aggregate.resource
|> AshSql.Join.relationship_path_to_relationships(aggregate.relationship_path)
|> Enum.map(& &1.name)

{expanded_path || [], aggregate.resource, aggregate.join_filters || %{},

Check warning on line 145 in lib/aggregate.ex

View workflow job for this annotation

GitHub Actions / ash-ci / mix dialyzer

guard_fail

The guard clause can never succeed.
aggregate.query.action.name}
end)
|> Enum.flat_map(fn {{path, resource, join_filters, read_action}, aggregates} ->
Expand Down
21 changes: 19 additions & 2 deletions lib/join.ex
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,15 @@ defmodule AshSql.Join do
raise "no such relationship #{inspect(resource)}.#{name}"
end

relationship_path_to_relationships(relationship.destination, rest, [relationship | acc])
case Map.get(relationship, :through) do
through_path when is_list(through_path) ->
relationships = relationship_path_to_relationships(resource, through_path, [])
destination = Map.get(List.last(relationships) || relationship, :destination)
relationship_path_to_relationships(destination, rest, Enum.reverse(relationships) ++ acc)

_ ->
relationship_path_to_relationships(relationship.destination, rest, [relationship | acc])
end
end

def related_subquery(
Expand Down Expand Up @@ -491,9 +499,18 @@ defmodule AshSql.Join do
ecto_query
end

resource_for_prefix =
case relationship do
%{type: :many_to_many, through: through} when not is_list(through) ->
through

_ ->
relationship.destination
end

{:ok,
ecto_query
|> set_join_prefix(query, Map.get(relationship, :through, relationship.destination))
|> set_join_prefix(query, resource_for_prefix)
|> Ecto.Query.exclude(:select)}

{:error, error} ->
Expand Down
46 changes: 32 additions & 14 deletions lib/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,39 @@ defmodule AshSql.Query do
|> case do
{:ok, lateral_join_source_query} ->
lateral_join_source_query =
if Enum.count(path) == 2 do
if Enum.count(path) >= 2 do
through_entries = Enum.drop(path, 1)

Map.update!(lateral_join_source_query, :__ash_bindings__, fn bindings ->
bindings
|> Map.put(:lateral_join_bindings, [start_bindings + 1])
|> Map.update!(:bindings, fn bindings ->
Map.put(
bindings,
start_bindings + 1,
%{
source: path |> Enum.at(1) |> elem(3) |> Map.get(:source),
path: [path |> Enum.at(1) |> elem(3) |> Map.get(:name)],
type: :inner
}
)
end)
{updated_bindings, _} =
Enum.reduce(through_entries, {bindings, start_bindings + 1}, fn entry,
{acc_bindings,
binding_idx} ->
rel = elem(entry, 3)

acc_bindings =
acc_bindings
|> Map.update(
:lateral_join_bindings,
[binding_idx],
&(&1 ++ [binding_idx])
)
|> Map.update!(:bindings, fn b ->
Map.put(
b,
binding_idx,
%{
source: Map.get(rel, :source),
path: [Map.get(rel, :name)],
type: :inner
}
)
end)

{acc_bindings, binding_idx + 1}
end)

updated_bindings
end)
else
lateral_join_source_query
Expand Down
Loading