@@ -301,7 +301,7 @@ defmodule AshSql.Query do
301301 parent_ref_attrs =
302302 query . __ash_bindings__ [ :load_aggregates ]
303303 |> List . wrap ( )
304- |> extract_aggregate_parent_ref_attributes ( resource )
304+ |> extract_aggregate_parent_ref_attributes ( resource , query )
305305
306306 query_with_parent_refs =
307307 Enum . reduce ( parent_ref_attrs , query , fn attr , query ->
@@ -310,7 +310,9 @@ defmodule AshSql.Query do
310310 end )
311311
312312 query_with_order =
313- from ( row in query_with_parent_refs , select_merge: % { __order__: over ( row_number ( ) , :order ) } )
313+ from ( row in query_with_parent_refs ,
314+ select_merge: % { __order__: over ( row_number ( ) , :order ) }
315+ )
314316
315317 query_without_limit_and_offset =
316318 query_with_order
@@ -631,18 +633,26 @@ defmodule AshSql.Query do
631633 end )
632634 end
633635
634- defp extract_aggregate_parent_ref_attributes ( aggregates , resource ) do
636+ defp extract_aggregate_parent_ref_attributes ( aggregates , resource , query ) do
635637 aggregates
636638 |> Enum . flat_map ( fn aggregate ->
637639 case aggregate . relationship_path do
638640 [ first_rel_name | _ ] ->
639641 relationship = Ash.Resource.Info . relationship ( resource , first_rel_name )
640642
643+ rel_fields =
644+ if ! Map . get ( relationship , :no_attributes? ) && Map . get ( relationship , :source_attribute ) do
645+ [ relationship . source_attribute ]
646+ else
647+ [ ]
648+ end
649+
641650 if relationship && relationship . filter do
642- extract_parent_attrs_from_filter ( relationship . filter )
651+ extract_parent_attrs_from_filter ( relationship . filter , query )
643652 else
644653 [ ]
645654 end
655+ |> Enum . concat ( rel_fields )
646656
647657 _ ->
648658 [ ]
@@ -651,8 +661,18 @@ defmodule AshSql.Query do
651661 |> Enum . uniq ( )
652662 end
653663
654- defp extract_parent_attrs_from_filter ( filter ) do
655- Ash.Filter . flat_map ( filter , fn
664+ defp extract_parent_attrs_from_filter ( filter , query ) do
665+ filter
666+ |> Ash.Actions.Read . add_calc_context_to_filter (
667+ query . __ash_bindings__ [ :context ] [ :private ] [ :actor ] ,
668+ query . __ash_bindings__ [ :context ] [ :private ] [ :authorize? ] ,
669+ query . __ash_bindings__ [ :context ] [ :private ] [ :tenant ] ,
670+ query . __ash_bindings__ [ :context ] [ :private ] [ :tracer ] ,
671+ query . __ash_bindings__ [ :domain ] ,
672+ query . __ash_bindings__ . resource ,
673+ parent_stack: query . __ash_bindings__ [ :parent_resources ] || [ ]
674+ )
675+ |> Ash.Filter . flat_map ( fn
656676 % Ash.Query.Parent { expr: expr } ->
657677 expr
658678 |> Ash.Filter . list_refs ( )
0 commit comments