HappenLee commented on code in PR #9231: URL: https://github.com/apache/incubator-doris/pull/9231#discussion_r865516492
########## be/src/vec/utils/arrow_column_to_doris_column.cpp: ########## @@ -0,0 +1,279 @@ +// 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 "vec/utils/arrow_column_to_doris_column.h" + +#include "vec/columns/column_nullable.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/runtime/vdatetime_value.h" + +#include <arrow/record_batch.h> +#include <arrow/array.h> +#include <arrow/status.h> + +#include "arrow/type.h" +#include "arrow/array/array_binary.h" +#include "arrow/array/array_nested.h" +#include "arrow/scalar.h" +#include "arrow/type_fwd.h" +#include "arrow/type_traits.h" + +#define FOR_ARROW_TYPES(M) \ + M(::arrow::Type::BOOL, TYPE_BOOLEAN) \ + M(::arrow::Type::INT8, TYPE_TINYINT) \ + M(::arrow::Type::UINT8, TYPE_TINYINT) \ + M(::arrow::Type::INT16, TYPE_SMALLINT) \ + M(::arrow::Type::UINT16, TYPE_SMALLINT) \ + M(::arrow::Type::INT32, TYPE_INT) \ + M(::arrow::Type::UINT32, TYPE_INT) \ + M(::arrow::Type::INT64, TYPE_BIGINT) \ + M(::arrow::Type::UINT64, TYPE_BIGINT) \ + M(::arrow::Type::HALF_FLOAT, TYPE_FLOAT) \ + M(::arrow::Type::FLOAT, TYPE_FLOAT) \ + M(::arrow::Type::DOUBLE, TYPE_DOUBLE) \ + M(::arrow::Type::BINARY, TYPE_VARCHAR) \ + M(::arrow::Type::FIXED_SIZE_BINARY, TYPE_VARCHAR) \ + M(::arrow::Type::STRING, TYPE_VARCHAR) \ + M(::arrow::Type::TIMESTAMP, TYPE_DATETIME) \ + M(::arrow::Type::DATE32, TYPE_DATE) \ + M(::arrow::Type::DATE64, TYPE_DATETIME) \ + M(::arrow::Type::DECIMAL, TYPE_DECIMALV2) + +#define FOR_ARROW_NUMERIC_TYPES(M) \ + M(arrow::Type::UINT8, UInt8) \ + M(arrow::Type::INT8, Int8) \ + M(arrow::Type::INT16, Int16) \ + M(arrow::Type::INT32, Int32) \ + M(arrow::Type::UINT64, UInt64) \ + M(arrow::Type::INT64, Int64) \ + M(arrow::Type::HALF_FLOAT, Float32) \ + M(arrow::Type::FLOAT, Float32) \ + M(arrow::Type::DOUBLE, Float64) + +namespace doris::vectorized { + +const PrimitiveType arrow_type_to_primitive_type(::arrow::Type::type type) { + switch(type) { +# define DISPATCH(ARROW_TYPE, CPP_TYPE) \ + case ARROW_TYPE: \ + return CPP_TYPE; + FOR_ARROW_TYPES(DISPATCH) +# undef DISPATCH + default: + break; + } + return INVALID_TYPE; +} + +static size_t fill_nullable_column(const arrow::Array* array, size_t array_idx, vectorized::ColumnNullable* nullable_column, + size_t num_elements) { + size_t null_elements_count = 0; + NullMap& map_data = nullable_column->get_null_map_data(); + for (size_t i = 0; i < num_elements; ++i) { + auto is_null = array->IsNull(array_idx + i); + map_data.emplace_back(is_null); + null_elements_count += is_null; + } + return null_elements_count; +} + +/// Inserts chars and offsets right into internal column data to reduce an overhead. +/// Internal offsets are shifted by one to the right in comparison with Arrow ones. So the last offset should map to the end of all chars. +/// Also internal strings are null terminated. +static Status convert_column_with_string_data(const arrow::Array* array, size_t array_idx, + MutableColumnPtr& data_column, size_t num_elements) { + PaddedPODArray<UInt8> & column_chars_t = assert_cast<ColumnString &>(*data_column).get_chars(); + PaddedPODArray<UInt32> & column_offsets = assert_cast<ColumnString &>(*data_column).get_offsets(); + + const auto & concrete_array = dynamic_cast<const arrow::BinaryArray &>(*array); Review Comment: code format: auto&. same to other. ########## be/src/vec/utils/arrow_column_to_doris_column.cpp: ########## @@ -0,0 +1,279 @@ +// 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 "vec/utils/arrow_column_to_doris_column.h" + +#include "vec/columns/column_nullable.h" +#include "vec/data_types/data_type_decimal.h" +#include "vec/runtime/vdatetime_value.h" + +#include <arrow/record_batch.h> +#include <arrow/array.h> +#include <arrow/status.h> + +#include "arrow/type.h" +#include "arrow/array/array_binary.h" +#include "arrow/array/array_nested.h" +#include "arrow/scalar.h" +#include "arrow/type_fwd.h" +#include "arrow/type_traits.h" + +#define FOR_ARROW_TYPES(M) \ + M(::arrow::Type::BOOL, TYPE_BOOLEAN) \ + M(::arrow::Type::INT8, TYPE_TINYINT) \ + M(::arrow::Type::UINT8, TYPE_TINYINT) \ + M(::arrow::Type::INT16, TYPE_SMALLINT) \ + M(::arrow::Type::UINT16, TYPE_SMALLINT) \ + M(::arrow::Type::INT32, TYPE_INT) \ + M(::arrow::Type::UINT32, TYPE_INT) \ + M(::arrow::Type::INT64, TYPE_BIGINT) \ + M(::arrow::Type::UINT64, TYPE_BIGINT) \ + M(::arrow::Type::HALF_FLOAT, TYPE_FLOAT) \ + M(::arrow::Type::FLOAT, TYPE_FLOAT) \ + M(::arrow::Type::DOUBLE, TYPE_DOUBLE) \ + M(::arrow::Type::BINARY, TYPE_VARCHAR) \ + M(::arrow::Type::FIXED_SIZE_BINARY, TYPE_VARCHAR) \ + M(::arrow::Type::STRING, TYPE_VARCHAR) \ + M(::arrow::Type::TIMESTAMP, TYPE_DATETIME) \ + M(::arrow::Type::DATE32, TYPE_DATE) \ + M(::arrow::Type::DATE64, TYPE_DATETIME) \ + M(::arrow::Type::DECIMAL, TYPE_DECIMALV2) + +#define FOR_ARROW_NUMERIC_TYPES(M) \ + M(arrow::Type::UINT8, UInt8) \ + M(arrow::Type::INT8, Int8) \ + M(arrow::Type::INT16, Int16) \ + M(arrow::Type::INT32, Int32) \ + M(arrow::Type::UINT64, UInt64) \ + M(arrow::Type::INT64, Int64) \ + M(arrow::Type::HALF_FLOAT, Float32) \ + M(arrow::Type::FLOAT, Float32) \ + M(arrow::Type::DOUBLE, Float64) + +namespace doris::vectorized { + +const PrimitiveType arrow_type_to_primitive_type(::arrow::Type::type type) { + switch(type) { +# define DISPATCH(ARROW_TYPE, CPP_TYPE) \ + case ARROW_TYPE: \ + return CPP_TYPE; + FOR_ARROW_TYPES(DISPATCH) +# undef DISPATCH + default: + break; + } + return INVALID_TYPE; +} + +static size_t fill_nullable_column(const arrow::Array* array, size_t array_idx, vectorized::ColumnNullable* nullable_column, + size_t num_elements) { + size_t null_elements_count = 0; + NullMap& map_data = nullable_column->get_null_map_data(); + for (size_t i = 0; i < num_elements; ++i) { + auto is_null = array->IsNull(array_idx + i); + map_data.emplace_back(is_null); + null_elements_count += is_null; + } + return null_elements_count; +} + +/// Inserts chars and offsets right into internal column data to reduce an overhead. +/// Internal offsets are shifted by one to the right in comparison with Arrow ones. So the last offset should map to the end of all chars. +/// Also internal strings are null terminated. +static Status convert_column_with_string_data(const arrow::Array* array, size_t array_idx, + MutableColumnPtr& data_column, size_t num_elements) { + PaddedPODArray<UInt8> & column_chars_t = assert_cast<ColumnString &>(*data_column).get_chars(); + PaddedPODArray<UInt32> & column_offsets = assert_cast<ColumnString &>(*data_column).get_offsets(); + + const auto & concrete_array = dynamic_cast<const arrow::BinaryArray &>(*array); Review Comment: Rethink here really need `dynamic_cast` ? 1. `dynamic_cast` means bad design of class or code, replace with virtual function 2. this code may throw exception if cast faild. it is danger here. -- 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]
