@@ -1159,10 +1159,12 @@ defmodule AshPostgres.DataLayer do
1159
1159
{ :ok , data_layer_query } ->
1160
1160
data_layer_query = Ecto.Query . exclude ( data_layer_query , :select )
1161
1161
1162
+ through_binding = Map . get ( query , :__ash_bindings__ ) [ :current ]
1163
+
1162
1164
through_resource
1163
1165
|> Ash.Query . new ( )
1164
1166
|> Ash.Query . put_context ( :data_layer , % {
1165
- start_bindings_at: Map . get ( data_layer_query , :__ash_bindings__ ) [ :current ]
1167
+ start_bindings_at: through_binding
1166
1168
} )
1167
1169
|> Ash.Query . set_context ( through_relationship . context )
1168
1170
|> Ash.Query . do_filter ( through_relationship . filter )
@@ -1192,7 +1194,7 @@ defmodule AshPostgres.DataLayer do
1192
1194
source_query ,
1193
1195
relationship . through
1194
1196
) ,
1195
- as: ^ Map . get ( data_layer_query , :__ash_bindings__ ) [ :current ] ,
1197
+ as: ^ through_binding ,
1196
1198
on:
1197
1199
field ( through , ^ destination_attribute_on_join_resource ) ==
1198
1200
field ( destination , ^ destination_attribute ) ,
@@ -1230,7 +1232,7 @@ defmodule AshPostgres.DataLayer do
1230
1232
source_query ,
1231
1233
relationship . through
1232
1234
) ,
1233
- as: ^ Map . get ( data_layer_query , :__ash_bindings__ ) [ :current ] ,
1235
+ as: ^ through_binding ,
1234
1236
on:
1235
1237
field ( through , ^ destination_attribute_on_join_resource ) ==
1236
1238
field ( destination , ^ destination_attribute ) ,
@@ -1295,10 +1297,8 @@ defmodule AshPostgres.DataLayer do
1295
1297
:no_inner_join? ,
1296
1298
true
1297
1299
)
1300
+ |> Map . delete ( :lateral_join_source )
1298
1301
} )
1299
- # This is a hack, but surely there is just no way someone writes
1300
- # a 500 binding query as the base of a lateral join query...
1301
- |> Ash.Query . set_context ( % { data_layer: % { start_bindings_at: 500 } } )
1302
1302
|> Ash.Query . set_tenant ( source_query . tenant )
1303
1303
|> filter_for_records ( root_data )
1304
1304
|> set_lateral_join_prefix ( query )
@@ -3259,12 +3259,20 @@ defmodule AshPostgres.DataLayer do
3259
3259
def filter ( query , filter , resource , opts \\ [ ] ) do
3260
3260
used_aggregates = Ash.Filter . used_aggregates ( filter , [ ] )
3261
3261
3262
+ query =
3263
+ AshSql.Bindings . default_bindings ( query , resource , AshPostgres.SqlImplementation )
3264
+
3262
3265
query
3263
3266
|> AshSql.Join . join_all_relationships ( filter , opts )
3264
3267
|> case do
3265
3268
{ :ok , query } ->
3266
3269
query
3267
- |> AshSql.Aggregate . add_aggregates ( used_aggregates , resource , false , 0 )
3270
+ |> AshSql.Aggregate . add_aggregates (
3271
+ used_aggregates ,
3272
+ resource ,
3273
+ false ,
3274
+ query . __ash_bindings__ . root_binding
3275
+ )
3268
3276
|> case do
3269
3277
{ :ok , query } ->
3270
3278
{ :ok , AshSql.Filter . add_filter_expression ( query , filter ) }
@@ -3280,12 +3288,24 @@ defmodule AshPostgres.DataLayer do
3280
3288
3281
3289
@ impl true
3282
3290
def add_aggregates ( query , aggregates , resource ) do
3283
- AshSql.Aggregate . add_aggregates ( query , aggregates , resource , true , 0 )
3291
+ AshSql.Aggregate . add_aggregates (
3292
+ query ,
3293
+ aggregates ,
3294
+ resource ,
3295
+ true ,
3296
+ query . __ash_bindings__ . root_binding
3297
+ )
3284
3298
end
3285
3299
3286
3300
@ impl true
3287
3301
def add_calculations ( query , calculations , resource , select? \\ true ) do
3288
- AshSql.Calculation . add_calculations ( query , calculations , resource , 0 , select? )
3302
+ AshSql.Calculation . add_calculations (
3303
+ query ,
3304
+ calculations ,
3305
+ resource ,
3306
+ query . __ash_bindings__ . root_binding ,
3307
+ select?
3308
+ )
3289
3309
end
3290
3310
3291
3311
def add_known_binding ( query , data , known_binding ) do
0 commit comments