Merge pull request #62319 from mxwell/materialized_prim_key

Fix primary key in materialized view
This commit is contained in:
jsc0218 2024-04-13 03:37:20 +00:00 committed by GitHub
commit b6cfba33f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 0 deletions

View File

@ -91,6 +91,12 @@ StorageMaterializedView::StorageMaterializedView(
{
StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(columns_);
auto * storage_def = query.storage;
if (storage_def && storage_def->primary_key)
storage_metadata.primary_key = KeyDescription::getKeyFromAST(storage_def->primary_key->ptr(),
storage_metadata.columns,
local_context->getGlobalContext());
if (query.sql_security)
storage_metadata.setSQLSecurity(query.sql_security->as<ASTSQLSecurity &>());

View File

@ -0,0 +1,3 @@
test id
test_mv
test_mv_pk value

View File

@ -0,0 +1,28 @@
DROP TABLE IF EXISTS test;
CREATE TABLE test
(
id UInt64,
value String
) ENGINE=MergeTree ORDER BY id;
INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob');
DROP VIEW IF EXISTS test_mv;
CREATE MATERIALIZED VIEW test_mv
(
id UInt64,
value String
) ENGINE=MergeTree
ORDER BY id AS SELECT id, value FROM test;
DROP VIEW IF EXISTS test_mv_pk;
CREATE MATERIALIZED VIEW test_mv_pk
(
value String,
id UInt64
) ENGINE=MergeTree PRIMARY KEY value
POPULATE AS SELECT value, id FROM test;
SELECT name, primary_key
FROM system.tables
WHERE database = currentDatabase() AND name LIKE 'test%';