[SERVER-25823] Add bitwise AND, OR, XOR to aggregation pipeline Created: 26/Aug/16  Updated: 11/May/23  Resolved: 06/Dec/22

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 6.3.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Adinoyi Omuya Assignee: Davis Haupt (Inactive)
Resolution: Done Votes: 10
Labels: BIC, bitwise, expression, query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Documented
Related
is related to SERVER-3518 Bitwise query operators Closed
Backwards Compatibility: Fully Compatible
Sprint: QO 2022-12-12
Participants:

 Description   

We already have support for bitwise operators - and, or, xor, complement - in both the query and update paths. It would be nice to have this in aggregation as well.

This would allow users run pipelines like:

db.foo.aggregate({
  "$project": {
    "flipped": {
      "$bitComplement": "$a"
    }
  }
})
db.foo.aggregate({
  "$project": {
    "xored": {
      "$bitXor": [
        NumberInt("10"),
        "$a",
        1,
      ]
    }
  }
})



 Comments   
Comment by Githook User [ 05/Dec/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-25823 add bitwise AND, OR, XOR to the agg language
Branch: master
https://github.com/mongodb/mongo/commit/1040c746da55b24f02f3f9715e91c57aeeccc8e6

Comment by Githook User [ 05/Dec/22 ]

Author:

{'name': 'Davis Haupt', 'email': 'davis.haupt@mongodb.com', 'username': 'davish'}

Message: SERVER-25823 add bitwise AND, OR, XOR to the agg language
Branch: master
https://github.com/10gen/mongo-enterprise-modules/commit/8635df3d76975100b88ec50da0745a917de0b203

Comment by Davis Haupt (Inactive) [ 17/Nov/22 ]

Changed the title of this ticket to reflect the work to add AND, OR, and XOR to the agg language. Work for bitwise NOT will be completed in SERVER-71450.

Comment by Davis Haupt (Inactive) [ 17/May/22 ]

Moving this ticket to be blocked on the syntax review.

Comment by Davis Haupt (Inactive) [ 05/May/22 ]

This PR, unlike the previous PR, only implements bitwise and, or, and xor to reach feature parity with update and match. Other operations, like bitshifting, have not been implemented since they're not yet present in any part of the database. We can file a new ticket if that functionality is still desired.

Comment by Davis Haupt (Inactive) [ 05/May/22 ]

Syntax and semantics for the most recent PR:

  • Unlike the update $bit operator, there are separate agg expressions for $bitAnd, $bitOr, $bitXor. They are all variadic operators that can take in any number of expressions that evaluate to NumberInt or NumberLong.
  • They use the same type widening logic that is present in the update path, so that ints are casted to longs before any bitwise operation is performed on them.
  • The operators throw an error if unsupported types like Doubles are encountered.
  • The default value for each operator is the identity value for each operator. This means that if the operator list is empty, the default value is returned. This value is -1 for $bitAnd (all ones in a signed integer) and 0 for $bitOr and $bitXor.
  • If there is any nullish argument in the operand list, then the whole expression evaluates to null.
Comment by Patrick Meredith [ 27/Oct/18 ]

Code Review: https://mongodbcr.appspot.com/226050001/

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