Skip to content
Draft
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
20 changes: 20 additions & 0 deletions lib/aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule AshSql.Aggregate do
def add_aggregates(query, aggregates, resource, select?, source_binding, root_data) do
with {:ok, query, aggregates} <-
AshSql.Aggregate.Common.normalize(query, aggregates, resource, root_data) do
query = track_projection_sources(query, aggregates, resource, source_binding)

strategy(query, resource).add_aggregates(
query,
aggregates,
Expand All @@ -31,6 +33,24 @@ defmodule AshSql.Aggregate do
end
end

defp track_projection_sources(query, aggregates, resource, binding) do
if get_in(query.__ash_bindings__, [:bindings, binding, :projection]) do
Enum.reduce(aggregates, query, fn
%{related?: false}, query ->
query

%{relationship_path: [first | _]}, query ->
relationship = Ash.Resource.Info.relationship(resource, first)
AshSql.Query.Projection.track_source(query, binding, relationship)

_, query ->
query
end)
else
query
end
end

def extract_shared_filters(aggregates) do
AshSql.Aggregate.Lateral.extract_shared_filters(aggregates)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/aggregate/grouped/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ defmodule AshSql.Aggregate.Grouped.Query do

repo = AshSql.dynamic_repo(resource, implementation, query)

value = repo.one(query, AshSql.repo_opts(repo, implementation, nil, nil, resource))
value =
repo.one(
AshSql.Query.Projection.prune(query),
AshSql.repo_opts(repo, implementation, nil, nil, resource)
)

{:ok, maybe_default_value(value, aggregate)}
end
Expand All @@ -78,6 +82,7 @@ defmodule AshSql.Aggregate.Grouped.Query do

result =
query
|> AshSql.Query.Projection.prune()
|> repo.one(AshSql.repo_opts(repo, implementation, nil, nil, resource))
|> Map.get(aggregate.name)

Expand Down
8 changes: 6 additions & 2 deletions lib/aggregate/lateral/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ defmodule AshSql.Aggregate.Lateral.Query do

_ ->
repo = AshSql.dynamic_repo(resource, implementation, query)
repo.one(group_query, AshSql.repo_opts(repo, implementation, nil, nil, resource))

repo.one(
AshSql.Query.Projection.prune(group_query),
AshSql.repo_opts(repo, implementation, nil, nil, resource)
)
end

{:ok, add_single_aggs(result, resource, query, cant_group, implementation)}
Expand Down Expand Up @@ -244,7 +248,7 @@ defmodule AshSql.Aggregate.Lateral.Query do
Map.merge(
result || %{},
repo.one(
query,
AshSql.Query.Projection.prune(query),
AshSql.repo_opts(repo, query.__ash_bindings__.sql_behaviour, nil, nil, resource)
)
)
Expand Down
9 changes: 8 additions & 1 deletion lib/bindings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ defmodule AshSql.Bindings do
calculation_names: %{},
context: context,
root_binding: start_bindings,
bindings: %{start_bindings => %{path: [], type: :root, source: resource}}
bindings: %{
start_bindings => %{
path: [],
type: :root,
source: resource,
projection: context[:data_layer][:projection]
}
}
})
end

Expand Down
Loading
Loading