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: afe7a03

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

Benchmark Measurement % Change Patch Value Stable Region H-Score Z-Score
BenchmarkMultiFindMany total_bytes_allocated -14.4631 644322504.0000 Avg: 753268664.2759
Med: 762006024.0000
Stdev: 51179960.9079
0.7420 -2.1287
BenchmarkMultiFindMany total_mem_allocs -14.3829 397192.0000 Avg: 463916.8966
Med: 468895.0000
Stdev: 31471.8129
0.7402 -2.1201
BenchmarkBSONDeepDocumentDecoding total_mem_allocs -2.8213 12786414.0000 Avg: 13157634.9524
Med: 13200636.0000
Stdev: 146890.2456
0.7823 -2.5272
BenchmarkBSONDeepDocumentDecoding total_bytes_allocated -2.8135 277814256.0000 Avg: 285856726.4762
Med: 286791096.0000
Stdev: 3182806.7075
0.7822 -2.5268
BenchmarkMultiFindMany ops_per_second_med -2.6422 3636363.6364 Avg: 3735050.9578
Med: 3731343.2836
Stdev: 39010.8300
0.7833 -2.5297
BenchmarkMultiFindMany ops_per_second_max -2.3340 4149377.5934 Avg: 4248536.7696
Med: 4255319.1489
Stdev: 27973.7336
0.8539 -3.5447
BenchmarkBSONDeepDocumentDecoding total_time_seconds -1.5221 1.1809 Avg: 1.1992
Med: 1.1989
Stdev: 0.0057
0.8455 -3.2040
BenchmarkBSONDeepDocumentDecoding allocated_bytes_per_op 0.0120 15101.0000 Avg: 15099.1905
Med: 15099.0000
Stdev: 0.5118
0.8747 3.5358

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

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.

1 participant