eldenmoon commented on code in PR #59183:
URL: https://github.com/apache/doris/pull/59183#discussion_r2660269060


##########
be/src/olap/rowset/segment_v2/variant/variant_util.cpp:
##########
@@ -0,0 +1,215 @@
+// 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 "olap/rowset/segment_v2/variant/variant_util.h"
+
+#include <glog/logging.h>
+
+#include <cstdint>
+#include <memory>
+#include <string_view>
+#include <unordered_map>
+#include <vector>
+
+#include "common/status.h"
+#include "olap/tablet_schema.h"
+#include "vec/columns/column.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/columns/column_string.h"
+#include "vec/columns/column_variant.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/schema_util.h"
+#include "vec/core/block.h"
+#include "vec/data_types/data_type_jsonb.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_string.h"
+#include "vec/data_types/data_type_variant.h"
+#include "vec/json/parse2column.h"
+
+namespace doris::segment_v2::variant_util {
+
+std::unordered_map<std::string_view, vectorized::ColumnVariant::Subcolumn>
+materialize_docs_to_subcolumns(const vectorized::ColumnVariant& variant) {
+    std::unordered_map<std::string_view, vectorized::ColumnVariant::Subcolumn> 
subcolumns;
+
+    const auto [column_key, column_value] = 
variant.get_doc_value_data_paths_and_values();
+    const auto& column_offsets = variant.serialized_doc_value_column_offsets();
+    const size_t num_rows = column_offsets.size();
+
+    DCHECK_EQ(num_rows, variant.size()) << "doc snapshot offsets size mismatch 
with variant rows";
+
+    // Best-effort reserve: at most number of kv pairs.
+    subcolumns.reserve(column_key->size());
+
+    for (int row = 0; row < num_rows; ++row) {
+        const size_t start = column_offsets[row - 1];
+        const size_t end = column_offsets[row];
+        for (size_t i = start; i < end; ++i) {
+            const auto& key = column_key->get_data_at(i);
+            const std::string_view path_sv(key.data, key.size);
+
+            auto [it, inserted] = subcolumns.try_emplace(
+                    path_sv, vectorized::ColumnVariant::Subcolumn {0, true, 
false});
+            auto& subcolumn = it->second;
+            if (inserted) {
+                subcolumn.insert_many_defaults(row);
+            } else if (subcolumn.size() != row) {
+                subcolumn.insert_many_defaults(row - subcolumn.size());
+            }
+            subcolumn.deserialize_from_binary_column(column_value, i);
+        }
+    }
+
+    for (auto& [path, subcolumn] : subcolumns) {
+        if (subcolumn.size() != num_rows) {
+            subcolumn.insert_many_defaults(num_rows - subcolumn.size());
+        }
+    }
+
+    return subcolumns;
+}
+
+namespace {
+
+Status _parse_variant_columns(vectorized::Block& block, const 
std::vector<uint32_t>& variant_pos,
+                              const std::vector<vectorized::ParseConfig>& 
configs) {
+    for (size_t i = 0; i < variant_pos.size(); ++i) {
+        auto column_ref = block.get_by_position(variant_pos[i]).column;
+        bool is_nullable = column_ref->is_nullable();
+        vectorized::MutableColumnPtr var_column = column_ref->assume_mutable();
+        if (is_nullable) {
+            const auto& nullable = assert_cast<const 
vectorized::ColumnNullable&>(*column_ref);
+            var_column = nullable.get_nested_column_ptr()->assume_mutable();
+        }
+        auto& var = assert_cast<vectorized::ColumnVariant&>(*var_column);
+        var_column->finalize();
+
+        vectorized::MutableColumnPtr variant_column;
+        if (var.is_doc_mode()) {

Review Comment:
   only when rows larger than config then need to materialize



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