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:
|
2013-11-13 14:39:48 +00:00
|
|
|
|
PushingToViewsBlockOutputStream(String database_, String table_, const Context &context_, ASTPtr query_ptr_)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
:database(database_), table(table_), context(context_), query_ptr(query_ptr_)
|
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
if (database.empty())
|
|
|
|
|
database = context.getCurrentDatabase();
|
2013-11-08 17:43:03 +00:00
|
|
|
|
storage = context.getTable(database, table);
|
2013-11-13 14:39:48 +00:00
|
|
|
|
Dependencies dependencies = context.getDependencies(DatabaseAndTableName(database, table));
|
|
|
|
|
for (size_t i = 0; i < dependencies.size(); ++i)
|
2013-11-08 17:43:03 +00:00
|
|
|
|
{
|
2013-11-13 14:39:48 +00:00
|
|
|
|
children.push_back(new 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-13 14:39:48 +00:00
|
|
|
|
String getName() const { return "PushingToViewsBlockOutputStream"; }
|
2013-11-08 17:43:03 +00:00
|
|
|
|
|
|
|
|
|
void write(const Block & block)
|
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
|
BlockInputStreamPtr from = new OneBlockInputStream(block);
|
|
|
|
|
InterpreterSelectQuery select(queries[i], context, QueryProcessingStage::Complete, 0, from);
|
2013-11-20 16:02:45 +00:00
|
|
|
|
BlockInputStreamPtr data = new MaterializingBlockInputStream(select.execute());
|
2013-11-08 17:43:03 +00:00
|
|
|
|
copyData(*data, *children[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-20 16:02:45 +00:00
|
|
|
|
Block buf(block);
|
|
|
|
|
if (storage->getName() != "View")
|
|
|
|
|
storage->write(query_ptr)->write(buf);
|
2013-11-08 17:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
StoragePtr storage;
|
|
|
|
|
String database;
|
|
|
|
|
String table;
|
|
|
|
|
Context context;
|
|
|
|
|
ASTPtr query_ptr;
|
|
|
|
|
std::vector<BlockOutputStreamPtr> children;
|
|
|
|
|
std::vector<ASTPtr> queries;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|