Skip to content
Open
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
35 changes: 30 additions & 5 deletions lib/join.ex
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@
has_parent_expr? =
opts[:require_lateral?] ||
!!query.__ash_bindings__.context[:data_layer][:has_parent_expr?] ||
not is_nil(query.limit)
not is_nil(query.limit) ||
not is_nil(query.offset)

query =
if has_parent_expr? do
Expand Down Expand Up @@ -457,6 +458,13 @@
|> Ash.Query.unset(:sort)
end
end)
|> then(fn query ->
if not is_nil(Map.get(relationship, :offset)) do

Check warning on line 462 in lib/join.ex

View workflow job for this annotation

GitHub Actions / ash-ci / mix credo --strict

Avoid negated conditions in if-else blocks.
Ash.Query.offset(query, relationship.offset)
else
query
end
end)
|> set_has_parent_expr_context(relationship)
|> case do
%{valid?: true} = related_query ->
Expand Down Expand Up @@ -544,14 +552,23 @@

defp limit_from_many(
query,
%{from_many?: true, destination: destination},
%{from_many?: true, destination: destination} = relationship,
filter,
filter_subquery?,
opts
) do
offset = Map.get(relationship, :offset)

if filter_subquery? do
inner_query =
if offset do
from(row in query, limit: 1, offset: ^offset)
else
from(row in query, limit: 1)
end

query =
from(row in Ecto.Query.subquery(from(row in query, limit: 1)),
from(row in Ecto.Query.subquery(inner_query),
as: ^query.__ash_bindings__.root_binding
)
|> Map.put(:__ash_bindings__, query.__ash_bindings__)
Expand All @@ -578,7 +595,7 @@

defp limit_from_many(
query,
%{limit: limit, destination: destination},
%{limit: limit, destination: destination} = relationship,
filter,
filter_subquery?,
opts
Expand All @@ -587,11 +604,19 @@
# Check if query has parent expressions - if so, we can't wrap in a non-lateral subquery
# because parent references won't resolve across the subquery boundary
has_parent_expr? = !!query.__ash_bindings__.context[:data_layer][:has_parent_expr?]
offset = Map.get(relationship, :offset)

if filter_subquery? && !has_parent_expr? do
# Wrap the limited query in a subquery, then apply filter on top
inner_query =
if offset do
from(row in query, limit: ^limit, offset: ^offset)
else
from(row in query, limit: ^limit)
end

query =
from(row in Ecto.Query.subquery(from(row in query, limit: ^limit)),
from(row in Ecto.Query.subquery(inner_query),
as: ^query.__ash_bindings__.root_binding
)
|> Map.put(:__ash_bindings__, query.__ash_bindings__)
Expand Down
Loading