Try make cache for getSampleBlock (#2313)

* Cache for getSampleBlock

* Update InterpreterSelectWithUnionQuery.cpp

* Update 00632_get_sample_block_cache.sql

* Add tests
This commit is contained in:
proller 2018-05-30 22:23:15 +03:00 committed by alexey-milovidov
parent ead9493741
commit dc721ed721
5 changed files with 196 additions and 3 deletions

View File

@ -1732,6 +1732,11 @@ void Context::setFormatSchemaPath(const String & path)
shared->format_schema_path = path;
}
Context::getSampleBlockCacheType & Context::getSampleBlockCache() const
{
return getQueryContext().get_sample_block_cache;
}
std::shared_ptr<ActionLocksManager> Context::getActionLocksManager()
{
auto lock = getLock();
@ -1742,7 +1747,6 @@ std::shared_ptr<ActionLocksManager> Context::getActionLocksManager()
return shared->action_locks_manager;
}
SessionCleaner::~SessionCleaner()
{
try

View File

@ -10,6 +10,7 @@
#include <common/MultiVersion.h>
#include <Core/Types.h>
#include <Core/NamesAndTypes.h>
#include <Core/Block.h>
#include <Interpreters/Settings.h>
#include <Interpreters/ClientInfo.h>
#include <IO/CompressionSettings.h>
@ -398,6 +399,10 @@ public:
/// User name and session identifier. Named sessions are local to users.
using SessionKey = std::pair<String, String>;
using getSampleBlockCacheType = std::unordered_map<std::string, Block>;
mutable Context::getSampleBlockCacheType get_sample_block_cache;
getSampleBlockCacheType & getSampleBlockCache() const;
private:
/** Check if the current client has access to the specified database.
* If access is denied, throw an exception.

View File

@ -9,7 +9,7 @@
#include <DataTypes/getLeastSupertype.h>
#include <Columns/ColumnConst.h>
#include <Common/typeid_cast.h>
#include <Parsers/queryToString.h>
namespace DB
{
@ -157,7 +157,15 @@ Block InterpreterSelectWithUnionQuery::getSampleBlock(
const ASTPtr & query_ptr,
const Context & context)
{
return InterpreterSelectWithUnionQuery(query_ptr, context).getSampleBlock();
auto & cache = context.getSampleBlockCache();
/// Using query string because query_ptr changes for every internal SELECT
auto key = queryToString(query_ptr);
if (cache.find(key) != cache.end())
{
return cache[key];
}
return cache[key] = InterpreterSelectWithUnionQuery(query_ptr, context).getSampleBlock();
}

View File

@ -0,0 +1,2 @@
Still alive
65535

File diff suppressed because one or more lines are too long