correct column list for rewritten INSERT query into Distributed [#CLICKHOUSE-4161]

This commit is contained in:
Alexey Zatelepin 2018-11-23 20:39:16 +03:00
parent ebfdd0e7f9
commit 01501fa8db

View File

@ -23,6 +23,7 @@
#include <Parsers/ASTExpressionList.h> #include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTTablesInSelectQuery.h> #include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTDropQuery.h> #include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTIdentifier.h>
#include <Interpreters/InterpreterSelectQuery.h> #include <Interpreters/InterpreterSelectQuery.h>
#include <Interpreters/InterpreterAlterQuery.h> #include <Interpreters/InterpreterAlterQuery.h>
@ -75,25 +76,22 @@ ASTPtr rewriteSelectQuery(const ASTPtr & query, const std::string & database, co
return modified_query_ast; return modified_query_ast;
} }
/// insert query has database and table names as bare strings /// The columns list in the original INSERT query is incorrect because inserted blocks are transformed
/// If the query is null, it creates a insert query with the database and tables /// to the form of the sample block of the Distributed table. So we rewrite it and add all columns from
/// Or it creates a copy of query, changes the database and table names. /// the sample block instead.
ASTPtr rewriteInsertQuery(const ASTPtr & query, const std::string & database, const std::string & table) ASTPtr createInsertToRemoteTableQuery(const std::string & database, const std::string & table, const Block & sample_block)
{ {
ASTPtr modified_query_ast = nullptr; auto query = std::make_shared<ASTInsertQuery>();
if (query == nullptr) query->database = database;
modified_query_ast = std::make_shared<ASTInsertQuery>(); query->table = table;
else
modified_query_ast = query->clone();
auto & actual_query = typeid_cast<ASTInsertQuery &>(*modified_query_ast); auto columns = std::make_shared<ASTExpressionList>();
actual_query.database = database; query->columns = columns;
actual_query.table = table; query->children.push_back(columns);
actual_query.table_function = nullptr; for (const auto & col : sample_block)
/// make sure query is not INSERT SELECT columns->children.push_back(std::make_shared<ASTIdentifier>(col.name));
actual_query.select = nullptr;
return modified_query_ast; return query;
} }
/// Calculate maximum number in file names in directory and all subdirectories. /// Calculate maximum number in file names in directory and all subdirectories.
@ -274,7 +272,7 @@ BlockInputStreams StorageDistributed::read(
} }
BlockOutputStreamPtr StorageDistributed::write(const ASTPtr & query, const Settings & settings) BlockOutputStreamPtr StorageDistributed::write(const ASTPtr &, const Settings & settings)
{ {
auto cluster = getCluster(); auto cluster = getCluster();
@ -298,7 +296,7 @@ BlockOutputStreamPtr StorageDistributed::write(const ASTPtr & query, const Setti
/// DistributedBlockOutputStream will not own cluster, but will own ConnectionPools of the cluster /// DistributedBlockOutputStream will not own cluster, but will own ConnectionPools of the cluster
return std::make_shared<DistributedBlockOutputStream>( return std::make_shared<DistributedBlockOutputStream>(
*this, rewriteInsertQuery(query, remote_database, remote_table), cluster, settings, insert_sync, timeout); *this, createInsertToRemoteTableQuery(remote_database, remote_table, getSampleBlock()), cluster, settings, insert_sync, timeout);
} }