diff --git a/src/Databases/DatabasesCommon.cpp b/src/Databases/DatabasesCommon.cpp index 4ba793d858d..3ffa08f8ec7 100644 --- a/src/Databases/DatabasesCommon.cpp +++ b/src/Databases/DatabasesCommon.cpp @@ -149,6 +149,12 @@ ASTPtr getCreateQueryFromStorage(const StoragePtr & storage, const ASTPtr & ast_ return nullptr; } ast_column_declaration->type = ast_type; + + if (auto column_default = metadata_ptr->columns.getDefault(column_name_and_type.name)) + { + ast_column_declaration->default_specifier = toString(column_default->kind); + ast_column_declaration->default_expression = column_default->expression; + } } ast_expression_list->children.emplace_back(ast_column_declaration); } diff --git a/tests/queries/0_stateless/02117_show_create_table_system.reference b/tests/queries/0_stateless/02117_show_create_table_system.reference index 92a38eaf35d..ab074dfb8ba 100644 --- a/tests/queries/0_stateless/02117_show_create_table_system.reference +++ b/tests/queries/0_stateless/02117_show_create_table_system.reference @@ -51,7 +51,7 @@ CREATE TABLE system.clusters `database_shard_name` String, `database_replica_name` String, `is_active` Nullable(UInt8), - `name` String + `name` String ALIAS cluster ) ENGINE = SystemClusters COMMENT 'SYSTEM TABLE is built on the fly.' @@ -134,7 +134,7 @@ CREATE TABLE system.databases `uuid` UUID, `engine_full` String, `comment` String, - `database` String + `database` String ALIAS name ) ENGINE = SystemDatabases COMMENT 'SYSTEM TABLE is built on the fly.' @@ -265,7 +265,7 @@ CREATE TABLE system.events `event` String, `value` UInt64, `description` String, - `name` String + `name` String ALIAS event ) ENGINE = SystemEvents COMMENT 'SYSTEM TABLE is built on the fly.' @@ -376,7 +376,7 @@ CREATE TABLE system.metrics `metric` String, `value` Int64, `description` String, - `name` String + `name` String ALIAS metric ) ENGINE = SystemMetrics COMMENT 'SYSTEM TABLE is built on the fly.' @@ -513,9 +513,9 @@ CREATE TABLE system.parts `has_lightweight_delete` UInt8, `last_removal_attempt_time` DateTime, `removal_state` String, - `bytes` UInt64, - `marks_size` UInt64, - `part_name` String + `bytes` UInt64 ALIAS bytes_on_disk, + `marks_size` UInt64 ALIAS marks_bytes, + `part_name` String ALIAS name ) ENGINE = SystemParts COMMENT 'SYSTEM TABLE is built on the fly.' @@ -569,9 +569,9 @@ CREATE TABLE system.parts_columns `subcolumns.data_compressed_bytes` Array(UInt64), `subcolumns.data_uncompressed_bytes` Array(UInt64), `subcolumns.marks_bytes` Array(UInt64), - `bytes` UInt64, - `marks_size` UInt64, - `part_name` String + `bytes` UInt64 ALIAS bytes_on_disk, + `marks_size` UInt64 ALIAS marks_bytes, + `part_name` String ALIAS name ) ENGINE = SystemPartsColumns COMMENT 'SYSTEM TABLE is built on the fly.' @@ -616,10 +616,10 @@ CREATE TABLE system.processes `ProfileEvents` Map(String, UInt64), `Settings` Map(String, String), `current_database` String, - `ProfileEvents.Names` Array(String), - `ProfileEvents.Values` Array(UInt64), - `Settings.Names` Array(String), - `Settings.Values` Array(String) + `ProfileEvents.Names` Array(String) ALIAS mapKeys(ProfileEvents), + `ProfileEvents.Values` Array(UInt64) ALIAS mapValues(ProfileEvents), + `Settings.Names` Array(String) ALIAS mapKeys(Settings), + `Settings.Values` Array(String) ALIAS mapValues(Settings) ) ENGINE = SystemProcesses COMMENT 'SYSTEM TABLE is built on the fly.' @@ -682,9 +682,9 @@ CREATE TABLE system.projection_parts `rows_where_ttl_info.expression` Array(String), `rows_where_ttl_info.min` Array(DateTime), `rows_where_ttl_info.max` Array(DateTime), - `bytes` UInt64, - `marks_size` UInt64, - `part_name` String + `bytes` UInt64 ALIAS bytes_on_disk, + `marks_size` UInt64 ALIAS marks_bytes, + `part_name` String ALIAS name ) ENGINE = SystemProjectionParts COMMENT 'SYSTEM TABLE is built on the fly.' @@ -738,9 +738,9 @@ CREATE TABLE system.projection_parts_columns `column_data_uncompressed_bytes` UInt64, `column_marks_bytes` UInt64, `column_modification_time` Nullable(DateTime), - `bytes` UInt64, - `marks_size` UInt64, - `part_name` String + `bytes` UInt64 ALIAS bytes_on_disk, + `marks_size` UInt64 ALIAS marks_bytes, + `part_name` String ALIAS name ) ENGINE = SystemProjectionPartsColumns COMMENT 'SYSTEM TABLE is built on the fly.' @@ -1094,7 +1094,7 @@ CREATE TABLE system.tables `loading_dependencies_table` Array(String), `loading_dependent_database` Array(String), `loading_dependent_table` Array(String), - `table` String + `table` String ALIAS name ) ENGINE = SystemTables COMMENT 'SYSTEM TABLE is built on the fly.' diff --git a/tests/queries/0_stateless/02885_create_distributed_table_without_as.reference b/tests/queries/0_stateless/02885_create_distributed_table_without_as.reference new file mode 100644 index 00000000000..4a6b2122663 --- /dev/null +++ b/tests/queries/0_stateless/02885_create_distributed_table_without_as.reference @@ -0,0 +1,5 @@ +CREATE TABLE default.dist_tbl\n(\n `key` UInt32,\n `value` UInt32 DEFAULT 42\n)\nENGINE = Distributed(\'test_shard_localhost\', \'default\', \'local_tbl\', rand()) +local_tbl +99 42 +dist_tbl +99 42 diff --git a/tests/queries/0_stateless/02885_create_distributed_table_without_as.sql b/tests/queries/0_stateless/02885_create_distributed_table_without_as.sql new file mode 100644 index 00000000000..46da94e3727 --- /dev/null +++ b/tests/queries/0_stateless/02885_create_distributed_table_without_as.sql @@ -0,0 +1,16 @@ +-- Here a Distributed table without AS must detect its structure. + +DROP TABLE IF EXISTS dist_tbl; +DROP TABLE IF EXISTS local_tbl; + +CREATE TABLE local_tbl (`key` UInt32, `value` UInt32 DEFAULT 42) ENGINE = MergeTree ORDER BY key; +CREATE TABLE dist_tbl ENGINE = Distributed('test_shard_localhost', currentDatabase(), 'local_tbl', rand()); +SHOW CREATE TABLE dist_tbl; +INSERT INTO dist_tbl (key) SETTINGS insert_distributed_sync=1 VALUES (99); +SELECT 'local_tbl'; +SELECT * FROM local_tbl; +SELECT 'dist_tbl'; +SELECT * FROM dist_tbl; + +DROP TABLE dist_tbl; +DROP TABLE local_tbl;