JasperFx / marten

.NET Transactional Document DB and Event Store on PostgreSQL
https://martendb.io
MIT License
2.79k stars 441 forks source link

When combining .Include and .Select in a linq query the result will be all rows in the table #3192

Closed arildboifot closed 3 months ago

arildboifot commented 3 months ago

Given a query like this, expecting 0 rows, it actually returns all rows.:

        var teams = new Dictionary<Guid, Team>();        
        var ids = Array.Empty<Guid>();
        var tp = await _session
           .Query<TeamPlayer>()
           .Include(x => x.TeamId, teams)
           .Where(x => x.Id.IsOneOf(ids) && x.IsAdministrator == true)
           .Select(x => x.PlayerId)
           .ToListAsync();

Generated sql with .Select

drop table if exists mt_temp_id_list1;
create temp table mt_temp_id_list1 as (
    select d.id, d.data, d.mt_version, d.tenant_id, d.mt_deleted, d.mt_deleted_at, d.player_id, d.team_id, d.pending_invitation, d.is_administrator
    from public.mt_doc_teamplayers as d
    where (d.mt_deleted = False and d.tenant_id = $1 and d.id = ANY($2) and d.is_administrator = $3)
);
  : *DEFAULT*
  :
  : True
 select d.id, d.data, d.mt_version, d.tenant_id, d.mt_deleted, d.mt_deleted_at
 from public.mt_doc_team as d
 where (d.mt_deleted = False and d.tenant_id = $1 and d.id in (select d.team_id from mt_temp_id_list1 as d));
  : *DEFAULT*

 select d.player_id from public.mt_doc_teamplayers as d

Generated sql without .Select

-- Query without .Select(x => x.PlayerId)
drop table if exists mt_temp_id_list1;
create temp table mt_temp_id_list1 as (
    select d.id, d.data, d.mt_version, d.tenant_id, d.mt_deleted, d.mt_deleted_at, d.player_id, d.team_id, d.pending_invitation, d.is_administrator
    from public.mt_doc_teamplayers as d
    where (d.mt_deleted = False and d.tenant_id = $1 and d.id = ANY($2) and d.is_administrator = $3)
);
  : *DEFAULT*
  :
  : True
 select d.id, d.data, d.mt_version, d.tenant_id, d.mt_deleted, d.mt_deleted_at
 from public.mt_doc_team as d
 where (d.mt_deleted = False and d.tenant_id = $1 and d.id in (select d.team_id from mt_temp_id_list1 as d));
  : *DEFAULT*
 select d.id, d.data, d.mt_version, d.tenant_id, d.mt_deleted, d.mt_deleted_at, d.player_id, d.team_id, d.pending_invitation, d.is_administrator from mt_temp_id_list1 as d