#include "StorageSystemQueryCache.h" #include #include #include #include #include namespace DB { NamesAndTypesList StorageSystemQueryCache::getNamesAndTypes() { return { {"query", std::make_shared()}, {"key_hash", std::make_shared()}, {"expires_at", std::make_shared()}, {"stale", std::make_shared()}, {"compressed", std::make_shared()}, {"shared", std::make_shared()}, {"result_size", std::make_shared()} }; } StorageSystemQueryCache::StorageSystemQueryCache(const StorageID & table_id_) : IStorageSystemOneBlock(table_id_) { } void StorageSystemQueryCache::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const { auto query_cache = context->getQueryCache(); if (!query_cache) return; std::vector content = query_cache->dump(); const String & username = context->getUserName(); for (const auto & [key, query_result] : content) { /// Showing other user's queries is considered a security risk if (key.username.has_value() && key.username != username) continue; 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.is_compressed); res_columns[5]->insert(!key.username.has_value()); res_columns[6]->insert(QueryCache::QueryResultWeight()(*query_result)); } } }