diff --git a/src/Storages/MergeTree/MergeTask.cpp b/src/Storages/MergeTree/MergeTask.cpp index 8c1dca7ac10..37eb94f269b 100644 --- a/src/Storages/MergeTree/MergeTask.cpp +++ b/src/Storages/MergeTree/MergeTask.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include @@ -125,6 +125,10 @@ public: if (!finalized) throw Exception(ErrorCodes::LOGICAL_ERROR, "Temporary file is not finalized yet"); + /// tmp_disk might not create real file if no data was written to it. + if (final_size == 0) + return std::make_unique(); + /// Reopen the file for each read so that multiple reads can be performed in parallel and there is no need to seek to the beginning. auto raw_file_read_buffer = std::make_unique(tmp_file_name_on_disk); return std::make_unique(std::move(raw_file_read_buffer)); @@ -136,7 +140,8 @@ public: write_buffer->finalize(); uncompressed_write_buffer->finalize(); finalized = true; - return write_buffer->count(); + final_size = write_buffer->count(); + return final_size; } private: @@ -145,6 +150,7 @@ private: std::unique_ptr write_buffer; const String tmp_file_name_on_disk; bool finalized = false; + size_t final_size = 0; }; static void addMissedColumnsToSerializationInfos(