Skip to content

Conversation

@rupprecht
Copy link
Contributor

Rationale for this change

When a std::multimap has multiple entries with the same key, calling m.find(key) returns an unspecified element.

Historically, this returns the first matching element. However, this is not guaranteed, and recent libc++ changes make this return an arbitrary element. This can lead to surprising behavior, i.e. different values returned based on what other entries are in the multimap (and what the shape of the internal tree is).

What changes are included in this PR?

Replace m.find(key) with m.equal_range(key) to preserve behavior and have predictable results. The behavior of this is guaranteed to return a range of all matching elements in insertion order, and the beginning of the range is the same element as what's normally returned by m.find(key).

Are these changes tested?

Yes

Are there any user-facing changes?

No

Copy link
Member

@lidavidm lidavidm left a comment

Choose a reason for hiding this comment

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

Thanks!

@github-actions github-actions bot added awaiting merge Awaiting merge and removed awaiting review Awaiting review labels Dec 21, 2025
@lidavidm
Copy link
Member

It appears this needs to be formatted:

--- a/cpp/src/arrow/flight/transport/grpc/util_internal.cc
+++ b/cpp/src/arrow/flight/transport/grpc/util_internal.cc
@@ -55,30 +55,25 @@ static bool FromGrpcContext(const ::grpc::ClientContext& ctx,
   const std::multimap<::grpc::string_ref, ::grpc::string_ref>& trailers =
       ctx.GetServerTrailingMetadata();
 
-  const auto [code_val_begin, code_val_end] =
-      trailers.equal_range(kGrpcStatusCodeHeader);
+  const auto [code_val_begin, code_val_end] = trailers.equal_range(kGrpcStatusCodeHeader);
   if (code_val_begin == code_val_end) return false;
 
   std::optional<std::string> message;
-  if (const auto [it, end] = trailers.equal_range(kGrpcStatusMessageHeader);
-      it != end) {
+  if (const auto [it, end] = trailers.equal_range(kGrpcStatusMessageHeader); it != end) {
     message = std::string(it->second.data(), it->second.size());
   }
 
   std::optional<std::string> detail_message;
-  if (const auto [it, end] = trailers.equal_range(kGrpcStatusDetailHeader);
-      it != end) {
+  if (const auto [it, end] = trailers.equal_range(kGrpcStatusDetailHeader); it != end) {
     detail_message = std::string(it->second.data(), it->second.size());
   }
 
   std::optional<std::string> detail_bin;
-  if (const auto [it, end] = trailers.equal_range(kBinaryErrorDetailsKey);
-      it != end) {
+  if (const auto [it, end] = trailers.equal_range(kBinaryErrorDetailsKey); it != end) {
     detail_bin = std::string(it->second.data(), it->second.size());
   }
 
-  std::string code_str(code_val_begin->second.data(),
-                       code_val_begin->second.size());
+  std::string code_str(code_val_begin->second.data(), code_val_begin->second.size());
   *status = internal::ReconstructStatus(code_str, current_status, std::move(message),
                                         std::move(detail_message), std::move(detail_bin),
                                         std::move(flight_status_detail));

@rupprecht
Copy link
Contributor Author

It appears this needs to be formatted:

Oops, fixed.

@lidavidm lidavidm merged commit 8040f2a into apache:main Dec 23, 2025
45 checks passed
@lidavidm lidavidm removed the awaiting merge Awaiting merge label Dec 23, 2025
@conbench-apache-arrow
Copy link

After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 8040f2a.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants