ClickHouse/src/Storages/System/StorageSystemQueryResultCache.cpp

56 lines
1.8 KiB
C++
Raw Normal View History

2022-11-29 13:15:28 +00:00
#include "StorageSystemQueryResultCache.h"
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/Cache/QueryResultCache.h>
#include <Interpreters/Context.h>
namespace DB
{
NamesAndTypesList StorageSystemQueryResultCache::getNamesAndTypes()
{
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>()}
};
}
StorageSystemQueryResultCache::StorageSystemQueryResultCache(const StorageID & table_id_)
: IStorageSystemOneBlock(table_id_)
{
}
void StorageSystemQueryResultCache::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
{
auto query_result_cache = context->getQueryResultCache();
if (!query_result_cache)
return;
2022-12-18 12:37:44 +00:00
const String & username = context->getUserName();
2022-11-29 13:15:28 +00:00
std::lock_guard lock(query_result_cache->mutex);
2023-01-02 11:08:49 +00:00
for (const auto & [key, result] : query_result_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
}
}
}