Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Core/FormatFactorySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Core/SettingsChangesHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
{
Expand Down
8 changes: 8 additions & 0 deletions src/Processors/Formats/Impl/Parquet/Prefetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Formats/FormatParserSharedResources.h>
#include <IO/copyData.h>
#include <IO/ReadBufferFromFileBase.h>
#include <IO/SeekableReadBuffer.h>
#include <IO/WithFileSize.h>
#include <IO/WriteBufferFromVector.h>
Expand Down Expand Up @@ -72,7 +73,14 @@ void Prefetcher::determineReadModeAndFileSize(ReadBuffer * reader_, const ReadOp
}

if (reader)
{
file_size = getFileSizeFromReadBuffer(*seekable);
if (auto * file_buf = dynamic_cast<ReadBufferFromFileBase *>(seekable))
{
size_t view_offset = 0;
is_local_file = file_buf->isRegularLocalFile(&view_offset);
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Processors/Formats/Impl/Parquet/Prefetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +181,7 @@ class Prefetcher
PaddedPODArray<char> entire_file;

size_t file_size{};
bool is_local_file = false;
size_t min_bytes_for_seek{};
size_t bytes_per_read_task{};

Expand Down
7 changes: 3 additions & 4 deletions src/Processors/Formats/Impl/Parquet/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(file_size / 100, 128ul << 10, 2ul << 20);
footer_read_size = prefetcher.isLocalFile() ? 64ul << 10 : std::clamp<size_t>(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<size_t>(footer_read_size, 8));
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/Formats/Impl/Parquet/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unordered_set<UInt64>> & row_groups_to_read);
void preparePrewhere();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 25 additions & 7 deletions tests/queries/0_stateless/05385_parquet_footer_read_size.sql
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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');
Loading