ClickHouse/src/Storages/System/StorageSystemQueryCache.cpp

58 lines
1.9 KiB
C++
Raw Normal View History

#include "StorageSystemQueryCache.h"
2022-11-29 13:15:28 +00:00
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/Cache/QueryCache.h>
2022-11-29 13:15:28 +00:00
#include <Interpreters/Context.h>
namespace DB
{
NamesAndTypesList StorageSystemQueryCache::getNamesAndTypes()
2022-11-29 13:15:28 +00:00
{
return {
{"query", std::make_shared<DataTypeString>()},
{"result_size", std::make_shared<DataTypeUInt64>()},
2022-11-29 13:15:28 +00:00
{"stale", std::make_shared<DataTypeUInt8>()},
{"shared", std::make_shared<DataTypeUInt8>()},
{"compressed", std::make_shared<DataTypeUInt8>()},
{"expires_at", std::make_shared<DataTypeDateTime>()},
{"key_hash", std::make_shared<DataTypeUInt64>()}
2022-11-29 13:15:28 +00:00
};
}
StorageSystemQueryCache::StorageSystemQueryCache(const StorageID & table_id_)
2022-11-29 13:15:28 +00:00
: IStorageSystemOneBlock(table_id_)
{
}
void StorageSystemQueryCache::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
2022-11-29 13:15:28 +00:00
{
auto query_cache = context->getQueryCache();
2022-11-29 13:15:28 +00:00
if (!query_cache)
2022-11-29 13:15:28 +00:00
return;
2023-03-13 09:34:03 +00:00
std::vector<QueryCache::Cache::KeyMapped> content = query_cache->dump();
2022-12-18 12:37:44 +00:00
2023-03-31 10:37:11 +00:00
const String & user_name = context->getUserName();
2022-11-29 13:15:28 +00:00
2023-03-13 09:34:03 +00:00
for (const auto & [key, query_result] : content)
2022-11-29 13:15:28 +00:00
{
/// Showing other user's queries is considered a security risk
2023-03-31 10:37:11 +00:00
if (!key.is_shared && key.user_name != user_name)
continue;
2022-11-29 13:15:28 +00:00
res_columns[0]->insert(key.queryStringFromAst()); /// approximates the original query string
res_columns[1]->insert(QueryCache::QueryCacheEntryWeight()(*query_result));
res_columns[2]->insert(key.expires_at < std::chrono::system_clock::now());
2023-03-31 10:37:11 +00:00
res_columns[3]->insert(!key.is_shared);
res_columns[4]->insert(key.is_compressed);
res_columns[5]->insert(std::chrono::system_clock::to_time_t(key.expires_at));
res_columns[6]->insert(key.ast->getTreeHash().first);
2022-11-29 13:15:28 +00:00
}
}
}