zhangstar333 commented on code in PR #12492: URL: https://github.com/apache/doris/pull/12492#discussion_r966653604
########## be/src/vec/runtime/vparquet_writer.cpp: ########## @@ -0,0 +1,782 @@ +// 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/runtime/vparquet_writer.h" + +#include <arrow/array.h> +#include <arrow/status.h> +#include <time.h> + +#include "io/file_writer.h" +#include "util/mysql_global.h" +#include "util/types.h" +#include "vec/columns/column_nullable.h" +#include "vec/columns/column_string.h" +#include "vec/columns/column_vector.h" +#include "vec/data_types/data_type_nullable.h" +#include "vec/exprs/vexpr.h" + +namespace doris::vectorized { + +VParquetWriterWrapper::VParquetWriterWrapper(doris::FileWriter* file_writer, + const std::vector<VExprContext*>& output_vexpr_ctxs, + const std::map<std::string, std::string>& properties, + const std::vector<std::vector<std::string>>& schema, + bool output_object_data) + : _output_vexpr_ctxs(output_vexpr_ctxs), + _str_schema(schema), + _cur_written_rows(0), + _rg_writer(nullptr), + _output_object_data(output_object_data) { + _outstream = std::shared_ptr<ParquetOutputStream>(new ParquetOutputStream(file_writer)); + parse_properties(properties); + parse_schema(schema); + init_parquet_writer(); +} + +void VParquetWriterWrapper::parse_properties( + const std::map<std::string, std::string>& propertie_map) { + parquet::WriterProperties::Builder builder; + for (auto it = propertie_map.begin(); it != propertie_map.end(); it++) { + std::string property_name = it->first; + std::string property_value = it->second; + if (property_name == "compression") { + // UNCOMPRESSED, SNAPPY, GZIP, BROTLI, ZSTD, LZ4, LZO, BZ2 + if (property_value == "snappy") { + builder.compression(parquet::Compression::SNAPPY); + } else if (property_value == "gzip") { + builder.compression(parquet::Compression::GZIP); + } else if (property_value == "brotli") { + builder.compression(parquet::Compression::BROTLI); + } else if (property_value == "zstd") { + builder.compression(parquet::Compression::ZSTD); + } else if (property_value == "lz4") { + builder.compression(parquet::Compression::LZ4); + } else if (property_value == "lzo") { + builder.compression(parquet::Compression::LZO); + } else if (property_value == "bz2") { + builder.compression(parquet::Compression::BZ2); + } else { + builder.compression(parquet::Compression::UNCOMPRESSED); + } + } else if (property_name == "disable_dictionary") { + if (property_value == "true") { + builder.enable_dictionary(); + } else { + builder.disable_dictionary(); + } + } else if (property_name == "version") { + if (property_value == "v1") { + builder.version(parquet::ParquetVersion::PARQUET_1_0); + } else { + builder.version(parquet::ParquetVersion::PARQUET_2_LATEST); + } + } + } + _properties = builder.build(); +} + +Status VParquetWriterWrapper::parse_schema(const std::vector<std::vector<std::string>>& schema) { + parquet::schema::NodeVector fields; + for (auto column = schema.begin(); column != schema.end(); column++) { + std::string repetition_type = (*column)[0]; + parquet::Repetition::type parquet_repetition_type = parquet::Repetition::REQUIRED; + if (repetition_type.find("required") != std::string::npos) { + parquet_repetition_type = parquet::Repetition::REQUIRED; + } else if (repetition_type.find("repeated") != std::string::npos) { + parquet_repetition_type = parquet::Repetition::REPEATED; + } else if (repetition_type.find("optional") != std::string::npos) { + parquet_repetition_type = parquet::Repetition::OPTIONAL; + } else { + parquet_repetition_type = parquet::Repetition::UNDEFINED; + } + + std::string data_type = (*column)[1]; + parquet::Type::type parquet_data_type = parquet::Type::BYTE_ARRAY; + if (data_type == "boolean") { + parquet_data_type = parquet::Type::BOOLEAN; + } else if (data_type.find("int32") != std::string::npos) { + parquet_data_type = parquet::Type::INT32; + } else if (data_type.find("int64") != std::string::npos) { + parquet_data_type = parquet::Type::INT64; + } else if (data_type.find("int96") != std::string::npos) { + parquet_data_type = parquet::Type::INT96; + } else if (data_type.find("float") != std::string::npos) { + parquet_data_type = parquet::Type::FLOAT; + } else if (data_type.find("double") != std::string::npos) { + parquet_data_type = parquet::Type::DOUBLE; + } else if (data_type.find("byte_array") != std::string::npos) { + parquet_data_type = parquet::Type::BYTE_ARRAY; + } else if (data_type.find("fixed_len_byte_array") != std::string::npos) { + parquet_data_type = parquet::Type::FIXED_LEN_BYTE_ARRAY; + } else { + parquet_data_type = parquet::Type::UNDEFINED; + } + + std::string column_name = (*column)[2]; + fields.push_back(parquet::schema::PrimitiveNode::Make(column_name, parquet_repetition_type, + parquet::LogicalType::None(), + parquet_data_type)); + _schema = std::static_pointer_cast<parquet::schema::GroupNode>( + parquet::schema::GroupNode::Make("schema", parquet::Repetition::REQUIRED, fields)); + } + return Status::OK(); +} + +Status VParquetWriterWrapper::write(const Block& block) { + if (block.rows() == 0) { + return Status::OK(); + } + size_t sz = block.rows(); + try { + for (size_t i = 0; i < block.columns(); i++) { + auto& col = block.get_by_position(i).column; + auto& type = block.get_by_position(i).type; + switch (_output_vexpr_ctxs[i]->root()->type().type) { + case TYPE_BOOLEAN: { + if (_str_schema[i][1] != "boolean") { + return Status::InvalidArgument( + "project field type is boolean, " + "but the definition type of column {} is {}", + _str_schema[i][2], _str_schema[i][1]); + } + parquet::RowGroupWriter* rgWriter = get_rg_writer(); + parquet::BoolWriter* col_writer = + static_cast<parquet::BoolWriter*>(rgWriter->column(i)); + bool default_bool = false; + if (const auto* not_nullable_column = + check_and_get_column<const ColumnVector<UInt8>>(col.get())) { + col_writer->WriteBatch( + sz, nullptr, nullptr, + reinterpret_cast<const bool*>(not_nullable_column->get_data().data())); + } else if (const auto* nullable_column = + check_and_get_column<const ColumnNullable>(col.get())) { Review Comment: here check nullable column, it maybe placed on the top of switch, so that all cases not need to write check nullable code -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org