|
| 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 "exec/schema_scanner/schema_table_options_scanner.h" |
| 19 | + |
| 20 | +#include "runtime/client_cache.h" |
| 21 | +#include "runtime/exec_env.h" |
| 22 | +#include "runtime/runtime_state.h" |
| 23 | +#include "util/thrift_rpc_helper.h" |
| 24 | +#include "vec/common/string_ref.h" |
| 25 | +#include "vec/core/block.h" |
| 26 | +#include "vec/data_types/data_type_factory.hpp" |
| 27 | + |
| 28 | +namespace doris { |
| 29 | +std::vector<SchemaScanner::ColumnDesc> SchemaTableOptionsScanner::_s_tbls_columns = { |
| 30 | + {"TABLE_NAME", TYPE_VARCHAR, sizeof(StringRef), true}, |
| 31 | + {"TABLE_CATALOG", TYPE_VARCHAR, sizeof(StringRef), true}, |
| 32 | + {"TABLE_SCHEMA", TYPE_VARCHAR, sizeof(StringRef), true}, |
| 33 | + {"DISTRIBUTE_KEY", TYPE_STRING, sizeof(StringRef), true}, |
| 34 | + {"DISTRIBUTE_TYPE", TYPE_STRING, sizeof(StringRef), true}, |
| 35 | + {"BUCKETS_NUM", TYPE_INT, sizeof(int32_t), true}, |
| 36 | + {"PARTITION_NUM", TYPE_INT, sizeof(int32_t), true}, |
| 37 | + {"PROPERTIES", TYPE_STRING, sizeof(StringRef), true}, |
| 38 | +}; |
| 39 | + |
| 40 | +SchemaTableOptionsScanner::SchemaTableOptionsScanner() |
| 41 | + : SchemaScanner(_s_tbls_columns, TSchemaTableType::SCH_TABLE_OPTIONS) {} |
| 42 | + |
| 43 | +Status SchemaTableOptionsScanner::start(RuntimeState* state) { |
| 44 | + _block_rows_limit = state->batch_size(); |
| 45 | + _rpc_timeout = state->execution_timeout() * 1000; |
| 46 | + return Status::OK(); |
| 47 | +} |
| 48 | + |
| 49 | +Status SchemaTableOptionsScanner::get_block_from_fe() { |
| 50 | + TNetworkAddress master_addr = ExecEnv::GetInstance()->master_info()->network_address; |
| 51 | + |
| 52 | + TSchemaTableRequestParams schema_table_request_params; |
| 53 | + for (int i = 0; i < _s_tbls_columns.size(); i++) { |
| 54 | + schema_table_request_params.__isset.columns_name = true; |
| 55 | + schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); |
| 56 | + } |
| 57 | + schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); |
| 58 | + |
| 59 | + TFetchSchemaTableDataRequest request; |
| 60 | + request.__set_schema_table_name(TSchemaTableName::TABLE_OPTIONS); |
| 61 | + request.__set_schema_table_params(schema_table_request_params); |
| 62 | + |
| 63 | + TFetchSchemaTableDataResult result; |
| 64 | + |
| 65 | + RETURN_IF_ERROR(ThriftRpcHelper::rpc<FrontendServiceClient>( |
| 66 | + master_addr.hostname, master_addr.port, |
| 67 | + [&request, &result](FrontendServiceConnection& client) { |
| 68 | + client->fetchSchemaTableData(result, request); |
| 69 | + }, |
| 70 | + _rpc_timeout)); |
| 71 | + |
| 72 | + Status status(Status::create(result.status)); |
| 73 | + if (!status.ok()) { |
| 74 | + LOG(WARNING) << "fetch table options from FE failed, errmsg=" << status; |
| 75 | + return status; |
| 76 | + } |
| 77 | + std::vector<TRow> result_data = result.data_batch; |
| 78 | + |
| 79 | + _tableoptions_block = vectorized::Block::create_unique(); |
| 80 | + for (int i = 0; i < _s_tbls_columns.size(); ++i) { |
| 81 | + TypeDescriptor descriptor(_s_tbls_columns[i].type); |
| 82 | + auto data_type = vectorized::DataTypeFactory::instance().create_data_type(descriptor, true); |
| 83 | + _tableoptions_block->insert(vectorized::ColumnWithTypeAndName( |
| 84 | + data_type->create_column(), data_type, _s_tbls_columns[i].name)); |
| 85 | + } |
| 86 | + _tableoptions_block->reserve(_block_rows_limit); |
| 87 | + if (result_data.size() > 0) { |
| 88 | + int col_size = result_data[0].column_value.size(); |
| 89 | + if (col_size != _s_tbls_columns.size()) { |
| 90 | + return Status::InternalError<false>("table options schema is not match for FE and BE"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + for (int i = 0; i < result_data.size(); i++) { |
| 95 | + TRow row = result_data[i]; |
| 96 | + for (int j = 0; j < _s_tbls_columns.size(); j++) { |
| 97 | + RETURN_IF_ERROR(insert_block_column(row.column_value[j], j, _tableoptions_block.get(), |
| 98 | + _s_tbls_columns[j].type)); |
| 99 | + } |
| 100 | + } |
| 101 | + return Status::OK(); |
| 102 | +} |
| 103 | + |
| 104 | +Status SchemaTableOptionsScanner::get_next_block(vectorized::Block* block, bool* eos) { |
| 105 | + if (!_is_init) { |
| 106 | + return Status::InternalError("Used before initialized."); |
| 107 | + } |
| 108 | + |
| 109 | + if (nullptr == block || nullptr == eos) { |
| 110 | + return Status::InternalError("input pointer is nullptr."); |
| 111 | + } |
| 112 | + |
| 113 | + if (_tableoptions_block == nullptr) { |
| 114 | + RETURN_IF_ERROR(get_block_from_fe()); |
| 115 | + _total_rows = _tableoptions_block->rows(); |
| 116 | + } |
| 117 | + |
| 118 | + if (_row_idx == _total_rows) { |
| 119 | + *eos = true; |
| 120 | + return Status::OK(); |
| 121 | + } |
| 122 | + |
| 123 | + int current_batch_rows = std::min(_block_rows_limit, _total_rows - _row_idx); |
| 124 | + vectorized::MutableBlock mblock = vectorized::MutableBlock::build_mutable_block(block); |
| 125 | + mblock.add_rows(_tableoptions_block.get(), _row_idx, current_batch_rows); |
| 126 | + _row_idx += current_batch_rows; |
| 127 | + |
| 128 | + *eos = _row_idx == _total_rows; |
| 129 | + return Status::OK(); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace doris |
0 commit comments