|
| 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 "olap/partial_update_info.h" |
| 19 | + |
| 20 | +#include <gen_cpp/olap_file.pb.h> |
| 21 | + |
| 22 | +#include "olap/tablet_schema.h" |
| 23 | + |
| 24 | +namespace doris { |
| 25 | + |
| 26 | +void PartialUpdateInfo::init(const TabletSchema& tablet_schema, bool partial_update, |
| 27 | + const std::set<string>& partial_update_cols, bool is_strict_mode, |
| 28 | + int64_t timestamp_ms, const std::string& timezone, |
| 29 | + const std::string& auto_increment_column, int64_t cur_max_version) { |
| 30 | + is_partial_update = partial_update; |
| 31 | + partial_update_input_columns = partial_update_cols; |
| 32 | + max_version_in_flush_phase = cur_max_version; |
| 33 | + this->timestamp_ms = timestamp_ms; |
| 34 | + this->timezone = timezone; |
| 35 | + missing_cids.clear(); |
| 36 | + update_cids.clear(); |
| 37 | + for (auto i = 0; i < tablet_schema.num_columns(); ++i) { |
| 38 | + auto tablet_column = tablet_schema.column(i); |
| 39 | + if (!partial_update_input_columns.contains(tablet_column.name())) { |
| 40 | + missing_cids.emplace_back(i); |
| 41 | + if (!tablet_column.has_default_value() && !tablet_column.is_nullable() && |
| 42 | + tablet_schema.auto_increment_column() != tablet_column.name()) { |
| 43 | + can_insert_new_rows_in_partial_update = false; |
| 44 | + } |
| 45 | + } else { |
| 46 | + update_cids.emplace_back(i); |
| 47 | + } |
| 48 | + if (auto_increment_column == tablet_column.name()) { |
| 49 | + is_schema_contains_auto_inc_column = true; |
| 50 | + } |
| 51 | + } |
| 52 | + this->is_strict_mode = is_strict_mode; |
| 53 | + is_input_columns_contains_auto_inc_column = |
| 54 | + is_partial_update && partial_update_input_columns.contains(auto_increment_column); |
| 55 | + _generate_default_values_for_missing_cids(tablet_schema); |
| 56 | +} |
| 57 | + |
| 58 | +void PartialUpdateInfo::to_pb(PartialUpdateInfoPB* partial_update_info_pb) const { |
| 59 | + partial_update_info_pb->set_is_partial_update(is_partial_update); |
| 60 | + partial_update_info_pb->set_max_version_in_flush_phase(max_version_in_flush_phase); |
| 61 | + for (const auto& col : partial_update_input_columns) { |
| 62 | + partial_update_info_pb->add_partial_update_input_columns(col); |
| 63 | + } |
| 64 | + for (auto cid : missing_cids) { |
| 65 | + partial_update_info_pb->add_missing_cids(cid); |
| 66 | + } |
| 67 | + for (auto cid : update_cids) { |
| 68 | + partial_update_info_pb->add_update_cids(cid); |
| 69 | + } |
| 70 | + partial_update_info_pb->set_can_insert_new_rows_in_partial_update( |
| 71 | + can_insert_new_rows_in_partial_update); |
| 72 | + partial_update_info_pb->set_is_strict_mode(is_strict_mode); |
| 73 | + partial_update_info_pb->set_timestamp_ms(timestamp_ms); |
| 74 | + partial_update_info_pb->set_timezone(timezone); |
| 75 | + partial_update_info_pb->set_is_input_columns_contains_auto_inc_column( |
| 76 | + is_input_columns_contains_auto_inc_column); |
| 77 | + partial_update_info_pb->set_is_schema_contains_auto_inc_column( |
| 78 | + is_schema_contains_auto_inc_column); |
| 79 | + for (const auto& value : default_values) { |
| 80 | + partial_update_info_pb->add_default_values(value); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void PartialUpdateInfo::from_pb(PartialUpdateInfoPB* partial_update_info_pb) { |
| 85 | + is_partial_update = partial_update_info_pb->is_partial_update(); |
| 86 | + max_version_in_flush_phase = partial_update_info_pb->has_max_version_in_flush_phase() |
| 87 | + ? partial_update_info_pb->max_version_in_flush_phase() |
| 88 | + : -1; |
| 89 | + partial_update_input_columns.clear(); |
| 90 | + for (const auto& col : partial_update_info_pb->partial_update_input_columns()) { |
| 91 | + partial_update_input_columns.insert(col); |
| 92 | + } |
| 93 | + missing_cids.clear(); |
| 94 | + for (auto cid : partial_update_info_pb->missing_cids()) { |
| 95 | + missing_cids.push_back(cid); |
| 96 | + } |
| 97 | + update_cids.clear(); |
| 98 | + for (auto cid : partial_update_info_pb->update_cids()) { |
| 99 | + update_cids.push_back(cid); |
| 100 | + } |
| 101 | + can_insert_new_rows_in_partial_update = |
| 102 | + partial_update_info_pb->can_insert_new_rows_in_partial_update(); |
| 103 | + is_strict_mode = partial_update_info_pb->is_strict_mode(); |
| 104 | + timestamp_ms = partial_update_info_pb->timestamp_ms(); |
| 105 | + timezone = partial_update_info_pb->timezone(); |
| 106 | + is_input_columns_contains_auto_inc_column = |
| 107 | + partial_update_info_pb->is_input_columns_contains_auto_inc_column(); |
| 108 | + is_schema_contains_auto_inc_column = |
| 109 | + partial_update_info_pb->is_schema_contains_auto_inc_column(); |
| 110 | + default_values.clear(); |
| 111 | + for (const auto& value : partial_update_info_pb->default_values()) { |
| 112 | + default_values.push_back(value); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +std::string PartialUpdateInfo::summary() const { |
| 117 | + return fmt::format( |
| 118 | + "update_cids={}, missing_cids={}, is_strict_mode={}, max_version_in_flush_phase={}", |
| 119 | + update_cids.size(), missing_cids.size(), is_strict_mode, max_version_in_flush_phase); |
| 120 | +} |
| 121 | + |
| 122 | +void PartialUpdateInfo::_generate_default_values_for_missing_cids( |
| 123 | + const TabletSchema& tablet_schema) { |
| 124 | + for (unsigned int cur_cid : missing_cids) { |
| 125 | + const auto& column = tablet_schema.column(cur_cid); |
| 126 | + if (column.has_default_value()) { |
| 127 | + std::string default_value; |
| 128 | + if (UNLIKELY(tablet_schema.column(cur_cid).type() == |
| 129 | + FieldType::OLAP_FIELD_TYPE_DATETIMEV2 && |
| 130 | + to_lower(tablet_schema.column(cur_cid).default_value()) |
| 131 | + .find(to_lower("CURRENT_TIMESTAMP")) != |
| 132 | + std::string::npos)) { |
| 133 | + DateV2Value<DateTimeV2ValueType> dtv; |
| 134 | + dtv.from_unixtime(timestamp_ms / 1000, timezone); |
| 135 | + default_value = dtv.debug_string(); |
| 136 | + } else if (UNLIKELY(tablet_schema.column(cur_cid).type() == |
| 137 | + FieldType::OLAP_FIELD_TYPE_DATEV2 && |
| 138 | + to_lower(tablet_schema.column(cur_cid).default_value()) |
| 139 | + .find(to_lower("CURRENT_DATE")) != |
| 140 | + std::string::npos)) { |
| 141 | + DateV2Value<DateV2ValueType> dv; |
| 142 | + dv.from_unixtime(timestamp_ms / 1000, timezone); |
| 143 | + default_value = dv.debug_string(); |
| 144 | + } else { |
| 145 | + default_value = tablet_schema.column(cur_cid).default_value(); |
| 146 | + } |
| 147 | + default_values.emplace_back(default_value); |
| 148 | + } else { |
| 149 | + // place an empty string here |
| 150 | + default_values.emplace_back(); |
| 151 | + } |
| 152 | + } |
| 153 | + CHECK_EQ(missing_cids.size(), default_values.size()); |
| 154 | +} |
| 155 | +} // namespace doris |
0 commit comments