diff --git a/src/Core/FormatFactorySettings.h b/src/Core/FormatFactorySettings.h index 4951eda47549b..35746894025e2 100644 --- a/src/Core/FormatFactorySettings.h +++ b/src/Core/FormatFactorySettings.h @@ -210,7 +210,7 @@ When reading Parquet files, skip whole row groups based on the WHERE expressions When reading Parquet files (with reader v3), skip whole row groups based on the WHERE/PREWHERE expressions and the dictionary page contents, when all data pages of a column chunk are dictionary-encoded. The value is the maximum dictionary page size (in bytes) for which this optimization is applied; set to 0 to disable. This takes precedence over the bloom filter when both are available. )", 0) \ DECLARE(UInt64, input_format_parquet_footer_read_size, 0, R"( -Size (in bytes) of the initial tail read that fetches the Parquet footer (`FileMetaData`) when opening a file with reader v3. `0` (default) sizes the read adaptively to the file - 1% of the file size, clamped to `[128 KiB, 2 MiB]` - so it usually covers the whole footer in a single read even for wide files with hundreds of columns. Set a non-zero value to force a fixed initial read size instead of the adaptive one; this is useful for very high-latency object storage where reading a larger tail up front avoids a second round trip. The value is clamped to the file size. +Size (in bytes) of the initial tail read that fetches the Parquet footer (`FileMetaData`) when opening a file with reader v3. `0` (default) reads a 64 KiB tail when the file is read directly from the local filesystem, and for other sources (object storage, URL, HDFS) sizes the read adaptively - 1% of the file size, clamped to `[128 KiB, 2 MiB]` - so it usually covers the whole footer in a single read even for wide files with hundreds of columns. Set a non-zero value to force a fixed initial read size instead of the adaptive one; this is useful for very high-latency object storage where reading a larger tail up front avoids a second round trip. The value is clamped to the file size. )", 0) \ DECLARE(Bool, input_format_parquet_enable_json_parsing, true, R"( When reading Parquet files, parse JSON columns as ClickHouse JSON Column. diff --git a/src/Core/SettingsChangesHistory.cpp b/src/Core/SettingsChangesHistory.cpp index 3ad1ac61f06fc..212b88e6aa179 100644 --- a/src/Core/SettingsChangesHistory.cpp +++ b/src/Core/SettingsChangesHistory.cpp @@ -63,7 +63,7 @@ const VersionToSettingsChangesMap & getSettingsChangesHistory() }); addSettingsChanges(settings_changes_history, "26.8.27.20001.altinityantalya", { - {"input_format_parquet_footer_read_size", 65536, 0, "New setting to override the adaptive Parquet footer initial read size with a fixed number of bytes; 0 keeps the adaptive behavior. Before this setting the reader always read a fixed 64 KiB tail. `compatibility` with 26.8 restores that fixed read."}, + {"input_format_parquet_footer_read_size", 65536, 0, "New setting for the Parquet footer initial read size; 0 reads 64 KiB of a local file and sizes the read adaptively for other sources. Before this setting the reader always read a fixed 64 KiB tail. `compatibility` with 26.8 restores that fixed read."}, }); addSettingsChanges(settings_changes_history, "26.8", { diff --git a/src/Processors/Formats/Impl/Parquet/Prefetcher.cpp b/src/Processors/Formats/Impl/Parquet/Prefetcher.cpp index c673a467b7279..4b17ca058090c 100644 --- a/src/Processors/Formats/Impl/Parquet/Prefetcher.cpp +++ b/src/Processors/Formats/Impl/Parquet/Prefetcher.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -72,7 +73,14 @@ void Prefetcher::determineReadModeAndFileSize(ReadBuffer * reader_, const ReadOp } if (reader) + { file_size = getFileSizeFromReadBuffer(*seekable); + if (auto * file_buf = dynamic_cast(seekable)) + { + size_t view_offset = 0; + is_local_file = file_buf->isRegularLocalFile(&view_offset); + } + } } } diff --git a/src/Processors/Formats/Impl/Parquet/Prefetcher.h b/src/Processors/Formats/Impl/Parquet/Prefetcher.h index 135492aff741b..3c31a6d420274 100644 --- a/src/Processors/Formats/Impl/Parquet/Prefetcher.h +++ b/src/Processors/Formats/Impl/Parquet/Prefetcher.h @@ -66,6 +66,7 @@ class Prefetcher void readSync(char * to, size_t n, size_t offset); size_t getFileSize() const { return file_size; } + bool isLocalFile() const { return is_local_file; } private: friend class PrefetchHandle; @@ -180,6 +181,7 @@ class Prefetcher PaddedPODArray entire_file; size_t file_size{}; + bool is_local_file = false; size_t min_bytes_for_seek{}; size_t bytes_per_read_task{}; diff --git a/src/Processors/Formats/Impl/Parquet/Reader.cpp b/src/Processors/Formats/Impl/Parquet/Reader.cpp index 4fe93f9570533..d5a00b49a8c19 100644 --- a/src/Processors/Formats/Impl/Parquet/Reader.cpp +++ b/src/Processors/Formats/Impl/Parquet/Reader.cpp @@ -231,11 +231,10 @@ parq::FileMetaData Reader::readFileMetaData(Prefetcher & prefetcher, size_t foot if (file_size <= 8) throw Exception(ErrorCodes::INCORRECT_DATA, "Parquet file too short: {} bytes", file_size); - /// Read a tail sized to the file (1%, clamped to [128 KiB, 2 MiB]) so it usually covers the whole - /// footer - FileMetaData for wider range of layouts - in one read. A non-zero - /// `footer_read_size` overrides this adaptive size with a fixed read size. + /// A second read of a local file is cheap. Elsewhere a tail sized to the file (1%, clamped to [128 KiB, 2 MiB]) + /// usually covers the whole footer in one round trip. A non-zero `footer_read_size` is a fixed read size. if (footer_read_size == 0) - footer_read_size = std::clamp(file_size / 100, 128ul << 10, 2ul << 20); + footer_read_size = prefetcher.isLocalFile() ? 64ul << 10 : std::clamp(file_size / 100, 128ul << 10, 2ul << 20); /// The read must cover at least the 8-byte trailer (metadata size + magic) so the offsets below /// don't underflow; an explicit `footer_read_size` smaller than that is bumped up to 8. size_t initial_read_size = std::min(file_size, std::max(footer_read_size, 8)); diff --git a/src/Processors/Formats/Impl/Parquet/Reader.h b/src/Processors/Formats/Impl/Parquet/Reader.h index 63ffd2ffb07bf..2f6add7e00cc7 100644 --- a/src/Processors/Formats/Impl/Parquet/Reader.h +++ b/src/Processors/Formats/Impl/Parquet/Reader.h @@ -566,7 +566,7 @@ struct Reader void init(const ReadOptions & options_, const Block & sample_block_, FormatFilterInfoPtr format_filter_info_); - /// `footer_read_size` overrides the initial footer read size; 0 sizes it adaptively to the file. + /// `footer_read_size` overrides the initial footer read size; 0 reads 64 KiB of a local file and sizes the read to the file otherwise. static parq::FileMetaData readFileMetaData(Prefetcher & prefetcher, size_t footer_read_size); void prefilterAndInitRowGroups(const std::optional> & row_groups_to_read); void preparePrewhere(); diff --git a/tests/queries/0_stateless/05385_parquet_footer_read_size.reference b/tests/queries/0_stateless/05385_parquet_footer_read_size.reference index 3baa52177c35d..1369ebc7de9e6 100644 --- a/tests/queries/0_stateless/05385_parquet_footer_read_size.reference +++ b/tests/queries/0_stateless/05385_parquet_footer_read_size.reference @@ -3,4 +3,8 @@ 10000 49995000 0 9999 10000 49995000 0 9999 10000 49995000 0 9999 +100000 +100000 +05385_tail_default 65536 +05385_tail_explicit 65536 65536 diff --git a/tests/queries/0_stateless/05385_parquet_footer_read_size.sql b/tests/queries/0_stateless/05385_parquet_footer_read_size.sql index c283d80bdf0cb..c00c3d3aa9def 100644 --- a/tests/queries/0_stateless/05385_parquet_footer_read_size.sql +++ b/tests/queries/0_stateless/05385_parquet_footer_read_size.sql @@ -1,15 +1,15 @@ -- Tags: no-fasttest --- Tests `input_format_parquet_footer_read_size`: the v3 reader sizes the initial footer read --- adaptively when the setting is 0 (default), and uses the explicit value otherwise. Every --- value - the adaptive default, a huge fixed read that covers the whole footer in one go, and a --- tiny fixed read that forces the second (`metadata_size + 8 > initial_read_size`) read path - must --- produce identical, correct results. +-- Tests `input_format_parquet_footer_read_size`: when the setting is 0 (default) the v3 reader reads +-- a 64 KiB footer tail of a local file (and sizes it adaptively for other sources), and uses the +-- explicit value otherwise. Every value - the default, a huge fixed read that covers the whole +-- footer in one go, and a tiny fixed read that forces the second (`metadata_size + 8 > +-- initial_read_size`) read path - must produce identical, correct results. insert into function file(currentDatabase() || '_data_05385.parquet') select number as x, toString(number) as s from numbers(10000) settings engine_file_truncate_on_insert = 1; --- Adaptive (default 0): 1% clamped to [128 KiB, 2 MiB]. +-- Default (0): a local file, so a 64 KiB tail. select count(), sum(x), min(s), max(s) from file(currentDatabase() || '_data_05385.parquet', auto, 'x UInt64, s String') settings input_format_parquet_footer_read_size = 0; @@ -29,7 +29,25 @@ select count(), sum(x), min(s), max(s) from file(currentDatabase() || '_data_053 select count(), sum(x), min(s), max(s) from file(currentDatabase() || '_data_05385.parquet', auto, 'x UInt64, s String') settings input_format_parquet_footer_read_size = 1; +-- A local file larger than the adaptive minimum (128 KiB) reads a 64 KiB tail by default, the same as an +-- explicit 65536. +insert into function file(currentDatabase() || '_tail_a_05385.parquet') + select cityHash64(number) as x from numbers(100000) settings engine_file_truncate_on_insert = 1; +insert into function file(currentDatabase() || '_tail_b_05385.parquet') + select cityHash64(number) as x from numbers(100000) settings engine_file_truncate_on_insert = 1; +select count() from file(currentDatabase() || '_tail_a_05385.parquet', Parquet, 'x UInt64') + settings input_format_parquet_footer_read_size = 0, storage_file_read_method = 'pread', + optimize_count_from_files = 1, optimize_trivial_count_query = 1, log_comment = '05385_tail_default'; +select count() from file(currentDatabase() || '_tail_b_05385.parquet', Parquet, 'x UInt64') + settings input_format_parquet_footer_read_size = 65536, storage_file_read_method = 'pread', + optimize_count_from_files = 1, optimize_trivial_count_query = 1, log_comment = '05385_tail_explicit'; +system flush logs query_log; +select log_comment, ProfileEvents['ReadBufferFromFileDescriptorReadBytes'] from system.query_log +where event_date >= yesterday() and current_database = currentDatabase() and type = 'QueryFinish' + and log_comment like '05385_tail_%' +order by log_comment; + -- The setting is new after 26.8. `compatibility` with 26.8 must restore the previous fixed --- 64 KiB footer read, not the new adaptive default (0). +-- 64 KiB footer read, not the new default (0). set compatibility = '26.8'; select getSetting('input_format_parquet_footer_read_size');