github-actions[bot] commented on code in PR #68853:
URL: https://github.com/apache/doris/pull/68853#discussion_r4663958339


##########
be/src/core/data_type_serde/data_type_file_serde.cpp:
##########
@@ -0,0 +1,254 @@
+// 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 "core/data_type_serde/data_type_file_serde.h"
+
+#include <rapidjson/document.h>
+
+#include <algorithm>
+#include <array>
+#include <utility>
+
+#include "core/column/column_const.h"
+#include "core/column/column_file.h"
+#include "core/column/column_nullable.h"
+#include "core/custom_allocator.h"
+#include "core/data_type/data_type_file.h"
+#include "core/string_buffer.hpp"
+#include "util/jsonb_writer.h"
+#include "util/mysql_row_buffer.h"
+#include "util/url_coding.h"
+
+namespace doris {
+namespace {
+
+constexpr size_t FILE_FIELDS = DataTypeFile::FIELD_COUNT;
+constexpr size_t INLINE_FIELD_INDEX = DataTypeFile::INLINE_FIELD_INDEX;
+
+// Multiples of three keep padding confined to the final chunk. Scratch space 
is bounded
+// independently of the inline value's size.
+void write_base64(StringRef bytes, BufferWritable& bw) {
+    constexpr size_t INPUT_CHUNK = 3 * 1024;
+    std::array<unsigned char, INPUT_CHUNK / 3 * 4> output;
+    ColumnString::check_chars_length((bytes.size / 3 + (bytes.size % 3 != 0)) 
* 4 + 2, 1);
+    bw.write('"');
+    for (size_t offset = 0; offset < bytes.size; offset += INPUT_CHUNK) {
+        const auto length = std::min(INPUT_CHUNK, bytes.size - offset);
+        const auto written = base64_encode(
+                reinterpret_cast<const unsigned char*>(bytes.data + offset), 
length, output.data());
+        bw.write(reinterpret_cast<const char*>(output.data()), written);
+    }
+    bw.write('"');
+}
+
+} // namespace
+
+DataTypeFileSerDe::DataTypeFileSerDe(int nesting_level) : 
DataTypeSerDe(nesting_level) {
+    const DataTypeFile type;
+    _names = type.get_element_names();
+    for (const auto& element : type.get_elements()) {
+        _element_serdes.push_back(element->get_serde(nesting_level + 1));
+    }
+}
+
+void DataTypeFileSerDe::set_return_object_as_string(bool value) {
+    DataTypeSerDe::set_return_object_as_string(value);
+    for (auto& serde : _element_serdes) {
+        serde->set_return_object_as_string(value);
+    }
+}
+
+void DataTypeFileSerDe::_write_json(const IColumn& column, int64_t row, 
BufferWritable& bw) const {
+    const auto [data, index] = check_column_const_set_readability(column, row);
+    const auto& file = assert_cast<const ColumnFile&>(*data);
+    bw.write('{');
+    for (size_t i = 0; i < FILE_FIELDS; ++i) {
+        if (i != 0) {
+            bw.write(',');
+        }
+        bw.write_json_string(_names[i].data(), _names[i].size());
+        bw.write(':');
+        const auto& child = file.get_column(i);
+        if (child.is_null_at(index)) {
+            bw.write("null", 4);
+        } else if (i == DataTypeFile::OFFSET_FIELD_INDEX || i == 
DataTypeFile::SIZE_FIELD_INDEX) {
+            bw.write_number(child.get_nested_column().get_int(index));
+        } else {
+            const auto bytes = child.get_nested_column().get_data_at(index);
+            if (i == INLINE_FIELD_INDEX) {
+                write_base64(bytes, bw);
+            } else {
+                bw.write_json_string(bytes.data, bytes.size);
+            }
+        }
+    }
+    bw.write('}');
+}
+
+void DataTypeFileSerDe::to_string(const IColumn& column, size_t row_num, 
BufferWritable& bw,
+                                  const FormatOptions& options) const {
+    _write_json(column, row_num, bw);
+}
+
+bool DataTypeFileSerDe::write_column_to_mysql_text(const IColumn& column, 
BufferWritable& bw,
+                                                   int64_t row_idx,
+                                                   const FormatOptions& 
options) const {
+    to_string(column, row_idx, bw, options);
+    return true;
+}
+
+Status DataTypeFileSerDe::write_column_to_mysql_binary(const IColumn& column,
+                                                       MysqlRowBinaryBuffer& 
row_buffer,
+                                                       int64_t row_idx, bool 
col_const,
+                                                       const FormatOptions& 
options) const {
+    auto output = ColumnString::create();
+    BufferWritable bw(*output);
+    to_string(column, index_check_const(row_idx, col_const), bw, options);
+    bw.commit();
+    const auto json = output->get_data_at(0);
+    if (row_buffer.push_string(json.data, json.size) != 0) {
+        return Status::InternalError("Packing FILE into MySQL binary buffer 
failed");
+    }
+    return Status::OK();
+}
+
+Status DataTypeFileSerDe::serialize_one_cell_to_json(const IColumn& column, 
int64_t row_num,
+                                                     BufferWritable& bw,
+                                                     FormatOptions& options) 
const {
+    _write_json(column, row_num, bw);
+    return Status::OK();
+}
+
+Status DataTypeFileSerDe::serialize_column_to_json(const IColumn& column, 
int64_t start_idx,
+                                                   int64_t end_idx, 
BufferWritable& bw,
+                                                   FormatOptions& options) 
const {
+    SERIALIZE_COLUMN_TO_JSON();
+}
+
+Status DataTypeFileSerDe::deserialize_one_cell_from_json(IColumn& column, 
Slice& slice,
+                                                         const FormatOptions&) 
const {
+    rapidjson::Document document;
+    document.Parse(slice.data, slice.size);
+    if (document.HasParseError() || !document.IsObject()) {
+        return Status::InvalidArgument("FILE aggregate state requires a JSON 
object");
+    }
+    File value(FILE_FIELDS);
+    DorisVector<char> inline_bytes;
+    for (size_t i = 0; i < FILE_FIELDS; ++i) {
+        DCHECK(document.HasMember(_names[i].c_str()));

Review Comment:
   [P2] Validate FILE JSON member types before RapidJSON getters. 
`deserialize_one_cell_from_json` only checks that the root is an object, then 
uses `DCHECK(document.HasMember(...))`, `GetInt64()`, `GetStringLength()`, and 
`GetString()` directly. A malformed aggregate-state value such as 
`{"uri":1,"offset":null,"size":null,"content_type":null,"checksum":null,"inline":null}`
 reaches `GetString()` on an integer (and missing fields bypass the DCHECK in 
release) instead of returning `Status::InvalidArgument`, so bad JSON can abort 
the BE at this deserialization boundary. Check required members and 
`IsString`/`IsInt64` (including inline Base64) before the typed access.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to