Skip to content

Commit e527def

Browse files
committed
Fix INSERT into Distributed() table with MATERIALIZED column
By just skipping MATERIALIZED columns during processing. P.S. you cannot use insert_allow_materialized_columns since it works only for Buffer() engine. Fixes: #4015 Fixes: #3673 Fixes: 01501fa ("correct column list for rewritten INSERT query into Distributed [#CLICKHOUSE-4161]")
1 parent b579c98 commit e527def

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

dbms/src/Storages/StorageDistributed.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,21 @@ ASTPtr rewriteSelectQuery(const ASTPtr & query, const std::string & database, co
8484
/// The columns list in the original INSERT query is incorrect because inserted blocks are transformed
8585
/// to the form of the sample block of the Distributed table. So we rewrite it and add all columns from
8686
/// the sample block instead.
87-
ASTPtr createInsertToRemoteTableQuery(const std::string & database, const std::string & table, const Block & sample_block)
87+
ASTPtr createInsertToRemoteTableQuery(const std::string & database, const std::string & table, const Block & sample_block, const ColumnsDescription & columns)
8888
{
8989
auto query = std::make_shared<ASTInsertQuery>();
9090
query->database = database;
9191
query->table = table;
9292

93-
auto columns = std::make_shared<ASTExpressionList>();
94-
query->columns = columns;
95-
query->children.push_back(columns);
93+
auto block_columns = std::make_shared<ASTExpressionList>();
94+
query->columns = block_columns;
95+
query->children.push_back(block_columns);
9696
for (const auto & col : sample_block)
97-
columns->children.push_back(std::make_shared<ASTIdentifier>(col.name));
97+
{
98+
if (columns.get(col.name).default_desc.kind == DB::ColumnDefaultKind::Materialized)
99+
continue;
100+
block_columns->children.push_back(std::make_shared<ASTIdentifier>(col.name));
101+
}
98102

99103
return query;
100104
}
@@ -331,9 +335,11 @@ BlockOutputStreamPtr StorageDistributed::write(const ASTPtr &, const Context & c
331335
bool insert_sync = settings.insert_distributed_sync || owned_cluster;
332336
auto timeout = settings.insert_distributed_timeout;
333337

338+
const ColumnsDescription & columns = getColumns();
339+
334340
/// DistributedBlockOutputStream will not own cluster, but will own ConnectionPools of the cluster
335341
return std::make_shared<DistributedBlockOutputStream>(
336-
context, *this, createInsertToRemoteTableQuery(remote_database, remote_table, getSampleBlock()), cluster,
342+
context, *this, createInsertToRemoteTableQuery(remote_database, remote_table, getSampleBlock(), columns), cluster,
337343
insert_sync, timeout);
338344
}
339345

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2018-08-01
2+
2018-08-01
3+
2018-08-01 2017-08-01
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DROP TABLE IF EXISTS test.test_local;
2+
DROP TABLE IF EXISTS test.test_distributed;
3+
4+
CREATE TABLE test.test_local (date Date, value Date MATERIALIZED toDate('2017-08-01')) ENGINE = MergeTree(date, date, 8192);
5+
CREATE TABLE test.test_distributed AS test.test_local ENGINE = Distributed('test_shard_localhost', 'test', test_local, rand());
6+
7+
SET insert_distributed_sync=1;
8+
9+
INSERT INTO test.test_distributed VALUES ('2018-08-01');
10+
SELECT * FROM test.test_distributed;
11+
SELECT * FROM test.test_local;
12+
SELECT date, value FROM test.test_local;

0 commit comments

Comments
 (0)