Correctly translate cardinality operators (Take/Skip/Distinct) in Join/LeftJoin/GroupJoin inner subqueries

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      A global cardinality operator in a Join/LeftJoin inner subquery (e.g. orders.OrderBy(o => o.Id).Take(3)) is translated into a $limit inside the correlated $lookup sub-pipeline, so it runs once per outer document instead of once globally before the join. Result: wrong row counts (per-outer-key top-N instead of global top-N). Introduced in CSHARP-6017 (PR #2017, 3.10.0), which added subquery-inner support for LeftJoin/includes but pushed all inner stages below the join correlation. GroupJoin is not silently wrong today only because it rejects subquery inners entirely (ExpressionNotSupportedException).

      Root cause

      JoinMethodToPipelineTranslator places the translated inner pipeline into the concise $lookup form that carries localField/foreignField. That correlation is applied before the sub-pipeline, so membership-changing stages see a per-outer-key subset. Membership-preserving stages ($match/$sort/$project/…) are unaffected; cardinality stages ($limit/$skip/$group/…) are not.

            Assignee:
            Damien Guard
            Reporter:
            Damien Guard
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: