From 068bf8422e9ce894e758562586fc6b0f7093ae60 Mon Sep 17 00:00:00 2001 From: Groene AI <270696204+groeneai@users.noreply.github.com> Date: Fri, 9 Oct 2026 10:14:14 +0000 Subject: [PATCH 1/2] Backport the 64 KiB Parquet footer tail for local files from master. The adaptive footer read (`clamp(file_size / 100, 128 KiB, 2 MiB)`, backported from 115686) saves a round trip on object storage, but for a local file a second read is cheap and the larger tail only costs bytes on every open. `Prefetcher` now records `ReadBufferFromFileBase::isRegularLocalFile`, and `Reader::readFileMetaData` reads 64 KiB when `input_format_parquet_footer_read_size` is 0 and the file is a regular local file. Other sources and an explicit non-zero setting are unchanged. Cherry-picked from 5a58f229ecb0; the setting description and the test follow the 26.8 layout (`05385_parquet_footer_read_size`). https://github.com/ClickHouse/ClickHouse/pull/124883 Related: https://github.com/ClickHouse/ClickHouse/issues/124673 Related: https://github.com/Altinity/ClickHouse/pull/2181 --- src/Core/FormatFactorySettings.h | 2 +- .../Formats/Impl/Parquet/Prefetcher.cpp | 8 +++++ .../Formats/Impl/Parquet/Prefetcher.h | 2 ++ .../Formats/Impl/Parquet/Reader.cpp | 7 ++-- src/Processors/Formats/Impl/Parquet/Reader.h | 2 +- .../05385_parquet_footer_read_size.reference | 4 +++ .../05385_parquet_footer_read_size.sql | 32 +++++++++++++++---- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/Core/FormatFactorySettings.h b/src/Core/FormatFactorySettings.h index 4951eda47549b..8838d248ec669 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 of a local file, 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/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'); From 131e924cb5f498e912c9bb1c88d9688b99bdcec1 Mon Sep 17 00:00:00 2001 From: Anton Ivashkin Date: Mon, 23 Nov 2026 17:16:53 +0100 Subject: [PATCH 2/2] Backport the precise description of the local Parquet footer tail from master. The setting description of `input_format_parquet_footer_read_size` now says the 64 KiB tail applies when the file is read directly from the local filesystem (the reader's buffer reports `isRegularLocalFile`). Reads through a file descriptor or a wrapping buffer keep the adaptive size. The `SettingsChangesHistory` reason no longer says that `0` keeps the adaptive behavior. Cherry-picked from 6fbb4e7e880b; on 26.8 the history reason is in `SettingsChangesHistory.cpp`, so it is changed there. https://github.com/ClickHouse/ClickHouse/pull/124883 Related: https://github.com/Altinity/ClickHouse/pull/2181 --- src/Core/FormatFactorySettings.h | 2 +- src/Core/SettingsChangesHistory.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core/FormatFactorySettings.h b/src/Core/FormatFactorySettings.h index 8838d248ec669..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) reads a 64 KiB tail of a local file, 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. +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 432241795742a..9e5c457c3f404 100644 --- a/src/Core/SettingsChangesHistory.cpp +++ b/src/Core/SettingsChangesHistory.cpp @@ -58,7 +58,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", {