Skip to content
Merged
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
6 changes: 2 additions & 4 deletions cpp/src/arrow/acero/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
#include "arrow/compute/api_vector.h"
#include "arrow/compute/exec.h"
#include "arrow/compute/expression.h"
#include "arrow/record_batch.h"
#include "arrow/result.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/async_util.h"
#include "arrow/util/future.h"

namespace arrow {

Expand All @@ -55,7 +53,7 @@ namespace acero {
/// \brief This must not be used in release-mode
struct DebugOptions;

using AsyncExecBatchGenerator = AsyncGenerator<std::optional<ExecBatch>>;
using AsyncExecBatchGenerator = std::function<Future<std::optional<ExecBatch>>()>;

/// \addtogroup acero-nodes
/// @{
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/acero/source_node_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "arrow/acero/map_node.h"
#include "arrow/acero/options.h"
#include "arrow/acero/test_nodes.h"
#include "arrow/record_batch.h"

namespace arrow {
namespace acero {
Expand Down
14 changes: 11 additions & 3 deletions cpp/src/arrow/dataset/dataset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "arrow/acero/util.h"
#include "arrow/dataset/dataset.h"
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/projector.h"
#include "arrow/dataset/scanner.h"
#include "arrow/table.h"
#include "arrow/util/async_generator.h"
Expand All @@ -41,7 +42,7 @@ const compute::Expression Fragment::kNoPartitionInformation = compute::literal(t
Fragment::Fragment(compute::Expression partition_expression,
std::shared_ptr<Schema> physical_schema)
: partition_expression_(std::move(partition_expression)),
physical_schema_(std::move(physical_schema)) {}
given_physical_schema_(std::move(physical_schema)) {}

Future<std::shared_ptr<InspectedFragment>> Fragment::InspectFragment(
const FragmentScanOptions* format_options, compute::ExecContext* exec_context) {
Expand Down Expand Up @@ -75,16 +76,23 @@ Future<std::optional<int64_t>> Fragment::CountRows(compute::Expression,
return Future<std::optional<int64_t>>::MakeFinished(std::nullopt);
}

Status Fragment::ClearCachedMetadata() {
auto lock = physical_schema_mutex_.Lock();
physical_schema_.reset();
Comment thread
pitrou marked this conversation as resolved.
Resolved
return Status::OK();
}

Result<std::shared_ptr<Schema>> InMemoryFragment::ReadPhysicalSchemaImpl() {
return physical_schema_;
return given_physical_schema_;
}

InMemoryFragment::InMemoryFragment(std::shared_ptr<Schema> schema,
RecordBatchVector record_batches,
compute::Expression partition_expression)
: Fragment(std::move(partition_expression), std::move(schema)),
record_batches_(std::move(record_batches)) {
DCHECK_NE(physical_schema_, nullptr);
DCHECK_NE(given_physical_schema_, nullptr);
physical_schema_ = given_physical_schema_;
}

InMemoryFragment::InMemoryFragment(RecordBatchVector record_batches,
Expand Down
10 changes: 10 additions & 0 deletions cpp/src/arrow/dataset/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ class ARROW_DS_EXPORT Fragment : public std::enable_shared_from_this<Fragment> {
virtual Future<std::optional<int64_t>> CountRows(
compute::Expression predicate, const std::shared_ptr<ScanOptions>& options);

/// \brief Clear any metadata that may have been cached by this object.
///
/// A fragment may typically cache metadata to speed up repeated accesses.
/// In use cases when memory use is more critical than CPU time, calling
/// this function can help reclaim memory.
virtual Status ClearCachedMetadata();

virtual std::string type_name() const = 0;
virtual std::string ToString() const { return type_name(); }

Expand All @@ -210,7 +217,10 @@ class ARROW_DS_EXPORT Fragment : public std::enable_shared_from_this<Fragment> {

util::Mutex physical_schema_mutex_;
compute::Expression partition_expression_ = compute::literal(true);
// The physical schema that is inferred from the Fragment
std::shared_ptr<Schema> physical_schema_;
// The physical schema that was passed to the Fragment constructor
std::shared_ptr<Schema> given_physical_schema_;
};

/// \brief Per-scan options for fragment(s) in a dataset.
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/dataset/dataset_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/discovery.h"
#include "arrow/dataset/partition.h"
#include "arrow/dataset/projector.h"
#include "arrow/dataset/test_util_internal.h"
#include "arrow/filesystem/mockfs.h"
#include "arrow/stl.h"
Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/dataset/discovery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "arrow/dataset/partition.h"
#include "arrow/dataset/type_fwd.h"
#include "arrow/filesystem/path_util.h"
#include "arrow/record_batch.h"
#include "arrow/util/logging.h"
#include "arrow/util/string.h"

Expand Down
1 change: 1 addition & 0 deletions cpp/src/arrow/dataset/file_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/dataset_writer.h"
#include "arrow/dataset/forest_internal.h"
#include "arrow/dataset/projector.h"
#include "arrow/dataset/scanner.h"
#include "arrow/dataset/subtree_internal.h"
#include "arrow/filesystem/filesystem.h"
Expand Down
21 changes: 15 additions & 6 deletions cpp/src/arrow/dataset/file_parquet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,12 @@ Status ParquetFileFragment::EnsureCompleteMetadata(parquet::arrow::FileReader* r
return EnsureCompleteMetadata(reader.get());
}

std::shared_ptr<Schema> schema;
RETURN_NOT_OK(reader->GetSchema(&schema));
if (physical_schema_ && !physical_schema_->Equals(*schema)) {
RETURN_NOT_OK(reader->GetSchema(&physical_schema_));
if (given_physical_schema_ && !given_physical_schema_->Equals(*physical_schema_)) {
return Status::Invalid("Fragment initialized with physical schema ",
*physical_schema_, " but ", source_.path(), " has schema ",
*schema);
*given_physical_schema_, " but ", source_.path(),
" has schema ", *physical_schema_);
}
physical_schema_ = std::move(schema);

if (!row_groups_) {
row_groups_ = Iota(reader->num_row_groups());
Expand All @@ -835,6 +833,10 @@ Status ParquetFileFragment::SetMetadata(
DCHECK_EQ(manifest_->descr, original_metadata_->schema())
<< "SchemaDescriptor should be owned by the original FileMetaData";

if (!physical_schema_) {
Comment thread
zanmato1984 marked this conversation as resolved.
Resolved
physical_schema_ = given_physical_schema_;
}

statistics_expressions_.resize(row_groups_->size(), compute::literal(true));
statistics_expressions_complete_.resize(manifest_->descr->num_columns(), false);

Expand All @@ -850,6 +852,13 @@ Status ParquetFileFragment::SetMetadata(
return Status::OK();
}

Status ParquetFileFragment::ClearCachedMetadata() {
metadata_.reset();
manifest_.reset();
original_metadata_.reset();
return FileFragment::ClearCachedMetadata();
}

Result<FragmentVector> ParquetFileFragment::SplitByRowGroup(
compute::Expression predicate) {
RETURN_NOT_OK(EnsureCompleteMetadata());
Expand Down
5 changes: 5 additions & 0 deletions cpp/src/arrow/dataset/file_parquet.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,16 @@ class ARROW_DS_EXPORT ParquetFileFragment : public FileFragment {
}

/// \brief Return the FileMetaData associated with this fragment.
///
/// This may return nullptr if the fragment wasn't scanned yet, or if
/// `ScanOptions::cache_metadata` was disabled.
std::shared_ptr<parquet::FileMetaData> metadata();

/// \brief Ensure this fragment's FileMetaData is in memory.
Status EnsureCompleteMetadata(parquet::arrow::FileReader* reader = NULLPTR);

Status ClearCachedMetadata() override;

/// \brief Return fragment which selects a filtered subset of this fragment's RowGroups.
Result<std::shared_ptr<Fragment>> Subset(compute::Expression predicate);
Result<std::shared_ptr<Fragment>> Subset(std::vector<int> row_group_ids);
Expand Down
19 changes: 16 additions & 3 deletions cpp/src/arrow/dataset/file_parquet_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

namespace arrow {

using internal::checked_cast;
using internal::checked_pointer_cast;

namespace dataset {
Expand Down Expand Up @@ -325,6 +326,7 @@ TEST_F(TestParquetFileFormat, CachedMetadata) {
FileSource source(tracked_input);
ASSERT_OK_AND_ASSIGN(auto fragment,
format_->MakeFragment(std::move(source), literal(true)));
auto pq_fragment = checked_cast<ParquetFileFragment*>(fragment.get());

// Read the file the first time, will read metadata
auto options = std::make_shared<ScanOptions>();
Expand All @@ -336,17 +338,28 @@ TEST_F(TestParquetFileFormat, CachedMetadata) {
options->projection = projection_descr.expression;
ASSERT_OK_AND_ASSIGN(auto generator, fragment->ScanBatchesAsync(options));
ASSERT_FINISHES_OK(CollectAsyncGenerator(std::move(generator)));
ASSERT_NE(nullptr, pq_fragment->metadata());

ASSERT_GT(tracked_input->bytes_read(), 0);
int64_t bytes_read_first_time = tracked_input->bytes_read();

ASSERT_OK(tracked_input->Seek(0));

// Read the file the second time, should not read metadata
tracked_input->ResetStats();
ASSERT_OK_AND_ASSIGN(generator, fragment->ScanBatchesAsync(options));
ASSERT_FINISHES_OK(CollectAsyncGenerator(std::move(generator)));
ASSERT_LT(tracked_input->bytes_read(), bytes_read_first_time);

// Clear cached metadata
ASSERT_OK(fragment->ClearCachedMetadata());
ASSERT_EQ(nullptr, pq_fragment->metadata());

// Read the file a third time, should read metadata
tracked_input->ResetStats();
ASSERT_OK_AND_ASSIGN(generator, fragment->ScanBatchesAsync(options));
ASSERT_FINISHES_OK(CollectAsyncGenerator(std::move(generator)));
int64_t bytes_read_second_time = tracked_input->bytes_read() - bytes_read_first_time;
ASSERT_LT(bytes_read_second_time, bytes_read_first_time);
ASSERT_EQ(tracked_input->bytes_read(), bytes_read_first_time);
ASSERT_NE(nullptr, pq_fragment->metadata());
}

TEST_F(TestParquetFileFormat, MultithreadedScan) {
Expand Down
22 changes: 16 additions & 6 deletions cpp/src/arrow/dataset/scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "arrow/dataset/dataset.h"
#include "arrow/dataset/dataset_internal.h"
#include "arrow/dataset/plan.h"
#include "arrow/record_batch.h"
#include "arrow/table.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/config.h"
Expand Down Expand Up @@ -318,10 +319,14 @@ Result<EnumeratedRecordBatchGenerator> FragmentToBatches(
RecordBatch::Make(options->dataset_schema, /*num_rows=*/0, std::move(columns)));
auto enumerated_batch_gen = MakeEnumeratedGenerator(std::move(batch_gen));

auto combine_fn =
[fragment](const Enumerated<std::shared_ptr<RecordBatch>>& record_batch) {
return EnumeratedRecordBatch{record_batch, fragment};
};
auto combine_fn = [fragment, cache_metadata = options->cache_metadata](
const Enumerated<std::shared_ptr<RecordBatch>>& record_batch) {
if (!cache_metadata && record_batch.last) {
ARROW_WARN_NOT_OK(fragment.value->ClearCachedMetadata(),
"Could not clear cached metadata on fragment");
}
Comment thread
zanmato1984 marked this conversation as resolved.
Resolved
return EnumeratedRecordBatch{record_batch, fragment};
};

return MakeMappedGenerator(enumerated_batch_gen, std::move(combine_fn));
}
Expand All @@ -343,7 +348,7 @@ class OneShotFragment : public Fragment {
OneShotFragment(std::shared_ptr<Schema> schema, RecordBatchIterator batch_it)
: Fragment(compute::literal(true), std::move(schema)),
batch_it_(std::move(batch_it)) {
DCHECK_NE(physical_schema_, nullptr);
DCHECK_NE(given_physical_schema_, nullptr);
}
Status CheckConsumed() {
if (!batch_it_) return Status::Invalid("OneShotFragment was already scanned");
Expand All @@ -362,7 +367,7 @@ class OneShotFragment : public Fragment {

protected:
Result<std::shared_ptr<Schema>> ReadPhysicalSchemaImpl() override {
return physical_schema_;
return given_physical_schema_;
}

RecordBatchIterator batch_it_;
Expand Down Expand Up @@ -936,6 +941,11 @@ Status ScannerBuilder::UseThreads(bool use_threads) {
return Status::OK();
}

Status ScannerBuilder::CacheMetadata(bool cache_metadata) {
scan_options_->cache_metadata = cache_metadata;
return Status::OK();
}

Status ScannerBuilder::BatchSize(int64_t batch_size) {
if (batch_size <= 0) {
return Status::Invalid("BatchSize must be greater than 0, got ", batch_size);
Expand Down
33 changes: 29 additions & 4 deletions cpp/src/arrow/dataset/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@
#include "arrow/compute/expression.h"
#include "arrow/compute/type_fwd.h"
#include "arrow/dataset/dataset.h"
#include "arrow/dataset/projector.h"
#include "arrow/dataset/type_fwd.h"
#include "arrow/dataset/visibility.h"
#include "arrow/io/interfaces.h"
#include "arrow/memory_pool.h"
#include "arrow/type_fwd.h"
#include "arrow/util/async_generator.h"
#include "arrow/util/async_generator_fwd.h"
#include "arrow/util/iterator.h"
#include "arrow/util/thread_pool.h"
#include "arrow/util/type_fwd.h"

namespace arrow {
Expand Down Expand Up @@ -117,6 +114,14 @@ struct ARROW_DS_EXPORT ScanOptions {
/// If true the scanner will add augmented fields to the output schema.
bool add_augmented_fields = true;

/// Whether to cache metadata when scanning.
///
/// Fragments may typically cache metadata to speed up repeated accesses.
/// However, in use cases where a single scan is done, or if memory use
/// is more critical than CPU time, setting this option to false can
/// lessen memory use.
bool cache_metadata = true;

/// Fragment-specific scan options.
std::shared_ptr<FragmentScanOptions> fragment_scan_options;

Expand Down Expand Up @@ -309,7 +314,13 @@ ARROW_DS_EXPORT void SetProjection(ScanOptions* options, ProjectionDescr project
struct TaggedRecordBatch {
std::shared_ptr<RecordBatch> record_batch;
std::shared_ptr<Fragment> fragment;

friend inline bool operator==(const TaggedRecordBatch& left,
const TaggedRecordBatch& right) {
return left.record_batch == right.record_batch && left.fragment == right.fragment;
}
};

using TaggedRecordBatchGenerator = std::function<Future<TaggedRecordBatch>()>;
using TaggedRecordBatchIterator = Iterator<TaggedRecordBatch>;

Expand All @@ -320,7 +331,13 @@ using TaggedRecordBatchIterator = Iterator<TaggedRecordBatch>;
struct EnumeratedRecordBatch {
Enumerated<std::shared_ptr<RecordBatch>> record_batch;
Enumerated<std::shared_ptr<Fragment>> fragment;

friend inline bool operator==(const EnumeratedRecordBatch& left,
const EnumeratedRecordBatch& right) {
return left.record_batch == right.record_batch && left.fragment == right.fragment;
}
};

using EnumeratedRecordBatchGenerator = std::function<Future<EnumeratedRecordBatch>()>;
using EnumeratedRecordBatchIterator = Iterator<EnumeratedRecordBatch>;

Expand Down Expand Up @@ -505,6 +522,14 @@ class ARROW_DS_EXPORT ScannerBuilder {
/// ThreadPool found in ScanOptions;
Status UseThreads(bool use_threads = true);

/// \brief Indicate if metadata should be cached when scanning
///
/// Fragments may typically cache metadata to speed up repeated accesses.
/// However, in use cases where a single scan is done, or if memory use
/// is more critical than CPU time, setting this option to false can
/// lessen memory use.
Status CacheMetadata(bool cache_metadata = true);
Comment thread
mapleFU marked this conversation as resolved.
Resolved

/// \brief Set the maximum number of rows per RecordBatch.
///
/// \param[in] batch_size the maximum number of rows.
Expand Down
Loading