Add table system.symbols

This commit is contained in:
Alexey Milovidov 2023-11-06 01:11:53 +01:00
parent 0f76ba83e0
commit 8182d51978
3 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,111 @@
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Storages/System/StorageSystemSymbols.h>
#include <Storages/System/getQueriedColumnsMaskAndHeader.h>
#include <Access/ContextAccess.h>
#include <Interpreters/Context.h>
#include <Processors/ISource.h>
#include <QueryPipeline/Pipe.h>
#include <Common/SymbolIndex.h>
namespace DB
{
StorageSystemSymbols::StorageSystemSymbols(const StorageID & table_id_)
: IStorage(table_id_)
{
StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(ColumnsDescription(
{
{"symbol", std::make_shared<DataTypeString>()},
{"address_begin", std::make_shared<DataTypeUInt64>()},
{"address_end", std::make_shared<DataTypeUInt64>()},
}));
setInMemoryMetadata(storage_metadata);
}
namespace
{
class SymbolsBlockSource : public ISource
{
private:
using Iterator = std::vector<SymbolIndex::Symbol>::const_iterator;
Iterator it;
const Iterator end;
std::vector<UInt8> columns_mask;
UInt64 max_block_size;
public:
SymbolsBlockSource(
Iterator begin_,
Iterator end_,
std::vector<UInt8> columns_mask_,
Block header,
UInt64 max_block_size_)
: ISource(std::move(header))
, it(begin_), end(end_), columns_mask(std::move(columns_mask_)), max_block_size(max_block_size_)
{
}
String getName() const override { return "Symbols"; }
protected:
Chunk generate() override
{
if (it == end)
return {};
MutableColumns res_columns = getPort().getHeader().cloneEmptyColumns();
size_t rows_count = 0;
while (rows_count < max_block_size && it != end)
{
size_t src_index = 0;
size_t res_index = 0;
if (columns_mask[src_index++])
res_columns[res_index++]->insert(it->name);
if (columns_mask[src_index++])
res_columns[res_index++]->insert(reinterpret_cast<uintptr_t>(it->address_begin));
if (columns_mask[src_index++])
res_columns[res_index++]->insert(reinterpret_cast<uintptr_t>(it->address_end));
++rows_count;
++it;
}
return Chunk(std::move(res_columns), rows_count);
}
};
}
Pipe StorageSystemSymbols::read(
const Names & column_names,
const StorageSnapshotPtr & storage_snapshot,
SelectQueryInfo & /* query_info */,
ContextPtr context,
QueryProcessingStage::Enum /*processed_stage*/,
const size_t max_block_size,
const size_t /*num_streams*/)
{
context->getAccess()->checkAccess(AccessType::INTROSPECTION);
storage_snapshot->check(column_names);
Block sample_block = storage_snapshot->metadata->getSampleBlock();
auto [columns_mask, res_block] = getQueriedColumnsMaskAndHeader(sample_block, column_names);
const auto & symbols = SymbolIndex::instance().symbols();
return Pipe(std::make_shared<SymbolsBlockSource>(
symbols.cbegin(), symbols.cend(), std::move(columns_mask), std::move(res_block), max_block_size));
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <Storages/IStorage.h>
namespace DB
{
class Context;
/** Implements the system table `symbols` for introspection of symbols in the ClickHouse binary.
*/
class StorageSystemSymbols final : public IStorage
{
public:
explicit StorageSystemSymbols(const StorageID & table_id_);
std::string getName() const override { return "SystemSymbols"; }
Pipe read(
const Names & column_names,
const StorageSnapshotPtr & storage_snapshot,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
size_t num_streams) override;
bool isSystemStorage() const override { return true; }
};
}

View File

@ -85,6 +85,7 @@
#include <Storages/System/StorageSystemJemalloc.h> #include <Storages/System/StorageSystemJemalloc.h>
#include <Storages/System/StorageSystemScheduler.h> #include <Storages/System/StorageSystemScheduler.h>
#include <Storages/System/StorageSystemS3Queue.h> #include <Storages/System/StorageSystemS3Queue.h>
#include <Storages/System/StorageSystemSymbols.h>
#if USE_RDKAFKA #if USE_RDKAFKA
#include <Storages/System/StorageSystemKafkaConsumers.h> #include <Storages/System/StorageSystemKafkaConsumers.h>
@ -151,6 +152,7 @@ void attachSystemTablesLocal(ContextPtr context, IDatabase & system_database)
attach<StorageSystemSchemaInferenceCache>(context, system_database, "schema_inference_cache"); attach<StorageSystemSchemaInferenceCache>(context, system_database, "schema_inference_cache");
attach<StorageSystemDroppedTables>(context, system_database, "dropped_tables"); attach<StorageSystemDroppedTables>(context, system_database, "dropped_tables");
attach<StorageSystemScheduler>(context, system_database, "scheduler"); attach<StorageSystemScheduler>(context, system_database, "scheduler");
attach<StorageSystemSymbols>(context, system_database, "symbols");
#if USE_RDKAFKA #if USE_RDKAFKA
attach<StorageSystemKafkaConsumers>(context, system_database, "kafka_consumers"); attach<StorageSystemKafkaConsumers>(context, system_database, "kafka_consumers");
#endif #endif