Skip to content
Open
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
22 changes: 22 additions & 0 deletions include/paimon/reader/file_batch_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
#pragma once

#include <memory>
#include <optional>
#include <utility>
#include <vector>

#include "paimon/predicate/predicate.h"
#include "paimon/reader/batch_reader.h"
#include "paimon/utils/range.h"
#include "paimon/utils/roaring_bitmap32.h"
namespace paimon {
/// The batch reader for a single file supports returning the line number of the last batch read for
Expand Down Expand Up @@ -85,4 +87,24 @@ class PAIMON_EXPORT FileBatchReader : public BatchReader {
virtual void Warmup() {}
};

/// Optional statistics filtering interface for an open file reader.
class PAIMON_EXPORT RowGroupFilter {
public:
virtual ~RowGroupFilter();

/// Uses metadata to find sorted, disjoint, inclusive file-local candidate ranges without
/// configuring the reader or reading column data. The predicate uses physical column names.
/// The expected row count comes from the file manifest. An empty vector means no rows can
/// match, and nullopt means no narrower selection is available. The caller must still
/// evaluate the predicate on candidate rows.
virtual Result<std::optional<std::vector<Range>>> GetRowGroupCandidateRanges(
int64_t expected_row_count, const std::shared_ptr<Predicate>& predicate) const = 0;

/// Records metadata I/O for an open reader discarded before scanning rows. Prefetch wrappers
/// count row groups once and may supply the bytes read through a shared cache.
virtual std::shared_ptr<Metrics> GetSkippedReaderMetrics(
bool count_row_groups = true,
std::optional<uint64_t> storage_read_bytes = std::nullopt) const = 0;
};

} // namespace paimon
4 changes: 4 additions & 0 deletions src/paimon/common/reader/batch_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

#include "arrow/c/abi.h"
#include "paimon/common/reader/reader_utils.h"
#include "paimon/reader/file_batch_reader.h"

