Skip to content

Commit

Permalink
Merge pull request ClickHouse#62319 from mxwell/materialized_prim_key
Browse files Browse the repository at this point in the history
Fix primary key in materialized view
  • Loading branch information
jsc0218 authored Apr 13, 2024
2 parents 6fa57b9 + dab3f55 commit b6cfba3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Storages/StorageMaterializedView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &>());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test id
test_mv
test_mv_pk value
28 changes: 28 additions & 0 deletions tests/queries/0_stateless/03035_materialized_primary_key.sql
Original file line number Diff line number Diff line change
@@ -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%';

0 comments on commit b6cfba3

Please sign in to comment.