leaves12138 commented on code in PR #24: URL: https://github.com/apache/paimon-cpp/pull/24#discussion_r3308725168
########## src/paimon/common/data/columnar/columnar_utils.h: ########## @@ -0,0 +1,106 @@ +/* + * 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. + */ +#pragma once + +#include <cassert> +#include <cstdint> +#include <cstring> +#include <memory> +#include <string_view> + +#include "arrow/api.h" +#include "arrow/array/array_base.h" +#include "arrow/array/array_binary.h" +#include "arrow/array/array_dict.h" +#include "arrow/array/array_primitive.h" +#include "arrow/type_traits.h" +#include "arrow/util/checked_cast.h" +#include "paimon/memory/bytes.h" + +namespace paimon { +class MemoryPool; + +class ColumnarUtils { + public: + ColumnarUtils() = delete; + ~ColumnarUtils() = delete; + + template <typename DataType, typename ValueType> + static ValueType GetGenericValue(const arrow::Array* array, int32_t pos) { + using ArrayType = typename arrow::TypeTraits<DataType>::ArrayType; + const auto* typed_array = arrow::internal::checked_cast<const ArrayType*>(array); + assert(typed_array); + return typed_array->Value(pos); + } + + static std::string_view GetView(const arrow::Array* array, int32_t pos) { + auto type_id = array->type_id(); + bool is_dict = (type_id == arrow::Type::type::DICTIONARY); + if (!is_dict) { + const auto* typed_array = + arrow::internal::checked_cast<const arrow::BinaryArray*>(array); + assert(typed_array); + return typed_array->GetView(pos); + } else { + const auto* typed_array = + arrow::internal::checked_cast<const arrow::DictionaryArray*>(array); + assert(typed_array); + auto dict_type = + arrow::internal::checked_pointer_cast<arrow::DictionaryType>(array->type()); + assert(dict_type); + auto value_type_id = dict_type->value_type()->id(); + auto index_type_id = dict_type->index_type()->id(); + int64_t dict_index = -1; + if (index_type_id == arrow::Type::type::INT32) { Review Comment: This only decodes dictionary indices for `INT32` and `INT64`, but Arrow dictionary indices are not limited to these two widths (for example `int8`/`int16` dictionaries are valid and commonly used for small dictionaries). For those arrays `dict_index` stays `-1`; in release builds the assert is compiled out and the code then calls `dictionary->GetView(-1)`, which can read before the offsets buffer or crash. Please handle all supported integer index types (or return a proper error instead of relying on `assert`). -- 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]
