Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): add spanner::Value support for TypeCode::FLOAT32 #13862

Merged
merged 2 commits into from Mar 28, 2024

Conversation

devbww
Copy link
Collaborator

@devbww devbww commented Mar 27, 2024

Represented as a float in C++, so no need for a wrapper class.


This change is Reviewable

Represented as a `float` in C++, so no need for a wrapper class.
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Mar 27, 2024
Copy link

codecov bot commented Mar 27, 2024

Codecov Report

Attention: Patch coverage is 98.19820% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 93.65%. Comparing base (77bbdeb) to head (ae32a7a).
Report is 3 commits behind head on main.

Files Patch % Lines
google/cloud/spanner/value.cc 92.85% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #13862      +/-   ##
==========================================
+ Coverage   93.05%   93.65%   +0.60%     
==========================================
  Files        2176     2263      +87     
  Lines      184982   195424   +10442     
==========================================
+ Hits       172126   183015   +10889     
+ Misses      12856    12409     -447     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@devbww devbww marked this pull request as ready for review March 27, 2024 21:10
@devbww devbww requested a review from a team as a code owner March 27, 2024 21:10
@@ -535,6 +560,22 @@ StatusOr<std::int64_t> Value::GetValue(std::int64_t,
return x;
}

StatusOr<float> Value::GetValue(float, google::protobuf::Value const& pv,
google::spanner::v1::Type const&) {
if (pv.kind_case() == google::protobuf::Value::kNumberValue) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to worry about loss of precision or anything like that? If not, maybe a comment explaining why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't believe we have anything to worry about in practice. We do have the (perhaps less than enlightening "narrowing" comment, due to the decision upstream to re-use google.protobuf.Value { double number_value } for the wire format. We already assert that a C++ double can represent such a value, and now we also assert that float -> double [-> proto-double -> double] -> float is an unsurprising conversion path. If it is surprising (or if it can't even be done in a language), they prescribe using the native 64-bit floating-point type to represent FLOAT32.

Copy link
Member

Choose a reason for hiding this comment

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

Ack. Just consider a more enlightening comment, I read it and thought "yes, it is narrowing, but why is it Okay in this case?" Would be nice if we could succinctly answer that question.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

}
std::string const& s = pv.string_value();
auto const inf = std::numeric_limits<float>::infinity();
if (s == "-Infinity") return -inf;
Copy link
Member

Choose a reason for hiding this comment

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

Just to show that I fear floating point, but I don't understand it: I think C++ does not guarantee that - std::numeric_limits<float>::infinity() yields a valid value, or that ::infinity() even exists. OTOH, on the platforms we could possibly care about, those things don't matter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could try to (statically?) assert std::numeric_limits<float>::has_infinity() somewhere, or otherwise require IEEE 754 support. Otherwise, I believe we'll at least get HUGE_VALF from infinity(), and so use it for "Infinity" on such a platform.

Copy link
Member

Choose a reason for hiding this comment

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

Optional: add the static assert. But it could get repetitive if we do it in each place we use that assumption, your call.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done (in a single place). Also added one for double.

@devbww devbww merged commit 8af53cf into googleapis:main Mar 28, 2024
62 checks passed
@devbww devbww deleted the float32-value branch March 28, 2024 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants