[GODRIVER-2451] int32 is converted to int64 after incrementing Created: 05/Jun/22  Updated: 27/Oct/23  Resolved: 30/Jun/22

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Pierre Depretz Assignee: Benji Rewis (Inactive)
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image+(3).png     PNG File image+(4).png     PNG File image+(5).png     PNG File image+(6).png     PNG File image+(7).png     PNG File image+(8).png     File increment.go    
Case:

 Description   

Summary

A user is trying to use the $inc operator to increment by a small number (uint32) an int32 type. This operation makes the field be converted into int64. This snippet should return an int32 as mentioned in the comment ("If ec.MinSize or if encodeToMinSize is true for a non-uint64 value we should write val as an int32") but since useMinSize is false in his case, an int64 is returned. 

Therefore the main question is, how to force useMinSize to true?

It's also worth noting that:

  • EncodeToMinSize returns false in his case despite no custom codec (uintcodec)
  • Therefore he uses the default encoder (defaultUIntCodec) here
  • Default encoder is created here ... which does not pass in any options so EncodeToMinSize gets the Golang zero value for bool which is false.

How to Reproduce

User images are attached



 Comments   
Comment by PM Bot [ 30/Jun/22 ]

There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information.

Comment by Benji Rewis (Inactive) [ 15/Jun/22 ]

Hello pierre.depretz@mongodb.com! Thanks for your bug report, and apologies for the late response. The Go driver team was at MongoDB world last week, and I just got a chance to take a deeper look at this.

Firstly, I was incorrect in my earlier statements on Slack. The default BSON registry in the Go driver registers a UIntCodec with EncodeToMinSize set to false by default. So, by default, any uint32 encoded by our BSON library will be written as an Int64. I'm not sure I agree with this behavior (it seems odd to me that we wouldn't default to using the minimum size), so I've filed GODRIVER-2460. Unfortunately, we cannot change the default behavior without breaking current users' code, so that potential change will have to wait until version 2.0 of the driver.

I believe what's happening in your example is that the uint32 value you're passing to $inc is being encoded as an Int64 in BSON. When the existing age field is incremented by an Int64, its type switches to Int64 from Int32. I would suggest specifying a custom Registry with a custom UIntCodec in your Client creation. Here's an example of what that might look like:

increment.go

Let me know if that workaround is helpful, and if not, I may have alternative suggestions.

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