[SERVER-56719] Short circuit $switch when any condition is constant Created: 06/May/21  Updated: 29/Oct/23  Resolved: 08/Jul/21

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

Type: Improvement Priority: Major - P3
Reporter: David Percy Assignee: Naama Bareket
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Problem/Incident
Related
related to SERVER-70190 ExpressionSwitch::optimize() can leav... Closed
is related to SERVER-55297 Short circuit $cond if condition is a... Closed
Backwards Compatibility: Fully Compatible
Backport Requested:
v5.2, v4.4, v4.2
Sprint: Query Optimization 2021-06-14, Query Optimization 2021-06-28, Query Optimization 2021-07-12
Participants:
Linked BF Score: 195

 Description   

There are several rules we can add to $switch to improve its constant folding:

  • When a case is a constant, and false, we can remove that case.
  • When a case is a constant, and true, we can turn that case into the default, and remove all subsequent cases.
  • When there are no cases, we can replace the $switch with its default.

Alternatively, we could desugar $switch to chain of $cond expressions; then SERVER-55297 would have the same effect as the above three rules. This approach makes the rewrites simpler but results in more calls to evaluate().

Either option would let $bucket take advantage of a $group-by-constant optimization, since $bucket desugars to $group + $switch. (Although I don't think we have a $group-by-constant optimization currently.)



 Comments   
Comment by Sviatlana Zuiko [ 03/Feb/22 ]

Sorry, it was my bad, I got confused. Declined BACKPORT-11790.

Comment by Mohammad Dashti (Inactive) [ 02/Feb/22 ]

sviatlana.zuiko wasn't this applied to 5.1? Then, it should be already in 5.2, right?

Comment by Sviatlana Zuiko [ 02/Feb/22 ]

Requesting a backport to 5.2 because of new failure occurrence there https://buildbaron.corp.mongodb.com/ui/#/bfg/BFG-1038308

Comment by Vivian Ge (Inactive) [ 06/Oct/21 ]

Updating the fixversion since branching activities occurred yesterday. This ticket will be in rc0 when it’s been triggered. For more active release information, please keep an eye on #server-release. Thank you!

Comment by Githook User [ 07/Jul/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit $switch when any condition is constant
Branch: master
https://github.com/mongodb/mongo/commit/25e6b31298e882b06b4afb6c72ada5d22e9aaeec

Comment by Mohammad Dashti (Inactive) [ 02/Jul/21 ]

Based on BF-21717, this needs to be back-ported to v4.2 and v4.4 to avoid fuzzer failures.

Comment by Githook User [ 30/Jun/21 ]

Author:

{'name': 'samontea', 'email': 'merciers.merciers@gmail.com', 'username': 'samontea'}

Message: Revert "SERVER-56719 Short circuit $switch when any condition is constant"
Branch: master
https://github.com/mongodb/mongo/commit/fa074097ba7d4ad79b0ad5f7c77b00b0af4c8a8e

Comment by Githook User [ 29/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit $switch when any condition is constant
Branch: master
https://github.com/mongodb/mongo/commit/43c655b0bc42d2ddf4bed061b60bbf7c1c9942e4

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/85f3203de63173dcf4013556c392164fe6d3b827

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/e7b844883776f82a3a49bf141f12951474983af0

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/0d2ca784072ba211e7915e61dea070fc716f3a19

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/db64e1703a0a6c65dd9ccc2761849ad11bd1513d

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/d6e7794eedf4cc5103616147957f491c4c134530

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/00b81d67efbbdcff030128f5fecb23f1586fa27e

Comment by Githook User [ 23/Jun/21 ]

Author:

{'name': 'Naama Bareket', 'email': 'naama.bareket@mongodb.com', 'username': 'naama-bareket'}

Message: SERVER-56719 Short circuit when any condition is constant
Branch: SERVER-56719
https://github.com/mongodb/mongo/commit/127c82d8726075c8cbd6e5604e75084d907162e6

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