ClickHouse/src/Storages/System/StorageSystemQueryCache.cpp

56 lines
1.8 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>()},
2023-01-10 09:39:45 +00:00
{"key_hash", std::make_shared<DataTypeUInt64>()},
2022-11-29 13:15:28 +00:00
{"expires_at", std::make_shared<DataTypeDateTime>()},
{"stale", std::make_shared<DataTypeUInt8>()},
{"shared", std::make_shared<DataTypeUInt8>()},
2022-11-29 13:15:28 +00:00
{"result_size", std::make_shared<DataTypeUInt64>()}
};
}
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;
2022-12-18 12:37:44 +00:00
const String & username = context->getUserName();
std::lock_guard lock(query_cache->mutex);
2022-11-29 13:15:28 +00:00
for (const auto & [key, result] : query_cache->cache)
2022-11-29 13:15:28 +00:00
{
/// Showing other user's queries is considered a security risk
2022-12-18 12:37:44 +00:00
if (key.username.has_value() && key.username != username)
continue;
2022-11-29 13:15:28 +00:00
res_columns[0]->insert(key.queryStringFromAst()); /// approximates the original query string
res_columns[1]->insert(key.ast->getTreeHash().first);
res_columns[2]->insert(std::chrono::system_clock::to_time_t(key.expires_at));
res_columns[3]->insert(key.expires_at < std::chrono::system_clock::now());
res_columns[4]->insert(!key.username.has_value());
res_columns[5]->insert(result.sizeInBytes());
2022-11-29 13:15:28 +00:00
}
}
}