github-actions[bot] commented on code in PR #34929: URL: https://github.com/apache/doris/pull/34929#discussion_r1617496274
########## be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp: ########## @@ -0,0 +1,484 @@ +// 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 "viceberg_table_writer.h" + +#include "runtime/runtime_state.h" +#include "vec/core/block.h" +#include "vec/core/column_with_type_and_name.h" +#include "vec/core/materialize_block.h" +#include "vec/exec/format/table/iceberg/partition_spec_parser.h" +#include "vec/exec/format/table/iceberg/schema_parser.h" +#include "vec/exprs/vexpr.h" +#include "vec/exprs/vexpr_context.h" +#include "vec/sink/writer/iceberg/partition_transformers.h" +#include "vec/sink/writer/iceberg/viceberg_partition_writer.h" +#include "vec/sink/writer/vhive_utils.h" + +namespace doris { +namespace vectorized { + +VIcebergTableWriter::VIcebergTableWriter(const TDataSink& t_sink, + const VExprContextSPtrs& output_expr_ctxs) + : AsyncResultWriter(output_expr_ctxs), _t_sink(t_sink) { + DCHECK(_t_sink.__isset.iceberg_table_sink); +} + +Status VIcebergTableWriter::init_properties(ObjectPool* pool) { + return Status::OK(); +} + +Status VIcebergTableWriter::open(RuntimeState* state, RuntimeProfile* profile) { + _state = state; + _profile = profile; + + // add all counter + _written_rows_counter = ADD_COUNTER(_profile, "WrittenRows", TUnit::UNIT); + _send_data_timer = ADD_TIMER(_profile, "SendDataTime"); + _partition_writers_dispatch_timer = + ADD_CHILD_TIMER(_profile, "PartitionsDispatchTime", "SendDataTime"); + _partition_writers_write_timer = + ADD_CHILD_TIMER(_profile, "PartitionsWriteTime", "SendDataTime"); + _partition_writers_count = ADD_COUNTER(_profile, "PartitionsWriteCount", TUnit::UNIT); + _open_timer = ADD_TIMER(_profile, "OpenTime"); + _close_timer = ADD_TIMER(_profile, "CloseTime"); + _write_file_counter = ADD_COUNTER(_profile, "WriteFileCount", TUnit::UNIT); + + SCOPED_TIMER(_open_timer); + try { + _schema = iceberg::SchemaParser::from_json(_t_sink.iceberg_table_sink.schema_json); + std::string partition_spec_json = + _t_sink.iceberg_table_sink + .partition_specs_json[_t_sink.iceberg_table_sink.partition_spec_id]; + if (!partition_spec_json.empty()) { + _partition_spec = iceberg::PartitionSpecParser::from_json(_schema, partition_spec_json); + _iceberg_partition_columns = _to_iceberg_partition_columns(); + } + } catch (doris::Exception& e) { + return e.to_status(); + } + + std::set<int> partition_idx_set; + for (const auto& iceberg_partition_column : _iceberg_partition_columns) { + partition_idx_set.insert(iceberg_partition_column.source_idx()); + } + + for (int i = 0; i < _schema->columns().size(); ++i) { + _write_output_vexpr_ctxs.emplace_back(_vec_output_expr_ctxs[i]); + } + + return Status::OK(); +} + +std::vector<VIcebergTableWriter::IcebergPartitionColumn> +VIcebergTableWriter::_to_iceberg_partition_columns() { + std::vector<IcebergPartitionColumn> partition_columns; + + std::unordered_map<int, int> id_to_column_idx; + id_to_column_idx.reserve(_schema->columns().size()); + for (int i = 0; i < _schema->columns().size(); i++) { + id_to_column_idx[_schema->columns()[i].field_id()] = i; + } + for (const auto& partition_field : _partition_spec->fields()) { + int column_idx = id_to_column_idx[partition_field.source_id()]; + std::unique_ptr<PartitionColumnTransform> partition_column_transform = + PartitionColumnTransforms::create( + partition_field, _vec_output_expr_ctxs[column_idx]->root()->type()); + partition_columns.emplace_back(partition_field, + _vec_output_expr_ctxs[column_idx]->root()->type(), + column_idx, std::move(partition_column_transform)); + } + return partition_columns; +} + +Status VIcebergTableWriter::write(vectorized::Block& block) { Review Comment: warning: function 'write' has cognitive complexity of 85 (threshold 50) [readability-function-cognitive-complexity] ```cpp Status VIcebergTableWriter::write(vectorized::Block& block) { ^ ``` <details> <summary>Additional context</summary> **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:109:** +1, including nesting penalty of 0, nesting level increased to 1 ```cpp if (block.rows() == 0) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:113:** +1, including nesting penalty of 0, nesting level increased to 1 ```cpp RETURN_IF_ERROR(vectorized::VExprContext::get_output_block_after_execute_exprs( ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:113:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp RETURN_IF_ERROR(vectorized::VExprContext::get_output_block_after_execute_exprs( ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:120:** +1, including nesting penalty of 0, nesting level increased to 1 ```cpp if (_iceberg_partition_columns.empty()) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:125:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp if (writer_iter == _partitions_to_writers.end()) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:128:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp } catch (doris::Exception& e) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:132:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:132:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:133:** +1, nesting level increased to 2 ```cpp } else { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:134:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if (writer_iter->second->written_len() > config::iceberg_sink_max_file_size) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:145:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp } catch (doris::Exception& e) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:149:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:149:** +5, including nesting penalty of 4, nesting level increased to 5 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:150:** +1, nesting level increased to 3 ```cpp } else { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:157:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp RETURN_IF_ERROR(writer->write(output_block)); ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:157:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp RETURN_IF_ERROR(writer->write(output_block)); ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:168:** +1, including nesting penalty of 0, nesting level increased to 1 ```cpp for (int i = 0; i < output_block.rows(); ++i) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:172:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp } catch (doris::Exception& e) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:179:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp } catch (doris::Exception& e) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:183:** nesting level increased to 2 ```cpp [&](const std::string& partition_name, int position, ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:189:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:189:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp RETURN_IF_ERROR(writer->open(_state, _profile)); ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:195:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp } catch (doris::Exception& e) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:202:** +2, including nesting penalty of 1, nesting level increased to 2 ```cpp if (writer_iter == _partitions_to_writers.end()) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:204:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if (_partitions_to_writers.size() + 1 > ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:210:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp RETURN_IF_ERROR(create_and_open_writer(partition_name, i, nullptr, 0, writer)); ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:210:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp RETURN_IF_ERROR(create_and_open_writer(partition_name, i, nullptr, 0, writer)); ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:211:** +1, nesting level increased to 2 ```cpp } else { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:213:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if (writer_iter->second->written_len() > config::iceberg_sink_max_file_size) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:222:** +4, including nesting penalty of 3, nesting level increased to 4 ```cpp RETURN_IF_ERROR(create_and_open_writer(partition_name, i, &file_name, ^ ``` **be/src/common/status.h:612:** expanded from macro 'RETURN_IF_ERROR' ```cpp do { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:222:** +5, including nesting penalty of 4, nesting level increased to 5 ```cpp RETURN_IF_ERROR(create_and_open_writer(partition_name, i, &file_name, ^ ``` **be/src/common/status.h:614:** expanded from macro 'RETURN_IF_ERROR' ```cpp if (UNLIKELY(!_status_.ok())) { \ ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:224:** +1, nesting level increased to 3 ```cpp } else { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:228:** +3, including nesting penalty of 2, nesting level increased to 3 ```cpp if (writer_pos_iter == writer_positions.end()) { ^ ``` **be/src/vec/sink/writer/iceberg/viceberg_table_writer.cpp:232:** +1, nesting level increased to 3 ```cpp } else { ^ ``` </details> -- 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