|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +#include "common/config.h" |
| 21 | +#include "core/block/block.h" |
| 22 | +#include "core/column/column_vector.h" |
| 23 | +#include "core/data_type/data_type_number.h" |
| 24 | +#include "exec/sink/writer/iceberg/viceberg_table_writer.h" |
| 25 | +#include "exec/sink/writer/vhive_table_writer.h" |
| 26 | +#include "exprs/vexpr_context.h" |
| 27 | +#include "exprs/vslot_ref.h" |
| 28 | +#include "runtime/runtime_state.h" |
| 29 | + |
| 30 | +namespace doris { |
| 31 | + |
| 32 | +class ExternalTableWriterPartitionLimitTest : public testing::TestWithParam<int32_t> { |
| 33 | +protected: |
| 34 | + void SetUp() override { |
| 35 | + _original_limit = config::table_sink_partition_write_max_partition_nums_per_writer; |
| 36 | + config::table_sink_partition_write_max_partition_nums_per_writer = GetParam(); |
| 37 | + } |
| 38 | + |
| 39 | + void TearDown() override { |
| 40 | + config::table_sink_partition_write_max_partition_nums_per_writer = _original_limit; |
| 41 | + } |
| 42 | + |
| 43 | + Block new_partition_block() { |
| 44 | + auto column = ColumnInt32::create(); |
| 45 | + column->get_data().push_back(GetParam()); |
| 46 | + return Block( |
| 47 | + {ColumnWithTypeAndName(std::move(column), std::make_shared<DataTypeInt32>(), "p")}); |
| 48 | + } |
| 49 | + |
| 50 | + void check_limit_status(const Status& status, const std::string& writer_type) { |
| 51 | + EXPECT_EQ(TStatusCode::INVALID_ARGUMENT, status.to_thrift().status_code); |
| 52 | + const auto message = status.to_string(); |
| 53 | + EXPECT_NE(std::string::npos, message.find(writer_type + " writer")); |
| 54 | + EXPECT_NE(std::string::npos, message.find("open partitions=" + std::to_string(GetParam()))); |
| 55 | + EXPECT_NE(std::string::npos, |
| 56 | + message.find("attempted partitions=" + std::to_string(GetParam() + 1))); |
| 57 | + EXPECT_NE(std::string::npos, message.find("limit=" + std::to_string(GetParam()))); |
| 58 | + EXPECT_NE(std::string::npos, message.find("per-writer limit")); |
| 59 | + EXPECT_NE(std::string::npos, |
| 60 | + message.find("table_sink_partition_write_max_partition_nums_per_writer")); |
| 61 | + EXPECT_NE(std::string::npos, message.find("Split the write into multiple statements")); |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + int32_t _original_limit = 0; |
| 66 | +}; |
| 67 | + |
| 68 | +TEST_P(ExternalTableWriterPartitionLimitTest, HiveRejectsNextPartitionBeforeOpeningAFile) { |
| 69 | + THiveColumn partition_column; |
| 70 | + partition_column.__set_name("p"); |
| 71 | + partition_column.__set_column_type(THiveColumnType::PARTITION_KEY); |
| 72 | + THiveTableSink hive_sink; |
| 73 | + hive_sink.__set_columns({partition_column}); |
| 74 | + TDataSink sink; |
| 75 | + sink.__set_hive_table_sink(hive_sink); |
| 76 | + auto type = std::make_shared<DataTypeInt32>(); |
| 77 | + VExprContextSPtrs exprs = { |
| 78 | + VExprContext::create_shared(VSlotRef::create_shared(0, 0, -1, type, "p"))}; |
| 79 | + VHiveTableWriter writer(sink, exprs, nullptr, nullptr); |
| 80 | + writer._partition_columns_input_index = {0}; |
| 81 | + // Existing writers are never dereferenced when rejecting a new partition. No filesystem is needed. |
| 82 | + for (int32_t i = 0; i < GetParam(); ++i) { |
| 83 | + writer._partitions_to_writers.emplace("p=" + std::to_string(i), nullptr); |
| 84 | + } |
| 85 | + RuntimeState state; |
| 86 | + auto block = new_partition_block(); |
| 87 | + check_limit_status(writer.write(&state, block), "Hive"); |
| 88 | + EXPECT_EQ(static_cast<size_t>(GetParam()), writer._partitions_to_writers.size()); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_P(ExternalTableWriterPartitionLimitTest, IcebergRejectsNextPartitionBeforeOpeningAFile) { |
| 92 | + TDataSink sink; |
| 93 | + sink.__set_iceberg_table_sink(TIcebergTableSink()); |
| 94 | + VExprContextSPtrs exprs; |
| 95 | + iceberg::PartitionField field(1, 1000, "p", "identity"); |
| 96 | + VIcebergTableWriter writer(sink, exprs, nullptr, nullptr); |
| 97 | + writer._iceberg_partition_columns.emplace_back( |
| 98 | + field, TYPE_INT, 0, std::vector<size_t> {}, |
| 99 | + std::make_unique<IdentityPartitionColumnTransform>(std::make_shared<DataTypeInt32>())); |
| 100 | + for (int32_t i = 0; i < GetParam(); ++i) { |
| 101 | + writer._partitions_to_writers.emplace("existing-" + std::to_string(i), nullptr); |
| 102 | + } |
| 103 | + auto block = new_partition_block(); |
| 104 | + check_limit_status(writer.write_prepared_block(block), "Iceberg"); |
| 105 | + EXPECT_EQ(static_cast<size_t>(GetParam()), writer._partitions_to_writers.size()); |
| 106 | +} |
| 107 | + |
| 108 | +INSTANTIATE_TEST_SUITE_P(DefaultAndConfiguredLimits, ExternalTableWriterPartitionLimitTest, |
| 109 | + testing::Values(128, 2)); |
| 110 | + |
| 111 | +} // namespace doris |
0 commit comments