Coverage Report

Created: 2025-03-29 15:49

/root/doris/be/src/exec/schema_scanner.cpp
Line
Count
Source (jump to first uncovered line)
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.h"
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/Types_types.h>
22
#include <glog/logging.h>
23
#include <string.h>
24
25
#include <new>
26
#include <ostream>
27
#include <utility>
28
29
#include "exec/schema_scanner/schema_active_queries_scanner.h"
30
#include "exec/schema_scanner/schema_backend_active_tasks.h"
31
#include "exec/schema_scanner/schema_catalog_meta_cache_stats_scanner.h"
32
#include "exec/schema_scanner/schema_charsets_scanner.h"
33
#include "exec/schema_scanner/schema_collations_scanner.h"
34
#include "exec/schema_scanner/schema_columns_scanner.h"
35
#include "exec/schema_scanner/schema_dummy_scanner.h"
36
#include "exec/schema_scanner/schema_file_cache_statistics.h"
37
#include "exec/schema_scanner/schema_files_scanner.h"
38
#include "exec/schema_scanner/schema_metadata_name_ids_scanner.h"
39
#include "exec/schema_scanner/schema_partitions_scanner.h"
40
#include "exec/schema_scanner/schema_processlist_scanner.h"
41
#include "exec/schema_scanner/schema_profiling_scanner.h"
42
#include "exec/schema_scanner/schema_routine_scanner.h"
43
#include "exec/schema_scanner/schema_rowsets_scanner.h"
44
#include "exec/schema_scanner/schema_schema_privileges_scanner.h"
45
#include "exec/schema_scanner/schema_schemata_scanner.h"
46
#include "exec/schema_scanner/schema_table_options_scanner.h"
47
#include "exec/schema_scanner/schema_table_privileges_scanner.h"
48
#include "exec/schema_scanner/schema_table_properties_scanner.h"
49
#include "exec/schema_scanner/schema_tables_scanner.h"
50
#include "exec/schema_scanner/schema_user_privileges_scanner.h"
51
#include "exec/schema_scanner/schema_user_scanner.h"
52
#include "exec/schema_scanner/schema_variables_scanner.h"
53
#include "exec/schema_scanner/schema_views_scanner.h"
54
#include "exec/schema_scanner/schema_workload_group_privileges.h"
55
#include "exec/schema_scanner/schema_workload_group_resource_usage_scanner.h"
56
#include "exec/schema_scanner/schema_workload_groups_scanner.h"
57
#include "exec/schema_scanner/schema_workload_sched_policy_scanner.h"
58
#include "olap/hll.h"
59
#include "pipeline/pipeline_x/dependency.h"
60
#include "runtime/define_primitive_type.h"
61
#include "runtime/fragment_mgr.h"
62
#include "runtime/types.h"
63
#include "util/string_util.h"
64
#include "util/types.h"
65
#include "vec/columns/column.h"
66
#include "vec/columns/column_complex.h"
67
#include "vec/columns/column_nullable.h"
68
#include "vec/columns/column_string.h"
69
#include "vec/columns/column_vector.h"
70
#include "vec/columns/columns_number.h"
71
#include "vec/common/string_ref.h"
72
#include "vec/core/block.h"
73
#include "vec/core/column_with_type_and_name.h"
74
#include "vec/core/types.h"
75
#include "vec/data_types/data_type.h"
76
#include "vec/data_types/data_type_factory.hpp"
77
78
namespace doris {
79
class ObjectPool;
80
81
SchemaScanner::SchemaScanner(const std::vector<ColumnDesc>& columns)
82
2
        : _is_init(false), _columns(columns), _schema_table_type(TSchemaTableType::SCH_INVALID) {}
83
84
SchemaScanner::SchemaScanner(const std::vector<ColumnDesc>& columns, TSchemaTableType::type type)
85
0
        : _is_init(false), _columns(columns), _schema_table_type(type) {}
86
87
2
SchemaScanner::~SchemaScanner() = default;
88
89
0
Status SchemaScanner::start(RuntimeState* state) {
90
0
    if (!_is_init) {
91
0
        return Status::InternalError("call Start before Init.");
92
0
    }
93
94
0
    return Status::OK();
95
0
}
96
97
0
Status SchemaScanner::get_next_block(RuntimeState* state, vectorized::Block* block, bool* eos) {
98
0
    if (_data_block == nullptr) {
99
0
        return Status::InternalError("No data left!");
100
0
    }
101
0
    DCHECK(_async_thread_running == false);
102
0
    RETURN_IF_ERROR(_scanner_status.status());
103
0
    for (size_t i = 0; i < block->columns(); i++) {
104
0
        std::move(*block->get_by_position(i).column)
105
0
                .mutate()
106
0
                ->insert_range_from(*_data_block->get_by_position(i).column, 0,
107
0
                                    _data_block->rows());
108
0
    }
109
0
    _data_block->clear_column_data();
110
0
    *eos = _eos;
111
0
    if (!*eos) {
112
0
        RETURN_IF_ERROR(get_next_block_async(state));
113
0
    }
114
0
    return Status::OK();
115
0
}
116
117
0
Status SchemaScanner::get_next_block_async(RuntimeState* state) {
118
0
    _dependency->block();
119
0
    auto task_ctx = state->get_task_execution_context();
120
0
    RETURN_IF_ERROR(ExecEnv::GetInstance()->fragment_mgr()->get_thread_pool()->submit_func(
121
0
            [this, task_ctx, state]() {
122
0
                auto task_lock = task_ctx.lock();
123
0
                if (task_lock == nullptr) {
124
0
                    return;
125
0
                }
126
0
                DCHECK(_async_thread_running == false);
127
0
                SCOPED_ATTACH_TASK(state);
128
0
                _dependency->block();
129
0
                _async_thread_running = true;
130
0
                if (!_opened) {
131
0
                    _data_block = vectorized::Block::create_unique();
132
0
                    _init_block(_data_block.get());
133
0
                    _scanner_status.update(start(state));
134
0
                    _opened = true;
135
0
                }
136
0
                bool eos = false;
137
0
                _scanner_status.update(get_next_block_internal(_data_block.get(), &eos));
138
0
                _eos = eos;
139
0
                _async_thread_running = false;
140
0
                _dependency->set_ready();
141
0
            }));
142
0
    return Status::OK();
143
0
}
144
145
0
Status SchemaScanner::get_next_block_internal(vectorized::Block* block, bool* eos) {
146
0
    if (!_is_init) {
147
0
        return Status::InternalError("used before initialized.");
148
0
    }
149
150
0
    if (nullptr == block || nullptr == eos) {
151
0
        return Status::InternalError("input pointer is nullptr.");
152
0
    }
153
154
0
    *eos = true;
155
0
    return Status::OK();
156
0
}
157
158
0
Status SchemaScanner::init(SchemaScannerParam* param, ObjectPool* pool) {
159
0
    if (_is_init) {
160
0
        return Status::OK();
161
0
    }
162
0
    if (nullptr == param || nullptr == pool) {
163
0
        return Status::InternalError("invalid parameter");
164
0
    }
165
166
0
    _param = param;
167
0
    _is_init = true;
168
169
0
    if (_param->profile) {
170
0
        _get_db_timer = ADD_TIMER(_param->profile, "GetDbTime");
171
0
        _get_table_timer = ADD_TIMER(_param->profile, "GetTableTime");
172
0
        _get_describe_timer = ADD_TIMER(_param->profile, "GetDescribeTime");
173
0
        _fill_block_timer = ADD_TIMER(_param->profile, "FillBlockTime");
174
0
    }
175
176
0
    return Status::OK();
177
0
}
178
179
0
std::unique_ptr<SchemaScanner> SchemaScanner::create(TSchemaTableType::type type) {
180
0
    switch (type) {
181
0
    case TSchemaTableType::SCH_TABLES:
182
0
        return SchemaTablesScanner::create_unique();
183
0
    case TSchemaTableType::SCH_SCHEMATA:
184
0
        return SchemaSchemataScanner::create_unique();
185
0
    case TSchemaTableType::SCH_COLUMNS:
186
0
        return SchemaColumnsScanner::create_unique();
187
0
    case TSchemaTableType::SCH_CHARSETS:
188
0
        return SchemaCharsetsScanner::create_unique();
189
0
    case TSchemaTableType::SCH_COLLATIONS:
190
0
        return SchemaCollationsScanner::create_unique();
191
0
    case TSchemaTableType::SCH_GLOBAL_VARIABLES:
192
0
        return SchemaVariablesScanner::create_unique(TVarType::GLOBAL);
193
0
    case TSchemaTableType::SCH_SESSION_VARIABLES:
194
0
    case TSchemaTableType::SCH_VARIABLES:
195
0
        return SchemaVariablesScanner::create_unique(TVarType::SESSION);
196
0
    case TSchemaTableType::SCH_VIEWS:
197
0
        return SchemaViewsScanner::create_unique();
198
0
    case TSchemaTableType::SCH_TABLE_PRIVILEGES:
199
0
        return SchemaTablePrivilegesScanner::create_unique();
200
0
    case TSchemaTableType::SCH_SCHEMA_PRIVILEGES:
201
0
        return SchemaSchemaPrivilegesScanner::create_unique();
202
0
    case TSchemaTableType::SCH_USER_PRIVILEGES:
203
0
        return SchemaUserPrivilegesScanner::create_unique();
204
0
    case TSchemaTableType::SCH_FILES:
205
0
        return SchemaFilesScanner::create_unique();
206
0
    case TSchemaTableType::SCH_PARTITIONS:
207
0
        return SchemaPartitionsScanner::create_unique();
208
0
    case TSchemaTableType::SCH_ROWSETS:
209
0
        return SchemaRowsetsScanner::create_unique();
210
0
    case TSchemaTableType::SCH_METADATA_NAME_IDS:
211
0
        return SchemaMetadataNameIdsScanner::create_unique();
212
0
    case TSchemaTableType::SCH_PROFILING:
213
0
        return SchemaProfilingScanner::create_unique();
214
0
    case TSchemaTableType::SCH_BACKEND_ACTIVE_TASKS:
215
0
        return SchemaBackendActiveTasksScanner::create_unique();
216
0
    case TSchemaTableType::SCH_ACTIVE_QUERIES:
217
0
        return SchemaActiveQueriesScanner::create_unique();
218
0
    case TSchemaTableType::SCH_WORKLOAD_GROUPS:
219
0
        return SchemaWorkloadGroupsScanner::create_unique();
220
0
    case TSchemaTableType::SCH_PROCESSLIST:
221
0
        return SchemaProcessListScanner::create_unique();
222
0
    case TSchemaTableType::SCH_PROCEDURES:
223
0
        return SchemaRoutinesScanner::create_unique();
224
0
    case TSchemaTableType::SCH_USER:
225
0
        return SchemaUserScanner::create_unique();
226
0
    case TSchemaTableType::SCH_WORKLOAD_POLICY:
227
0
        return SchemaWorkloadSchedulePolicyScanner::create_unique();
228
0
    case TSchemaTableType::SCH_FILE_CACHE_STATISTICS:
229
0
        return SchemaFileCacheStatisticsScanner::create_unique();
230
0
    case TSchemaTableType::SCH_WORKLOAD_GROUP_PRIVILEGES:
231
0
        return SchemaWorkloadGroupPrivilegesScanner::create_unique();
232
0
    case TSchemaTableType::SCH_WORKLOAD_GROUP_RESOURCE_USAGE:
233
0
        return SchemaBackendWorkloadGroupResourceUsage::create_unique();
234
0
    case TSchemaTableType::SCH_TABLE_PROPERTIES:
235
0
        return SchemaTablePropertiesScanner::create_unique();
236
0
    case TSchemaTableType::SCH_CATALOG_META_CACHE_STATISTICS:
237
0
        return SchemaCatalogMetaCacheStatsScanner::create_unique();
238
0
    case TSchemaTableType::SCH_TABLE_OPTIONS:
239
0
        return SchemaTableOptionsScanner::create_unique();
240
0
    default:
241
0
        return SchemaDummyScanner::create_unique();
242
0
        break;
243
0
    }
244
0
}
245
246
0
void SchemaScanner::_init_block(vectorized::Block* src_block) {
247
0
    const std::vector<SchemaScanner::ColumnDesc>& columns_desc(get_column_desc());
248
0
    for (int i = 0; i < columns_desc.size(); ++i) {
249
0
        TypeDescriptor descriptor(columns_desc[i].type);
250
0
        auto data_type = vectorized::DataTypeFactory::instance().create_data_type(descriptor, true);
251
0
        src_block->insert(vectorized::ColumnWithTypeAndName(data_type->create_column(), data_type,
252
0
                                                            columns_desc[i].name));
253
0
    }
254
0
}
255
256
Status SchemaScanner::fill_dest_column_for_range(vectorized::Block* block, size_t pos,
257
0
                                                 const std::vector<void*>& datas) {
258
0
    const ColumnDesc& col_desc = _columns[pos];
259
0
    vectorized::MutableColumnPtr column_ptr;
260
0
    column_ptr = std::move(*block->get_by_position(pos).column).assume_mutable();
261
0
    vectorized::IColumn* col_ptr = column_ptr.get();
262
263
0
    auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(col_ptr);
264
265
    // Resize in advance to improve insertion efficiency.
266
0
    size_t fill_num = datas.size();
267
0
    col_ptr = &nullable_column->get_nested_column();
268
0
    for (int i = 0; i < fill_num; ++i) {
269
0
        auto* data = datas[i];
270
0
        if (data == nullptr) {
271
            // For nested column need not insert default.
272
0
            nullable_column->insert_data(nullptr, 0);
273
0
            continue;
274
0
        } else {
275
0
            nullable_column->get_null_map_data().emplace_back(0);
276
0
        }
277
0
        switch (col_desc.type) {
278
0
        case TYPE_HLL: {
279
0
            auto* hll_slot = reinterpret_cast<HyperLogLog*>(data);
280
0
            assert_cast<vectorized::ColumnHLL*>(col_ptr)->get_data().emplace_back(*hll_slot);
281
0
            break;
282
0
        }
283
0
        case TYPE_VARCHAR:
284
0
        case TYPE_CHAR:
285
0
        case TYPE_STRING: {
286
0
            auto* str_slot = reinterpret_cast<StringRef*>(data);
287
0
            assert_cast<vectorized::ColumnString*>(col_ptr)->insert_data(str_slot->data,
288
0
                                                                         str_slot->size);
289
0
            break;
290
0
        }
291
292
0
        case TYPE_BOOLEAN: {
293
0
            uint8_t num = *reinterpret_cast<bool*>(data);
294
0
            assert_cast<vectorized::ColumnVector<vectorized::UInt8>*>(col_ptr)->insert_value(num);
295
0
            break;
296
0
        }
297
298
0
        case TYPE_TINYINT: {
299
0
            int8_t num = *reinterpret_cast<int8_t*>(data);
300
0
            assert_cast<vectorized::ColumnVector<vectorized::Int8>*>(col_ptr)->insert_value(num);
301
0
            break;
302
0
        }
303
304
0
        case TYPE_SMALLINT: {
305
0
            int16_t num = *reinterpret_cast<int16_t*>(data);
306
0
            assert_cast<vectorized::ColumnVector<vectorized::Int16>*>(col_ptr)->insert_value(num);
307
0
            break;
308
0
        }
309
310
0
        case TYPE_INT: {
311
0
            int32_t num = *reinterpret_cast<int32_t*>(data);
312
0
            assert_cast<vectorized::ColumnVector<vectorized::Int32>*>(col_ptr)->insert_value(num);
313
0
            break;
314
0
        }
315
316
0
        case TYPE_BIGINT: {
317
0
            int64_t num = *reinterpret_cast<int64_t*>(data);
318
0
            assert_cast<vectorized::ColumnVector<vectorized::Int64>*>(col_ptr)->insert_value(num);
319
0
            break;
320
0
        }
321
322
0
        case TYPE_LARGEINT: {
323
0
            __int128 num;
324
0
            memcpy(&num, data, sizeof(__int128));
325
0
            assert_cast<vectorized::ColumnVector<vectorized::Int128>*>(col_ptr)->insert_value(num);
326
0
            break;
327
0
        }
328
329
0
        case TYPE_FLOAT: {
330
0
            float num = *reinterpret_cast<float*>(data);
331
0
            assert_cast<vectorized::ColumnVector<vectorized::Float32>*>(col_ptr)->insert_value(num);
332
0
            break;
333
0
        }
334
335
0
        case TYPE_DOUBLE: {
336
0
            double num = *reinterpret_cast<double*>(data);
337
0
            assert_cast<vectorized::ColumnVector<vectorized::Float64>*>(col_ptr)->insert_value(num);
338
0
            break;
339
0
        }
340
341
0
        case TYPE_DATE: {
342
0
            assert_cast<vectorized::ColumnVector<vectorized::Int64>*>(col_ptr)->insert_data(
343
0
                    reinterpret_cast<char*>(data), 0);
344
0
            break;
345
0
        }
346
347
0
        case TYPE_DATEV2: {
348
0
            uint32_t num = *reinterpret_cast<uint32_t*>(data);
349
0
            assert_cast<vectorized::ColumnDateV2*>(col_ptr)->insert_value(num);
350
0
            break;
351
0
        }
352
353
0
        case TYPE_DATETIME: {
354
0
            assert_cast<vectorized::ColumnVector<vectorized::Int64>*>(col_ptr)->insert_data(
355
0
                    reinterpret_cast<char*>(data), 0);
356
0
            break;
357
0
        }
358
359
0
        case TYPE_DATETIMEV2: {
360
0
            uint64_t num = *reinterpret_cast<uint64_t*>(data);
361
0
            assert_cast<vectorized::ColumnDateTimeV2*>(col_ptr)->insert_value(num);
362
0
            break;
363
0
        }
364
365
0
        case TYPE_DECIMALV2: {
366
0
            const vectorized::Int128 num = (reinterpret_cast<PackedInt128*>(data))->value;
367
0
            assert_cast<vectorized::ColumnDecimal128V2*>(col_ptr)->insert_data(
368
0
                    reinterpret_cast<const char*>(&num), 0);
369
0
            break;
370
0
        }
371
0
        case TYPE_DECIMAL128I: {
372
0
            const vectorized::Int128 num = (reinterpret_cast<PackedInt128*>(data))->value;
373
0
            assert_cast<vectorized::ColumnDecimal128V3*>(col_ptr)->insert_data(
374
0
                    reinterpret_cast<const char*>(&num), 0);
375
0
            break;
376
0
        }
377
378
0
        case TYPE_DECIMAL32: {
379
0
            const int32_t num = *reinterpret_cast<int32_t*>(data);
380
0
            assert_cast<vectorized::ColumnDecimal32*>(col_ptr)->insert_data(
381
0
                    reinterpret_cast<const char*>(&num), 0);
382
0
            break;
383
0
        }
384
385
0
        case TYPE_DECIMAL64: {
386
0
            const int64_t num = *reinterpret_cast<int64_t*>(data);
387
0
            assert_cast<vectorized::ColumnDecimal64*>(col_ptr)->insert_data(
388
0
                    reinterpret_cast<const char*>(&num), 0);
389
0
            break;
390
0
        }
391
392
0
        default: {
393
0
            DCHECK(false) << "bad slot type: " << col_desc.type;
394
0
            std::stringstream ss;
395
0
            ss << "Fail to convert schema type:'" << col_desc.type << " on column:`"
396
0
               << std::string(col_desc.name) + "`";
397
0
            return Status::InternalError(ss.str());
398
0
        }
399
0
        }
400
0
    }
401
0
    return Status::OK();
402
0
}
403
404
0
std::string SchemaScanner::get_db_from_full_name(const std::string& full_name) {
405
0
    std::vector<std::string> part = split(full_name, ".");
406
0
    if (part.size() == 2) {
407
0
        return part[1];
408
0
    }
409
0
    return full_name;
410
0
}
411
412
Status SchemaScanner::insert_block_column(TCell cell, int col_index, vectorized::Block* block,
413
0
                                          PrimitiveType type) {
414
0
    vectorized::MutableColumnPtr mutable_col_ptr;
415
0
    mutable_col_ptr = std::move(*block->get_by_position(col_index).column).assume_mutable();
416
0
    auto* nullable_column = reinterpret_cast<vectorized::ColumnNullable*>(mutable_col_ptr.get());
417
0
    vectorized::IColumn* col_ptr = &nullable_column->get_nested_column();
418
419
0
    switch (type) {
420
0
    case TYPE_BIGINT: {
421
0
        reinterpret_cast<vectorized::ColumnVector<vectorized::Int64>*>(col_ptr)->insert_value(
422
0
                cell.longVal);
423
0
        nullable_column->get_null_map_data().emplace_back(0);
424
0
        break;
425
0
    }
426
427
0
    case TYPE_INT: {
428
0
        reinterpret_cast<vectorized::ColumnVector<vectorized::Int32>*>(col_ptr)->insert_value(
429
0
                cell.intVal);
430
0
        nullable_column->get_null_map_data().emplace_back(0);
431
0
        break;
432
0
    }
433
434
0
    case TYPE_BOOLEAN: {
435
0
        reinterpret_cast<vectorized::ColumnVector<vectorized::UInt8>*>(col_ptr)->insert_value(
436
0
                cell.boolVal);
437
0
        nullable_column->get_null_map_data().emplace_back(0);
438
0
        break;
439
0
    }
440
441
0
    case TYPE_STRING:
442
0
    case TYPE_VARCHAR:
443
0
    case TYPE_CHAR: {
444
0
        reinterpret_cast<vectorized::ColumnString*>(col_ptr)->insert_data(cell.stringVal.data(),
445
0
                                                                          cell.stringVal.size());
446
0
        nullable_column->get_null_map_data().emplace_back(0);
447
0
        break;
448
0
    }
449
450
0
    case TYPE_DATETIME: {
451
0
        std::vector<void*> datas(1);
452
0
        VecDateTimeValue src[1];
453
0
        src[0].from_date_str(cell.stringVal.data(), cell.stringVal.size());
454
0
        datas[0] = src;
455
0
        auto data = datas[0];
456
0
        reinterpret_cast<vectorized::ColumnVector<vectorized::Int64>*>(col_ptr)->insert_data(
457
0
                reinterpret_cast<char*>(data), 0);
458
0
        nullable_column->get_null_map_data().emplace_back(0);
459
0
        break;
460
0
    }
461
0
    default: {
462
0
        std::stringstream ss;
463
0
        ss << "unsupported column type:" << type;
464
0
        return Status::InternalError(ss.str());
465
0
    }
466
0
    }
467
0
    return Status::OK();
468
0
}
469
470
} // namespace doris