namespace paimon {

RowGroupFilter::~RowGroupFilter() = default;

BatchReader::ReadBatch BatchReader::MakeEofBatch() {
return std::make_pair(std::unique_ptr<ArrowArray>(), std::unique_ptr<ArrowSchema>());
}
Expand Down
7 changes: 5 additions & 2 deletions src/paimon/common/reader/concat_batch_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ static auto CatchPreparationErrors(Func&& func) -> decltype(func()) {
}

ConcatBatchReader::ConcatBatchReader(std::vector<std::unique_ptr<BatchReader>>&& readers,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool)
: arrow_pool_(arrow_pool), finished_reader_metrics_(std::make_shared<MetricsImpl>()) {
const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
std::shared_ptr<Metrics> finished_reader_metrics)
: arrow_pool_(arrow_pool),
finished_reader_metrics_(finished_reader_metrics ? std::move(finished_reader_metrics)
: std::make_shared<MetricsImpl>()) {
for (auto& reader : readers) {
readers_.emplace_back();
readers_.back().file_reader = dynamic_cast<FileBatchReader*>(reader.get());
Expand Down
4 changes: 3 additions & 1 deletion src/paimon/common/reader/concat_batch_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ class ConcatBatchReader : public BatchReader {
const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
const std::shared_ptr<Executor>& build_executor);

/// Can start with metrics from readers already closed during preparation.
ConcatBatchReader(std::vector<std::unique_ptr<BatchReader>>&& readers,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool);
const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
std::shared_ptr<Metrics> finished_reader_metrics = nullptr);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using default parameters in production code under src. If the change is relatively large, consider adding an overloaded constructor instead.

~ConcatBatchReader() override;

Result<ReadBatch> NextBatch() override;
Expand Down
16 changes: 15 additions & 1 deletion src/paimon/common/reader/delegating_prefetch_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace paimon {

class DelegatingPrefetchReader : public FileBatchReader {
class DelegatingPrefetchReader : public FileBatchReader, public RowGroupFilter {
public:
explicit DelegatingPrefetchReader(std::unique_ptr<PrefetchFileBatchReaderImpl> prefetch_reader)
: prefetch_reader_(std::move(prefetch_reader)) {}
Expand All @@ -52,6 +52,20 @@ class DelegatingPrefetchReader : public FileBatchReader {
return GetReader()->GetFileSchema();
}

Result<std::optional<std::vector<Range>>> GetRowGroupCandidateRanges(
int64_t expected_row_count, const std::shared_ptr<Predicate>& predicate) const override {
if (const auto* filter =
dynamic_cast<const RowGroupFilter*>(prefetch_reader_->GetFirstReader())) {
return filter->GetRowGroupCandidateRanges(expected_row_count, predicate);
}
return std::optional<std::vector<Range>>();
}

std::shared_ptr<Metrics> GetSkippedReaderMetrics(
bool count_row_groups, std::optional<uint64_t> storage_read_bytes) const override {
return prefetch_reader_->GetSkippedReaderMetrics(count_row_groups, storage_read_bytes);
}

Status SetReadSchema(::ArrowSchema* read_schema, const std::shared_ptr<Predicate>& predicate,
const std::optional<RoaringBitmap32>& selection_bitmap) override {
return prefetch_reader_->SetReadSchema(read_schema, predicate, selection_bitmap);
Expand Down
38 changes: 36 additions & 2 deletions src/paimon/common/reader/prefetch_file_batch_reader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "paimon/format/reader_builder.h"
#include "paimon/fs/file_system.h"
#include "paimon/memory/memory_pool.h"
#include "paimon/reader/file_batch_reader.h"

namespace arrow {
class Schema;
Expand Down Expand Up @@ -961,6 +962,40 @@ std::shared_ptr<Metrics> PrefetchFileBatchReaderImpl::GetReaderMetrics() const {
result->Merge(readers_[0]->GetReaderMetrics());
}

CollectPrefetchMetrics(result);
return result;
}

std::shared_ptr<Metrics> PrefetchFileBatchReaderImpl::GetSkippedReaderMetrics(
bool count_row_groups, std::optional<uint64_t> storage_read_bytes) const {
const auto* first_filter = dynamic_cast<const RowGroupFilter*>(GetFirstReader());
if (first_filter == nullptr) {
return GetReaderMetrics();
}

auto result = std::make_shared<MetricsImpl>();
CollectPrefetchMetrics(result);
if (!storage_read_bytes && cache_) {
// Only footer reads have run. The cache counters separate storage fetches from
// requests served by the cache and from misses that fell back to a direct read.
storage_read_bytes = result->GetCounter(ReadAheadCacheMetrics::IO_BYTES).value() +
result->GetCounter(ReadAheadCacheMetrics::READ_MISS_BYTES).value();
}
if (storage_read_bytes) {
result->Merge(first_filter->GetSkippedReaderMetrics(count_row_groups, storage_read_bytes));
} else {
result->Merge(first_filter->GetSkippedReaderMetrics(count_row_groups));
for (size_t i = 1; i < readers_.size(); ++i) {
if (const auto* filter = dynamic_cast<const RowGroupFilter*>(readers_[i].get())) {
result->Merge(filter->GetSkippedReaderMetrics(/*count_row_groups=*/false));
}
}
}
return result;
}

void PrefetchFileBatchReaderImpl::CollectPrefetchMetrics(
const std::shared_ptr<Metrics>& result) const {
auto set_prefetch_counter = [&result](const char* name, const std::atomic<uint64_t>& value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since result is a standard out parameter that doesn’t require ownership, I suggest using a raw pointer to make it explicit.

result->SetCounter(name, value.load(kMetricsMemoryOrder));
};
Expand Down Expand Up @@ -996,7 +1031,7 @@ std::shared_ptr<Metrics> PrefetchFileBatchReaderImpl::GetReaderMetrics() const {
}

if (!io_metrics_) {
return result;
return;
}
auto set_io_counter = [&result](const char* name, const std::atomic<uint64_t>& value) {
result->SetCounter(name, value.load(kMetricsMemoryOrder));
Expand All @@ -1016,7 +1051,6 @@ std::shared_ptr<Metrics> PrefetchFileBatchReaderImpl::GetReaderMetrics() const {
set_io_counter(PrefetchIoMetrics::ASYNC_LATENCY_SUM_US, io_metrics_->async_latency_sum_us);
result->SetGauge(PrefetchIoMetrics::ASYNC_PENDING,
static_cast<double>(io_metrics_->async_pending.load(kMetricsMemoryOrder)));
return result;
}

Result<std::unique_ptr<::ArrowSchema>> PrefetchFileBatchReaderImpl::GetFileSchema() const {
Expand Down
4 changes: 4 additions & 0 deletions src/paimon/common/reader/prefetch_file_batch_reader_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class PrefetchFileBatchReaderImpl : public PrefetchFileBatchReader {
Result<FileBatchReader::ReadBatchWithBitmap> NextBatchWithBitmap() override;

std::shared_ptr<Metrics> GetReaderMetrics() const override;
std::shared_ptr<Metrics> GetSkippedReaderMetrics(
bool count_row_groups, std::optional<uint64_t> storage_read_bytes) const;

Result<std::unique_ptr<::ArrowSchema>> GetFileSchema() const override;
Status SetReadSchema(::ArrowSchema* read_schema, const std::shared_ptr<Predicate>& predicate,
Expand Down Expand Up @@ -127,6 +129,8 @@ class PrefetchFileBatchReaderImpl : public PrefetchFileBatchReader {
const std::shared_ptr<PrefetchIoMetricsState>& io_metrics, WarmupLevel warmup_level,
const std::shared_ptr<arrow::MemoryPool>& arrow_pool);

void CollectPrefetchMetrics(const std::shared_ptr<Metrics>& result) const;

Status CleanUp();
void Workloop();
/// Starts the background prefetch thread if it is not running yet. The first read does this
Expand Down
35 changes: 31 additions & 4 deletions src/paimon/core/operation/abstract_split_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,38 @@ Result<std::unique_ptr<FileBatchReader>> AbstractSplitRead::CreateFileBatchReade
options_.GetFileSystem(), executor_, pool_, arrow_pool_);
}

Result<std::unique_ptr<FileBatchReader>> AbstractSplitRead::OpenDataFileReader(
const std::shared_ptr<DataFileMeta>& file,
const DataFilePathFactory& data_file_path_factory) const {
PAIMON_ASSIGN_OR_RAISE(std::string format_identifier, file->FileFormat());
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<ReaderBuilder> reader_builder,
PrepareReaderBuilder(format_identifier, /*extra_format_options=*/{}));
return CreateFileBatchReader(format_identifier, data_file_path_factory.ToPath(file),
file->file_size, std::move(reader_builder));
}

Result<std::unique_ptr<FileBatchReader>> AbstractSplitRead::ConfigureOpenedFileReader(
const BinaryRow& partition, const std::shared_ptr<DataFileMeta>& file,
const std::shared_ptr<arrow::Schema>& read_schema, DeletionVector::Factory dv_factory,
const std::optional<std::vector<Range>>& row_ranges,
const std::shared_ptr<DataFilePathFactory>& data_file_path_factory,
std::unique_ptr<FileBatchReader> file_reader) const {
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<FieldMappingBuilder> field_mapping_builder,
FieldMappingBuilder::Create(read_schema, context_->GetPartitionKeys(),
/*predicate=*/nullptr));
return CreateFieldMappingReader(data_file_path_factory->ToPath(file), file, partition,
/*reader_builder=*/nullptr, field_mapping_builder.get(),
dv_factory, row_ranges, data_file_path_factory,
/*exact_selection=*/nullptr, std::move(file_reader));
}

Result<std::unique_ptr<FileBatchReader>> AbstractSplitRead::CreateFieldMappingReader(
const std::string& data_file_path, const std::shared_ptr<DataFileMeta>& file_meta,
const BinaryRow& partition, std::unique_ptr<ReaderBuilder> reader_builder,
const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory,
const std::optional<std::vector<Range>>& row_ranges,
const std::shared_ptr<DataFilePathFactory>& data_file_path_factory,
const RoaringBitmap32* exact_selection) const {
const RoaringBitmap32* exact_selection, std::unique_ptr<FileBatchReader> file_reader) const {
std::shared_ptr<TableSchema> data_schema;
if (file_meta->schema_id == context_->GetTableSchema()->Id()) {
data_schema = context_->GetTableSchema();
Expand Down Expand Up @@ -456,9 +481,11 @@ Result<std::unique_ptr<FileBatchReader>> AbstractSplitRead::CreateFieldMappingRe
field_mapping->non_partition_info.non_partition_data_schema);

PAIMON_ASSIGN_OR_RAISE(std::string file_format_identifier, file_meta->FileFormat());
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<FileBatchReader> file_reader,
CreateFileBatchReader(file_format_identifier, data_file_path,
file_meta->file_size, std::move(reader_builder)));
if (!file_reader) {
PAIMON_ASSIGN_OR_RAISE(
file_reader, CreateFileBatchReader(file_format_identifier, data_file_path,
file_meta->file_size, std::move(reader_builder)));
}
if (VectorFileBatchReader::ContainsVector(read_schema)) {
file_reader = std::make_unique<VectorFileBatchReader>(std::move(file_reader), arrow_pool_);
}
Expand Down
14 changes: 13 additions & 1 deletion src/paimon/core/operation/abstract_split_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ class AbstractSplitRead : public SplitRead {
const std::map<std::string, std::string>& extra_format_options,
bool attach_blob_context) const;

Result<std::unique_ptr<FileBatchReader>> OpenDataFileReader(
const std::shared_ptr<DataFileMeta>& file,
const DataFilePathFactory& data_file_path_factory) const;

Result<std::unique_ptr<FileBatchReader>> ConfigureOpenedFileReader(
const BinaryRow& partition, const std::shared_ptr<DataFileMeta>& file,
const std::shared_ptr<arrow::Schema>& read_schema, DeletionVector::Factory dv_factory,
const std::optional<std::vector<Range>>& row_ranges,
const std::shared_ptr<DataFilePathFactory>& data_file_path_factory,
std::unique_ptr<FileBatchReader> file_reader) const;

Result<std::unique_ptr<BatchReader>> ApplyPredicateFilterIfNeeded(
std::unique_ptr<BatchReader>&& reader, const std::shared_ptr<Predicate>& predicate) const;

Expand Down Expand Up @@ -193,7 +204,8 @@ class AbstractSplitRead : public SplitRead {
const FieldMappingBuilder* field_mapping_builder, DeletionVector::Factory dv_factory,
const std::optional<std::vector<Range>>& row_ranges,
const std::shared_ptr<DataFilePathFactory>& data_file_path_factory,
const RoaringBitmap32* exact_selection) const;
const RoaringBitmap32* exact_selection,
std::unique_ptr<FileBatchReader> opened_reader = nullptr) const;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid default parameters, or alternatively add a new overload and remove unrelated parameters like reader_builder, etc.


/// The returned field ID set contains MAP fields handled by shredding read plans. It tells
/// FieldMappingReader to skip its generic selected-key filtering because the plans have already
Expand Down
Loading
Loading