2013-11-08 17:43:03 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
|
#include <DB/DataStreams/IBlockOutputStream.h>
|
|
|
|
|
#include <DB/DataStreams/OneBlockInputStream.h>
|
2013-11-20 16:02:45 +00:00
|
|
|
|
#include <DB/DataStreams/MaterializingBlockInputStream.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
#include <DB/Interpreters/InterpreterSelectQuery.h>
|
2013-11-15 09:43:50 +00:00
|
|
|
|
#include <DB/Storages/StorageView.h>
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Записывает данные в указанную таблицу, при этом рекурсивно вызываясь от всех зависимых вьюшек.
|
|
|
|
|
* Если вьюшка не материализованная, то в нее данные не записываются, лишь перенаправляются дальше.
|
|
|
|
|
*/
|
2013-11-13 14:39:48 +00:00
|
|
|
|
class PushingToViewsBlockOutputStream : public IBlockOutputStream
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2014-12-23 20:32:00 +00:00
|
|
|
|
PushingToViewsBlockOutputStream(String database, String table, const Context & context_, ASTPtr query_ptr_)
|
|
|
|
|
: context(context_), query_ptr(query_ptr_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
|
|
|
|
storage = context.getTable(database, table);
|
2015-09-18 00:46:36 +00:00
|
|
|
|
|
|
|
|
|
/** TODO Это очень важная строчка. При любой вставке в таблицу один из stream-ов должен владеть lock-ом.
|
|
|
|
|
* Хотя сейчас любая вставка в таблицу делается через PushingToViewsBlockOutputStream,
|
|
|
|
|
* но ясно, что здесь - не лучшее место для этой функциональности.
|
|
|
|
|
*/
|
2014-03-20 10:59:45 +00:00
|
|
|
|
addTableLock(storage->lockStructure(true));
|
|
|
|
|
|
2014-12-23 20:32:00 +00:00
|
|
|
|
Dependencies dependencies = context.getDependencies(database, table);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
for (size_t i = 0; i < dependencies.size(); ++i)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2016-05-28 12:22:22 +00:00
|
|
|
|
children.push_back(std::make_shared<PushingToViewsBlockOutputStream>(dependencies[i].first, dependencies[i].second, context, ASTPtr()));
|
2013-11-08 17:43:03 +00:00
|
|
|
|
queries.push_back(dynamic_cast<StorageView &>(*context.getTable(dependencies[i].first, dependencies[i].second)).getInnerQuery());
|
|
|
|
|
}
|
2014-04-14 09:42:32 +00:00
|
|
|
|
|
|
|
|
|
if (storage->getName() != "View")
|
2015-09-10 20:43:42 +00:00
|
|
|
|
output = storage->write(query_ptr, context.getSettingsRef());
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void write(const Block & block) override
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
for (size_t i = 0; i < children.size(); ++i)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2016-05-28 12:22:22 +00:00
|
|
|
|
BlockInputStreamPtr from = std::make_shared<OneBlockInputStream>(block);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
InterpreterSelectQuery select(queries[i], context, QueryProcessingStage::Complete, 0, from);
|
2016-05-28 12:22:22 +00:00
|
|
|
|
BlockInputStreamPtr data = std::make_shared<MaterializingBlockInputStream>(select.execute().in);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
copyData(*data, *children[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-14 09:42:32 +00:00
|
|
|
|
if (output)
|
2014-09-18 19:49:31 +00:00
|
|
|
|
output->write(block);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 00:52:03 +00:00
|
|
|
|
void flush() override
|
|
|
|
|
{
|
|
|
|
|
if (output)
|
|
|
|
|
output->flush();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 19:49:31 +00:00
|
|
|
|
void writePrefix() override
|
|
|
|
|
{
|
|
|
|
|
if (output)
|
|
|
|
|
output->writePrefix();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeSuffix() override
|
|
|
|
|
{
|
|
|
|
|
if (output)
|
|
|
|
|
output->writeSuffix();
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StoragePtr storage;
|
2014-04-14 09:42:32 +00:00
|
|
|
|
BlockOutputStreamPtr output;
|
2013-11-08 17:43:03 +00:00
|
|
|
|
Context context;
|
|
|
|
|
ASTPtr query_ptr;
|
|
|
|
|
std::vector<BlockOutputStreamPtr> children;
|
|
|
|
|
std::vector<ASTPtr> queries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|