Merge pull request #24896 from hexiaoting/dev_materialized_view

Add type check when create materialized view with To clause
This commit is contained in:
Yakov Olkhovskiy 2022-04-13 22:33:54 -04:00 committed by GitHub
commit c1a06ac63a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 0 deletions

View File

@ -1071,6 +1071,38 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create)
/// Set and retrieve list of columns, indices and constraints. Set table engine if needed. Rewrite query in canonical way.
TableProperties properties = getTablePropertiesAndNormalizeCreateQuery(create);
/// Check type compatible for materialized dest table and select columns
if (create.select && create.is_materialized_view && create.to_table_id)
{
if (StoragePtr to_table = DatabaseCatalog::instance().tryGetTable(
{create.to_table_id.database_name, create.to_table_id.table_name, create.to_table_id.uuid},
getContext()
))
{
Block input_block = InterpreterSelectWithUnionQuery(
create.select->clone(), getContext(), SelectQueryOptions().analyze()).getSampleBlock();
Block output_block = to_table->getInMemoryMetadataPtr()->getSampleBlock();
ColumnsWithTypeAndName input_columns;
ColumnsWithTypeAndName output_columns;
for (const auto & input_column : input_block)
{
if (const auto * output_column = output_block.findByName(input_column.name))
{
input_columns.push_back(input_column.cloneEmpty());
output_columns.push_back(output_column->cloneEmpty());
}
}
ActionsDAG::makeConvertingActions(
input_columns,
output_columns,
ActionsDAG::MatchColumnsMode::Position
);
}
}
DatabasePtr database;
bool need_add_to_database = !create.temporary;
if (need_add_to_database)

View File

@ -0,0 +1,5 @@
----------test--------:
----------test--------:
100 \0\0\0\0\0\0\0
101 \0\0\0\0\0\0\0
102 \0\0\0\0\0\0\0

View File

@ -0,0 +1,23 @@
DROP TABLE IF EXISTS test_mv;
DROP TABLE IF EXISTS test;
DROP TABLE IF EXISTS test_input;
CREATE TABLE test_input(id Int32) ENGINE=MergeTree() order by id;
CREATE TABLE test(`id` Int32, `pv` AggregateFunction(sum, Int32)) ENGINE = AggregatingMergeTree() ORDER BY id;
CREATE MATERIALIZED VIEW test_mv to test(`id` Int32, `pv` AggregateFunction(sum, Int32)) as SELECT id, sumState(1) as pv from test_input group by id; -- { serverError 70 }
INSERT INTO test_input SELECT toInt32(number % 1000) AS id FROM numbers(10);
select '----------test--------:';
select * from test;
create MATERIALIZED VIEW test_mv to test(`id` Int32, `pv` AggregateFunction(sum, Int32)) as SELECT id, sumState(toInt32(1)) as pv from test_input group by id;
INSERT INTO test_input SELECT toInt32(number % 1000) AS id FROM numbers(100,3);
select '----------test--------:';
select * from test;
DROP TABLE test_mv;
DROP TABLE test;
DROP TABLE test_input;