Skip to content

Conversation

@RafaelCenzano
Copy link

GODRIVER-3594

Summary

This PR adds AsFloat64() and AsFloat64OK() functions to handle conversions of BSON values into float64 values

Background & Motivation

Matches functions that exist currently for int conversions. It also allows the removal of using an BSON to int conversion and then a cast to float64. This allows floating point values whereas before values were converted to integers and would lose decimal values.

@mongodb-drivers-pr-bot
Copy link
Contributor

mongodb-drivers-pr-bot bot commented Dec 3, 2025

🧪 Performance Results

Commit SHA: 2f375c2

The following benchmark tests for version 69330634b6de26000712f43d had statistically significant changes (i.e., |z-score| > 1.96):

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkLargeDocInsertOne ops_per_second_min -49.3266 767.0818 Avg: 1513.7770
Med: 1570.8614
Stdev: 280.4358
0.8027 -2.6626
BenchmarkBSONFullDocumentDecoding ops_per_second_min -19.3999 1563.4283 Avg: 1939.7352
Med: 1940.3234
Stdev: 172.0859
0.7590 -2.1867
BenchmarkSingleRunCommand ns_per_op 11.8945 154178.0000 Avg: 137788.7742
Med: 134756.0000
Stdev: 8270.2438
0.7435 1.9817
BenchmarkSingleFindOneByID ops_per_second_med -10.8345 3690.7996 Avg: 4139.2703
Med: 4174.3283
Stdev: 161.6329
0.8081 -2.7746
BenchmarkSingleFindOneByID ns_per_op 10.6449 274669.0000 Avg: 248243.6774
Med: 245843.0000
Stdev: 9911.3638
0.7988 2.6662
BenchmarkMultiInsertLargeDocument ns_per_op 8.7095 32187710.0000 Avg: 29608930.1000
Med: 29448155.5000
Stdev: 1038948.4505
0.7781 2.4821
BenchmarkSingleFindOneByID ops_per_second_max -5.6686 4430.0713 Avg: 4696.2869
Med: 4715.3797
Stdev: 132.3884
0.7349 -2.0109
BenchmarkSingleFindOneByID allocated_bytes_per_op -0.1765 22418.0000 Avg: 22457.6364
Med: 22453.0000
Stdev: 18.8594
0.7336 -2.1017

For a comprehensive view of all microbenchmark results for this PR's commit, please check out the Evergreen perf task for this patch.

@RafaelCenzano RafaelCenzano added review-priority-normal Medium Priority PR for Review: within 1 business day enhancement labels Dec 3, 2025
@mongodb-drivers-pr-bot
Copy link
Contributor

API Change Report

./v2/bson

compatible changes

RawValue.AsFloat64: added
RawValue.AsFloat64OK: added

./v2/x/bsonx/bsoncore

compatible changes

Value.AsFloat64: added
Value.AsFloat64OK: added

@RafaelCenzano RafaelCenzano marked this pull request as ready for review December 3, 2025 22:14
@RafaelCenzano RafaelCenzano requested a review from a team as a code owner December 3, 2025 22:14
Copilot finished reviewing on behalf of RafaelCenzano December 3, 2025 22:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds AsFloat64() and AsFloat64OK() conversion functions to handle BSON-to-float64 conversions, matching existing patterns for integer conversions. It also fixes a bug in the matches.go file where the wrong variable was being checked.

  • Implements AsFloat64() and AsFloat64OK() methods in bsoncore.Value with proper error handling for numeric types
  • Adds wrapper methods in RawValue to expose the new functionality
  • Refactors comparison logic to use the new methods instead of manual type checking and casting
  • Fixes a bug where assertionVal.Type was incorrectly checked instead of actual.Type

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
x/bsonx/bsoncore/value.go Implements core AsFloat64() and AsFloat64OK() methods for converting BSON numeric types to float64, and removes redundant variable declarations in AsInt64 methods
x/bsonx/bsoncore/value_test.go Adds comprehensive test cases for the new AsFloat64() and AsFloat64OK() methods covering success cases, error cases, and insufficient bytes scenarios
bson/raw_value.go Adds public wrapper methods AsFloat64() and AsFloat64OK() to expose the core functionality through the RawValue API
internal/integration/unified/matches.go Refactors numeric comparison logic to use new AsFloat64() method and fixes bug where assertionVal.Type was checked instead of actual.Type

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return f64, true
}

// Equal compaes v to v2 and returns true if they are equal.
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in the comment: "compaes" should be "compares".

Suggested change
// Equal compaes v to v2 and returns true if they are equal.
// Equal compares v to v2 and returns true if they are equal.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to the changes but simple non controversial fix, afe7a03

Comment on lines +297 to +303
// AsFloat64 returns a BSON number as a float64. If the BSON type is not a numeric one, this method
// will panic.
func (rv RawValue) AsFloat64() float64 { return convertToCoreValue(rv).AsFloat64() }

// AsFloat64OK is the same as AsFloat64, except that it returns a boolean instead of
// panicking.
func (rv RawValue) AsFloat64OK() (float64, bool) { return convertToCoreValue(rv).AsFloat64OK() }
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newly added AsFloat64() and AsFloat64OK() methods lack test coverage in bson/raw_value_test.go. Since other methods like AsInt64() and AsInt64OK() are tested through the underlying bsoncore.Value tests, and this file has existing test coverage, consider adding tests to verify the wrapper methods work correctly.

Copilot uses AI. Check for mistakes.
Copy link
Author

@RafaelCenzano RafaelCenzano Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qingyang-hu I will defer to your review on this, it doesn't seem like most of the functions that handle wrapping RawValue conversions are tested and it just converts the raw value then uses the tested AsType functions so in my opinion unless we want to test the raw value conversion for each conversion type it is probably unneeded

@matthewdale
Copy link
Collaborator

The govulncheck errors are not related to this PR and are resolved by #2255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement review-priority-normal Medium Priority PR for Review: within 1 business day

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants