Skip to content

Cosmos: Projecting out nested documents retrieves the entire document #34067

Closed

Description

@roji
public virtual Task Project_owned_reference_navigation_which_owns_additional(bool async)
    => AssertQuery(
        async,
        ss => ss.Set<OwnedPerson>().OrderBy(o => o.Id).Select(p => p.PersonAddress));

Since PersonAddress owns further entity types (OwnedCountry), the expression inside the Select() contains IncludeExpression; this leads CosmosProjectionBindingExpressionVisitor into a specific path where we end up projecting the entire document. Compare how this works in relational - CosmosProjectionBindingExpressionVisitor is one place where Cosmos is still quite behind.

Activity

  1. added this to the 9.0.0 milestone on Jun 22, 2024
  2. self-assigned this
    on Jun 22, 2024
  3. changed the title [-]Cosmos: Projecting out owned entities retrieves the entire document[/-] [+]Cosmos: Projecting out owned entities which own additional entity types retrieves the entire document[/+] on Jun 22, 2024
  4. added a commit that references this issue on Jul 30, 2024
    53dca3b
  5. modified the milestones: 9.0.0, Backlog on Jul 31, 2024
  6. added a commit that references this issue on Aug 2, 2024
    a63802b
  7. 17 remaining items

  8. roji commented on May 19, 2026

    @roji
    MemberAuthor

    @JoasE thanks for this analysis.

    We indeed discussed similar cases back in EF 9 when redoing significant parts of the Cosmos query pipeline. Back then, of course, the discussion was restricted to projecting pure scalars, since the pipeline didn't (and still doesn't) support projecting out nested documents without the entire document.

    So the cases you're outlining above are new, and I admit they're not great... Here are some thoughts:

    1. For the first case, which IIUC is a problem only for AsNoTrackingWithIdentityResolution with owned entities; I generally think this isn't important enough. The thing to concentrate on going forward is IMHO complex types, where none of this can occur (complex types are always projected out as non-tracking and without any sort of identity). IMHO this also highlights the general weirdness of using the owned entity concept to map something that does not actually have an identity.
    2. For entity vs. anonymous type projection, if we squint a bit, I think the behavior actually makes sense. Anonymous type projection means there's a new in the projection, so the result indeed should never be undefined (at least I hope my understanding of the current behavior is correct) - it only be missing certain properties inside (since they're undefined). Whereas projecting structural types via properties is inherently different, and if the projected structural type is truly absent from the document, then the general "filter out absent/undefined" makes sense (to me), even if I can see how it could be surprising. But I'm convinced it's any more surprising than the existing filtering-out behavior of scalars which we already have today.
    3. I'm unaware of the behavioral difference you're describing between projecting an anonymous type with one property vs. two; that indeed sounds like a separate/unrelated bug. Can you open a separate issue with a repro?

    The main thing I'm trying to do here, is to stick to the database semantics/behavior as much as possible, and to avoid EF imposing an external behavior which we believe is better. In other words, I'm not a fan of the Cosmos behavior around filtering out undefined (though I understand where it comes from - Cosmos returns JSON and there's no concept of undefined there); but that's the Cosmos behavior that we're surfacing. Another way of looking at this is that someone using Cosmos without EF will run into exactly the same behavior - it's just how Cosmos works - and I don't think it should generally be our job to change or "repair" database behavior, just to expose it.

    But the above are just my own views - what are your thoughts on the above? Also, this is definitely something to be discussed with @AndriySvyryd.

  9. JoasE commented on May 19, 2026

    @JoasE
    Contributor

    @roji That makes a lot of sense, thanks for explaining.

    The main thing I'm trying to do here, is to stick to the database semantics/behavior as much as possible, and to avoid EF imposing an external behavior which we believe is better. In other words, I'm not a fan of the Cosmos behavior around filtering out undefined (though I understand where it comes from - Cosmos returns JSON and there's no concept of undefined there); but that's the Cosmos behavior that we're surfacing. Another way of looking at this is that someone using Cosmos without EF will run into exactly the same behavior - it's just how Cosmos works - and I don't think it should generally be our job to change or "repair" database behavior, just to expose it.

    Being the devils advocate, SELECT VALUE is opt-in behavior in the cosmos query langue. On the other hand, I did some testing and the (.NET) Cosmos SDK ItemLinqQueryable does also translate these cases to SELECT VALUE, and causes the same behavior for entity vs. anonymous type projection. So someone using the SDK would indeed run into the same and I think the new is never undefined argument makes sense there.

    For the first case, which IIUC is a problem only for AsNoTrackingWithIdentityResolution with owned entities; I generally think this isn't important enough. The thing to concentrate on going forward is IMHO complex types, where none of this can occur (complex types are always projected out as non-tracking and without any sort of identity). IMHO this also highlights the general weirdness of using the owned entity concept to map something that does not actually have an identity.

    I had similar thoughts about this case as well, but was also wondering whether there might be other cases now (or very likely in the future?), where EF might need to project out 2 values where the user query is only selecting one, opening up the same question again.

    Assuming we are keeping the SELECT VALUE behaviour, my main question on AsNoTrackingWithIdentityResolution is how I should move forward in the context of this issue. Are you implying we will just accept different filtering behavior for AsNoTrackingWithIdentityResolution for now? Maybe also good to note that relational currently always projects out the primary key of the owner entity, but I think I could make it so cosmos only projects it out when AsNoTrackingWithIdentityResolution is used. Or are you suggesting dropping support for AsNoTrackingWithIdentityResolution (edit: for owned entities) in cosmos, keeping in mind we will eventually drop support anyway?

  10. JoasE commented on Jun 16, 2026

    @JoasE
    Contributor

    For context: in #38298 (comment) , it was discussed that SELECT VALUE will remain for now, but is subject for removal in the future.

    @AndriySvyryd In this issue, I am still uncertain how to proceed with AsNoTrackingWithIdentityResolution considering it would cause inconsistent filtering behaviour.

    See:

    Assuming we are keeping the SELECT VALUE behaviour, my main question on AsNoTrackingWithIdentityResolution is how I should move forward in the context of this issue. Are you implying we will just accept different filtering behavior for AsNoTrackingWithIdentityResolution for now? Maybe also good to note that relational currently always projects out the primary key of the owner entity, but I think I could make it so cosmos only projects it out when AsNoTrackingWithIdentityResolution is used. Or are you suggesting dropping support for AsNoTrackingWithIdentityResolution (edit: for owned entities) in cosmos, keeping in mind we will eventually drop support anyway?

  11. AndriySvyryd commented on Jun 17, 2026

    @AndriySvyryd
    Member

    To get us to a place of some consistency I propose to apply this rule of thumb: "Any naked projection will be translated as SELECT VALUE and any top-level instantiation (DTO/anon/entity/complex) in the projection won't use SELECT VALUE".

    AsNoTrackingWithIdentityResolution on a query that doesn't already project out the entity or its key should throw.

  12. AndriySvyryd commented on Jun 19, 2026

    @AndriySvyryd
    Member

    @JoasE Just FYI - July 27 is the last day we can merge a big change like this one for v11

  13. modified the milestones: Backlog, 11.0.0 on Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions