alinaliBQ commented on code in PR #40939: URL: https://github.com/apache/arrow/pull/40939#discussion_r2380440051
########## cpp/src/arrow/flight/sql/odbc/flight_sql/json_converter.cc: ########## @@ -0,0 +1,326 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/flight/sql/odbc/flight_sql/json_converter.h" + +#include <rapidjson/rapidjson.h> +#include <rapidjson/writer.h> +#include <boost/beast/core/detail/base64.hpp> +#include "arrow/builder.h" +#include "arrow/flight/sql/odbc/flight_sql/utils.h" +#include "arrow/scalar.h" +#include "arrow/visitor.h" + +using arrow::Status; + +using boost::beast::detail::base64::encode; +using boost::beast::detail::base64::encoded_size; +namespace base64 = boost::beast::detail::base64; + +using driver::flight_sql::ThrowIfNotOK; + +namespace { +template <typename ScalarT> +Status ConvertScalarToStringAndWrite(const ScalarT& scalar, + rapidjson::Writer<rapidjson::StringBuffer>& writer) { + ARROW_ASSIGN_OR_RAISE(auto string_scalar, scalar.CastTo(arrow::utf8())) + const auto& view = reinterpret_cast<arrow::StringScalar*>(string_scalar.get())->view(); + writer.String(view.data(), view.length(), true); + return Status::OK(); +} + +template <typename BinaryScalarT> +Status ConvertBinaryToBase64StringAndWrite( + const BinaryScalarT& scalar, rapidjson::Writer<rapidjson::StringBuffer>& writer) { + const auto& view = scalar.view(); + size_t encoded_size = base64::encoded_size(view.length()); + std::vector<char> encoded(std::max(encoded_size, static_cast<size_t>(1))); + base64::encode(&encoded[0], view.data(), view.length()); + writer.String(&encoded[0], encoded_size, true); + return Status::OK(); +} + +template <typename ListScalarT> +Status WriteListScalar(const ListScalarT& scalar, + rapidjson::Writer<rapidjson::StringBuffer>& writer, + arrow::ScalarVisitor* visitor) { + writer.StartArray(); + for (int64_t i = 0; i < scalar.value->length(); ++i) { + if (scalar.value->IsNull(i)) { + writer.Null(); + } else { + const auto& result = scalar.value->GetScalar(i); + ThrowIfNotOK(result.status()); + ThrowIfNotOK(result.ValueOrDie()->Accept(visitor)); + } + } + + writer.EndArray(); + return Status::OK(); +} + +class ScalarToJson : public arrow::ScalarVisitor { + private: + rapidjson::StringBuffer string_buffer_; + rapidjson::Writer<rapidjson::StringBuffer> writer_{string_buffer_}; + + public: + void Reset() { + string_buffer_.Clear(); + writer_.Reset(string_buffer_); + } + + std::string ToString() { return string_buffer_.GetString(); } + + Status Visit(const arrow::NullScalar& scalar) override { + writer_.Null(); + + return Status::OK(); + } + + Status Visit(const arrow::BooleanScalar& scalar) override { + writer_.Bool(scalar.value); + + return Status::OK(); + } + + Status Visit(const arrow::Int8Scalar& scalar) override { + writer_.Int(scalar.value); + + return Status::OK(); + } + + Status Visit(const arrow::Int16Scalar& scalar) override { + writer_.Int(scalar.value); + + return Status::OK(); + } + + Status Visit(const arrow::Int32Scalar& scalar) override { + writer_.Int(scalar.value); + + return Status::OK(); + } + + Status Visit(const arrow::Int64Scalar& scalar) override { + writer_.Int64(scalar.value); Review Comment: I see from tests, rapidJSON can still convert the max and min int64 to strings, so rapidJSON seems to support all numbers within the range of (9223372036854775807,-9223372036854775808). For example the following test works: https://github.com/apache/arrow/blob/00439d104804fca889016d054c03ff6ba9d5560f/cpp/src/arrow/flight/sql/odbc/flight_sql/json_converter_test.cc#L76-L82 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
