diff --git a/dbms/src/Storages/StorageMerge.cpp b/dbms/src/Storages/StorageMerge.cpp index 63a2a66afaa..a7d235da595 100644 --- a/dbms/src/Storages/StorageMerge.cpp +++ b/dbms/src/Storages/StorageMerge.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -203,6 +204,12 @@ BlockInputStreams StorageMerge::read( */ StorageListWithLocks selected_tables = getSelectedTables(query_info.query, has_table_virtual_column, true); + if (selected_tables.empty()) + { + BlockInputStreams streams{std::make_shared(header)}; + return streams; + } + size_t remaining_streams = num_streams; size_t tables_count = selected_tables.size(); @@ -220,16 +227,16 @@ BlockInputStreams StorageMerge::read( if (current_streams) { source_streams = createSourceStreams( - query_info, processed_stage, max_block_size, real_column_names, modified_context, header, storage, - struct_lock, current_streams, has_table_virtual_column); + query_info, processed_stage, max_block_size, modified_context, header, storage, + struct_lock, real_column_names, current_streams, has_table_virtual_column); } else { - source_streams.emplace_back(std::make_shared(header, [=]() -> BlockInputStreamPtr + source_streams.emplace_back(std::make_shared(header, [=, &real_column_names]() -> BlockInputStreamPtr { - BlockInputStreams streams = createSourceStreams(query_info, processed_stage, max_block_size, real_column_names, + BlockInputStreams streams = createSourceStreams(query_info, processed_stage, max_block_size, modified_context, header, storage, - struct_lock, current_streams, has_table_virtual_column); + struct_lock, real_column_names, current_streams, has_table_virtual_column); if (streams.size() != 1) throw Exception("LogicalError: the lazy stream size must to be one.", ErrorCodes::LOGICAL_ERROR); @@ -249,10 +256,10 @@ BlockInputStreams StorageMerge::read( } BlockInputStreams StorageMerge::createSourceStreams(const SelectQueryInfo & query_info, const QueryProcessingStage::Enum & processed_stage, - const size_t max_block_size, const Names & real_column_names, - const Context & modified_context, const Block & header, const StoragePtr & storage, - const TableStructureReadLockPtr & struct_lock, size_t current_streams, - bool has_table_virtual_column) const + const size_t max_block_size, const Context & modified_context, + const Block & header, const StoragePtr & storage, + const TableStructureReadLockPtr & struct_lock, Names & real_column_names, + size_t current_streams, bool has_table_virtual_column) { SelectQueryInfo modified_query_info; modified_query_info.query = query_info.query->clone(); @@ -264,6 +271,10 @@ BlockInputStreams StorageMerge::createSourceStreams(const SelectQueryInfo & quer if (processed_stage <= storage->getQueryProcessingStage(modified_context)) { + /// If there are only virtual columns in query, you must request at least one other column. + if (real_column_names.size() ==0) + real_column_names.push_back(ExpressionActions::getSmallestColumn(storage->getColumns().getAllPhysical())); + source_streams = storage->read(real_column_names, modified_query_info, modified_context, processed_stage, max_block_size, current_streams); } @@ -282,6 +293,12 @@ BlockInputStreams StorageMerge::createSourceStreams(const SelectQueryInfo & quer source_streams.emplace_back(std::make_shared(interpreter_stream)); } + if (source_streams.empty()) + { + source_streams.emplace_back(std::make_shared(header)); + return source_streams; + } + if (!current_streams) { BlockInputStreamPtr stream = diff --git a/dbms/src/Storages/StorageMerge.h b/dbms/src/Storages/StorageMerge.h index a4570bacf30..ca636fd4237 100644 --- a/dbms/src/Storages/StorageMerge.h +++ b/dbms/src/Storages/StorageMerge.h @@ -75,9 +75,9 @@ protected: const Context & context, QueryProcessingStage::Enum processed_stage); BlockInputStreams createSourceStreams(const SelectQueryInfo & query_info, const QueryProcessingStage::Enum & processed_stage, - const size_t max_block_size, const Names &real_column_names, const Context & modified_context, + const size_t max_block_size, const Context & modified_context, const Block & header, const StoragePtr & storage, const TableStructureReadLockPtr & struct_lock, - size_t current_streams, bool has_table_virtual_column) const; + Names & real_column_names, size_t current_streams, bool has_table_virtual_column); }; } diff --git a/dbms/tests/queries/0_stateless/00458_merge_type_cast.reference b/dbms/tests/queries/0_stateless/00458_merge_type_cast.reference index dc8784b555e..3bbeacdc836 100644 --- a/dbms/tests/queries/0_stateless/00458_merge_type_cast.reference +++ b/dbms/tests/queries/0_stateless/00458_merge_type_cast.reference @@ -14,7 +14,8 @@ 1: 1 1 - -1: + -1: +18446744073709551615 Int32 | UInt64 1 1 diff --git a/dbms/tests/queries/0_stateless/00578_merge_table_and_table_virtual_column.reference b/dbms/tests/queries/0_stateless/00578_merge_table_and_table_virtual_column.reference index 943d6e5f7f4..1a14bde9162 100644 --- a/dbms/tests/queries/0_stateless/00578_merge_table_and_table_virtual_column.reference +++ b/dbms/tests/queries/0_stateless/00578_merge_table_and_table_virtual_column.reference @@ -3,6 +3,3 @@ 1000 1000 1000 -0 -0 -0