2023-01-26 22:23:10 +00:00
|
|
|
#include "StorageSystemQueryCache.h"
|
2022-11-29 13:15:28 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2023-01-26 22:23:10 +00:00
|
|
|
#include <Interpreters/Cache/QueryCache.h>
|
2022-11-29 13:15:28 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-01-26 22:23:10 +00:00
|
|
|
NamesAndTypesList StorageSystemQueryCache::getNamesAndTypes()
|
2022-11-29 13:15:28 +00:00
|
|
|
{
|
|
|
|
return {
|
|
|
|
{"query", std::make_shared<DataTypeString>()},
|
2023-04-03 10:33:10 +00:00
|
|
|
{"result_size", std::make_shared<DataTypeUInt64>()},
|
2022-11-29 13:15:28 +00:00
|
|
|
{"stale", std::make_shared<DataTypeUInt8>()},
|
2022-12-17 18:18:37 +00:00
|
|
|
{"shared", std::make_shared<DataTypeUInt8>()},
|
2023-04-03 10:33:10 +00:00
|
|
|
{"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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:23:10 +00:00
|
|
|
StorageSystemQueryCache::StorageSystemQueryCache(const StorageID & table_id_)
|
2022-11-29 13:15:28 +00:00
|
|
|
: IStorageSystemOneBlock(table_id_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-26 22:23:10 +00:00
|
|
|
void StorageSystemQueryCache::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
|
2022-11-29 13:15:28 +00:00
|
|
|
{
|
2023-01-26 22:23:10 +00:00
|
|
|
auto query_cache = context->getQueryCache();
|
2022-11-29 13:15:28 +00:00
|
|
|
|
2023-01-26 22:23:10 +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
|
|
|
{
|
2022-12-17 15:14:55 +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)
|
2022-12-17 15:14:55 +00:00
|
|
|
continue;
|
|
|
|
|
2022-11-29 13:15:28 +00:00
|
|
|
res_columns[0]->insert(key.queryStringFromAst()); /// approximates the original query string
|
2023-04-20 13:23:08 +00:00
|
|
|
res_columns[1]->insert(QueryCache::QueryCacheEntryWeight()(*query_result));
|
2023-04-03 10:33:10 +00:00
|
|
|
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);
|
2023-03-14 11:57:51 +00:00
|
|
|
res_columns[4]->insert(key.is_compressed);
|
2023-04-03 10:33:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|