tryPushdownProjectBeneathSort incorrectly blocks project-below-sort optimisation for meta expression sorts

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Optimization
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      QueryPlannerAnalysis::tryPushdownProjectBeneathSort checks whether a projection preserves all sort fields before pushing it below a sort. However, meta sort components (e.g. {$meta: "randVal"}, {}{$meta: "geoNearDistance"}{}) don't read document fields, the field name is a syntactic placeholder, so the projection check is irrelevant and the optimisation is always safe. As a result, queries like db.coll.find({}, {_id: 1}).sort({p: {$meta: "randVal")}} produce PROJECTION + SORT + COLLSCAN instead of the optimal SORT + PROJECTION + COLLSCAN. The fix is to skip any non-numeric sort component in the legality check. No correctness impact, both plan shapes return the same results. A QueryPlannerTest unit test and JS integration test using explain() should be added to guard the regression.

            Assignee:
            Unassigned
            Reporter:
            Alexander Ignatyev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: