[SERVER-79096] Simplified constant folding and ref tracking under interval simplification Created: 19/Jul/23  Updated: 29/Oct/23  Resolved: 03/Oct/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.2.0-rc0

Type: Task Priority: Major - P3
Reporter: Timour Katchaounov Assignee: Timour Katchaounov
Resolution: Fixed Votes: 0
Labels: M1
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Backwards Compatibility: Fully Compatible
Sprint: QO 2023-10-02, QO 2023-10-16
Participants:

 Description   

Interval simplification relies on constant folding, which uses heavily reference tracking.

Taking into account that interval boundaries are expressions of a specific form, both constant folding and its use of reference tracking can be simplified/optimized to improve optimization performance.

svilen.mihaylov@mongodb.com david.percy@mongodb.com I remember discussing some ideas with you, could you please add some notes here?



 Comments   
Comment by Githook User [ 03/Oct/23 ]

Author:

{'name': 'Timour Katchaounov', 'email': 'timour.katchaounov@mongodb.com', 'username': 'timourk'}

Message: SERVER-79096 Simplified constant folding and ref tracking under interval simplification

Before this commit, all comparisons of ABT interval bounds were done via constant folding. However, the input intervals' bounds are already constant folded before we get to do interval simplification. This commit replaces all "simple" comparisons of ABT interval bounds so that instead of going through constant folding, the comparisons use directly SBE value comparison.
Branch: master
https://github.com/mongodb/mongo/commit/23fbef8fb12d2eb4dba6080fc4f69fe278365689

Comment by David Percy [ 19/Jul/23 ]

In 'intersectIntervals' we are constant-folding these expressions that come from the input: https://github.com/10gen/mongo/blob/a0866639120c8bdce3eff8c446ea70e982ce1839/src/mongo/db/query/optimizer/utils/interval_utils.cpp#L442-L443 But we're already constant-folding when we construct an interval, so we can do a simple 'is<Constant>()' check when we read the interval. This would also avoid a copy.

In the same function, we're using constant folding to compare two expressions with Eq: https://github.com/10gen/mongo/blob/a0866639120c8bdce3eff8c446ea70e982ce1839/src/mongo/db/query/optimizer/utils/interval_utils.cpp#L459-L461. We want to only get 'true' if both arguments are constants, and are equal according to Eq. Instead of constant-folding we could do a more specialized thing where we check 'is<Contant>()' for the arguments and then call the SBE function that implements Eq.

A similar thing happens in 'cmpABT':

// Three way comparison of the two arguments. Returns boost::none if nothing can be determined
// about the comparison between the two arguments.
static boost::optional<int> cmpABT(const ABT& a1, const ABT& a2, const ConstFoldFn& constFold) {

https://github.com/10gen/mongo/blob/a0866639120c8bdce3eff8c446ea70e982ce1839/src/mongo/db/query/optimizer/utils/interval_utils.cpp#L57-L59

We can change this to use 'is<Constant>()' and call SBE's 3-way comparison. The behavior would change for inputs that are constant-foldable but not Constant nodes; that's fine because we can move that responsibility to the caller to make sure the inputs are simplified.

Generated at Thu Feb 08 06:40:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.