indigophox commented on code in PR #34817: URL: https://github.com/apache/arrow/pull/34817#discussion_r1397934196
########## cpp/src/arrow/flight/types.cc: ########## @@ -468,6 +468,320 @@ arrow::Result<CancelFlightInfoRequest> CancelFlightInfoRequest::Deserialize( return out; } +std::ostream& operator<<(std::ostream& os, const SetSessionOptionStatus& r) { + os << SetSessionOptionStatusNames[static_cast<int>(r)]; + return os; +} + +std::ostream& operator<<(std::ostream& os, const CloseSessionStatus& r) { + os << CloseSessionStatusNames[static_cast<int>(r)]; + return os; +} + +// Helpers for stringifying maps containing various types +std::ostream& operator<<(std::ostream& os, std::vector<std::string> v) { + os << '['; + std::string sep = ""; + for (const auto& x : v) { + os << sep << '"' << x << '"'; + sep = ", "; + } + os << ']'; + + return os; +} + +std::ostream& operator<<(std::ostream& os, const SessionOptionValue& v) { + std::visit([&](const auto& x) { os << x; }, v); + return os; +} + +std::ostream& operator<<(std::ostream& os, const SetSessionOptionsResult::Result& r) { + os << '{' << r.status << '}'; + return os; +} + +template <typename T> +std::ostream& operator<<(std::ostream& os, std::map<std::string, T> m) { + os << '{'; + std::string sep = ""; + for (const auto& [k, v] : m) { + os << sep << '[' << k << "]: '" << v; + sep = ", "; + } + os << '}'; + + return os; +} + +namespace { +static bool CompareSessionOptionMaps(const std::map<std::string, SessionOptionValue>& a, + const std::map<std::string, SessionOptionValue>& b) { + if (a.size() != b.size()) { + return false; + } + for (const auto& [k, v] : a) { + if (const auto it = b.find(k); it == b.end()) { + return false; + } else { + const auto& b_v = it->second; + if (v.index() != b_v.index()) { + return false; + } + if (v != b_v) { + return false; + } + } + } + return true; +} +} // namespace + +// SetSessionOptionsRequest + +std::string SetSessionOptionsRequest::ToString() const { + std::stringstream ss; + + ss << "<SetSessionOptionsRequest session_options=" << session_options << '>'; + + return ss.str(); +} + +bool SetSessionOptionsRequest::Equals(const SetSessionOptionsRequest& other) const { + return CompareSessionOptionMaps(session_options, other.session_options); +} + +arrow::Result<std::string> SetSessionOptionsRequest::SerializeToString() const { + pb::SetSessionOptionsRequest pb_request; + RETURN_NOT_OK(internal::ToProto(*this, &pb_request)); + + std::string out; + if (!pb_request.SerializeToString(&out)) { + return Status::IOError("Serialized SetSessionOptionsRequest exceeded 2GiB limit"); + } + return out; +} + +arrow::Result<SetSessionOptionsRequest> SetSessionOptionsRequest::Deserialize( + std::string_view serialized) { + // TODO these & SerializeToString should all be factored out to a superclass + pb::SetSessionOptionsRequest pb_request; + if (serialized.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { + return Status::Invalid( + "Serialized SetSessionOptionsRequest size should not exceed 2 GiB"); + } + google::protobuf::io::ArrayInputStream input(serialized.data(), + static_cast<int>(serialized.size())); + if (!pb_request.ParseFromZeroCopyStream(&input)) { + return Status::Invalid("Not a valid SetSessionOptionsRequest"); + } + SetSessionOptionsRequest out; + RETURN_NOT_OK(internal::FromProto(pb_request, &out)); + return out; +} + +// SetSessionOptionsResult + +std::string SetSessionOptionsResult::ToString() const { + std::stringstream ss; + + ss << "<SetSessionOptionsResult results=" << results << '>'; + + return ss.str(); +} + +bool SetSessionOptionsResult::Equals(const SetSessionOptionsResult& other) const { + if (results != other.results) { + return false; + } + return true; +} + +arrow::Result<std::string> SetSessionOptionsResult::SerializeToString() const { + pb::SetSessionOptionsResult pb_result; + RETURN_NOT_OK(internal::ToProto(*this, &pb_result)); + + std::string out; + if (!pb_result.SerializeToString(&out)) { + return Status::IOError("Serialized SetSessionOptionsResult exceeded 2GiB limit"); + } + return out; +} + +arrow::Result<SetSessionOptionsResult> SetSessionOptionsResult::Deserialize( + std::string_view serialized) { + pb::SetSessionOptionsResult pb_result; + if (serialized.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { + return Status::Invalid( + "Serialized SetSessionOptionsResult size should not exceed 2 GiB"); + } + google::protobuf::io::ArrayInputStream input(serialized.data(), + static_cast<int>(serialized.size())); + if (!pb_result.ParseFromZeroCopyStream(&input)) { + return Status::Invalid("Not a valid SetSessionOptionsResult"); + } + SetSessionOptionsResult out; + RETURN_NOT_OK(internal::FromProto(pb_result, &out)); + return out; +} + +// GetSessionOptionsRequest + +std::string GetSessionOptionsRequest::ToString() const { + return "<GetSessionOptionsRequest>"; +} + +bool GetSessionOptionsRequest::Equals(const GetSessionOptionsRequest& other) const { + return true; +} + +arrow::Result<std::string> GetSessionOptionsRequest::SerializeToString() const { + pb::GetSessionOptionsRequest pb_request; + RETURN_NOT_OK(internal::ToProto(*this, &pb_request)); + + std::string out; + if (!pb_request.SerializeToString(&out)) { + return Status::IOError("Serialized GetSessionOptionsRequest exceeded 2GiB limit"); + } + return out; +} + +arrow::Result<GetSessionOptionsRequest> GetSessionOptionsRequest::Deserialize( + std::string_view serialized) { + pb::GetSessionOptionsRequest pb_request; + if (serialized.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { + return Status::Invalid( + "Serialized GetSessionOptionsRequest size should not exceed 2 GiB"); + } + google::protobuf::io::ArrayInputStream input(serialized.data(), + static_cast<int>(serialized.size())); + if (!pb_request.ParseFromZeroCopyStream(&input)) { + return Status::Invalid("Not a valid GetSessionOptionsRequest"); + } + GetSessionOptionsRequest out; + RETURN_NOT_OK(internal::FromProto(pb_request, &out)); + return out; +} + +// GetSessionOptionsResult + +std::string GetSessionOptionsResult::ToString() const { + std::stringstream ss; + + ss << "<GetSessionOptionsResult session_options=" << session_options << '>'; + + return ss.str(); +} + +bool GetSessionOptionsResult::Equals(const GetSessionOptionsResult& other) const { + return CompareSessionOptionMaps(session_options, other.session_options); +} + +arrow::Result<std::string> GetSessionOptionsResult::SerializeToString() const { + pb::GetSessionOptionsResult pb_result; + RETURN_NOT_OK(internal::ToProto(*this, &pb_result)); + + std::string out; + if (!pb_result.SerializeToString(&out)) { + return Status::IOError("Serialized GetSessionOptionsResult exceeded 2GiB limit"); + } + return out; +} + +arrow::Result<GetSessionOptionsResult> GetSessionOptionsResult::Deserialize( + std::string_view serialized) { + pb::GetSessionOptionsResult pb_result; + if (serialized.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { + return Status::Invalid( + "Serialized GetSessionOptionsResult size should not exceed 2 GiB"); + } + google::protobuf::io::ArrayInputStream input(serialized.data(), + static_cast<int>(serialized.size())); + if (!pb_result.ParseFromZeroCopyStream(&input)) { + return Status::Invalid("Not a valid GetSessionOptionsResult"); + } + GetSessionOptionsResult out; + RETURN_NOT_OK(internal::FromProto(pb_result, &out)); + return out; +} + +// CloseSessionRequest + +std::string CloseSessionRequest::ToString() const { return "<CloseSessionRequest>"; } + +bool CloseSessionRequest::Equals(const CloseSessionRequest& other) const { return true; } + +arrow::Result<std::string> CloseSessionRequest::SerializeToString() const { + pb::CloseSessionRequest pb_request; + RETURN_NOT_OK(internal::ToProto(*this, &pb_request)); + + std::string out; + if (!pb_request.SerializeToString(&out)) { + return Status::IOError("Serialized CloseSessionRequest exceeded 2GiB limit"); + } + return out; +} + +arrow::Result<CloseSessionRequest> CloseSessionRequest::Deserialize( + std::string_view serialized) { + pb::CloseSessionRequest pb_request; + if (serialized.size() > static_cast<size_t>(std::numeric_limits<int>::max())) { + return Status::Invalid("Serialized CloseSessionRequest size should not exceed 2 GiB"); + } + google::protobuf::io::ArrayInputStream input(serialized.data(), + static_cast<int>(serialized.size())); + if (!pb_request.ParseFromZeroCopyStream(&input)) { + return Status::Invalid("Not a valid CloseSessionRequest"); + } + CloseSessionRequest out; + RETURN_NOT_OK(internal::FromProto(pb_request, &out)); + return out; +} + +// CloseSessionResult + +std::string CloseSessionResult::ToString() const { + std::stringstream ss; + + ss << "<CloseSessionResult result=" << status << '>'; + + return ss.str(); +} + +bool CloseSessionResult::Equals(const CloseSessionResult& other) const { + if (status != other.status) { + return false; + } + return true; Review Comment: This message may get extended in future (with human-readable text which may or may not get compared(!)) but fair enough for now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org