aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Storages/System/StorageSystemQueryCache.cpp
blob: 03757101ddfe5b9edc507896fea487a790b386f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "StorageSystemQueryCache.h"
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypesNumber.h>
#include <Interpreters/Cache/QueryCache.h>
#include <Interpreters/Context.h>


namespace DB
{

NamesAndTypesList StorageSystemQueryCache::getNamesAndTypes()
{
    return {
        {"query", std::make_shared<DataTypeString>()},
        {"result_size", std::make_shared<DataTypeUInt64>()},
        {"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>()}
    };
}

StorageSystemQueryCache::StorageSystemQueryCache(const StorageID & table_id_)
    : IStorageSystemOneBlock(table_id_)
{
}

void StorageSystemQueryCache::fillData(MutableColumns & res_columns, ContextPtr context, const SelectQueryInfo &) const
{
    QueryCachePtr query_cache = context->getQueryCache();

    if (!query_cache)
        return;

    std::vector<QueryCache::Cache::KeyMapped> content = query_cache->dump();

    const String & user_name = context->getUserName();

    for (const auto & [key, query_result] : content)
    {
        /// Showing other user's queries is considered a security risk
        if (!key.is_shared && key.user_name != user_name)
            continue;

        res_columns[0]->insert(key.query_string); /// 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());
        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().low64);
    }
}

}