Skip to content

Commit 8116998

Browse files
authored
ARROW-16910: [C++] Add Equals method for FileFragment (#13490)
Adding Equals method for `FileFragment` Authored-by: Vibhatha Abeykoon <vibhatha@gmail.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 0fdb9cc commit 8116998

7 files changed

Lines changed: 40 additions & 2 deletions

File tree

cpp/src/arrow/dataset/file_base.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ Result<std::shared_ptr<io::InputStream>> FileSource::OpenCompressed(
8989
return io::CompressedInputStream::Make(codec.get(), std::move(file));
9090
}
9191

92+
bool FileSource::Equals(const FileSource& other) const {
93+
bool match_file_system =
94+
(filesystem_ == nullptr && other.filesystem_ == nullptr) ||
95+
(filesystem_ && other.filesystem_ && filesystem_->Equals(other.filesystem_));
96+
return match_file_system && file_info_.Equals(other.file_info_) &&
97+
buffer_->Equals(*other.buffer_) && compression_ == other.compression_;
98+
}
99+
92100
Future<util::optional<int64_t>> FileFormat::CountRows(
93101
const std::shared_ptr<FileFragment>&, compute::Expression,
94102
const std::shared_ptr<ScanOptions>&) {
@@ -135,6 +143,10 @@ Future<util::optional<int64_t>> FileFragment::CountRows(
135143
return format()->CountRows(self, std::move(predicate), options);
136144
}
137145

146+
bool FileFragment::Equals(const FileFragment& other) const {
147+
return source_.Equals(other.source_) && format_->Equals(*other.format_);
148+
}
149+
138150
struct FileSystemDataset::FragmentSubtrees {
139151
// Forest for skipping fragments based on extracted subtree expressions
140152
compute::Forest forest;

cpp/src/arrow/dataset/file_base.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace dataset {
4646

4747
/// \brief The path and filesystem where an actual file is located or a buffer which can
4848
/// be read like a file
49-
class ARROW_DS_EXPORT FileSource {
49+
class ARROW_DS_EXPORT FileSource : public util::EqualityComparable<FileSource> {
5050
public:
5151
FileSource(std::string path, std::shared_ptr<fs::FileSystem> filesystem,
5252
Compression::type compression = Compression::UNCOMPRESSED)
@@ -114,6 +114,9 @@ class ARROW_DS_EXPORT FileSource {
114114
Result<std::shared_ptr<io::InputStream>> OpenCompressed(
115115
util::optional<Compression::type> compression = util::nullopt) const;
116116

117+
/// \brief equality comparison with another FileSource
118+
bool Equals(const FileSource& other) const;
119+
117120
private:
118121
static Result<std::shared_ptr<io::RandomAccessFile>> InvalidOpen() {
119122
return Status::Invalid("Called Open() on an uninitialized FileSource");
@@ -179,7 +182,8 @@ class ARROW_DS_EXPORT FileFormat : public std::enable_shared_from_this<FileForma
179182
};
180183

181184
/// \brief A Fragment that is stored in a file with a known format
182-
class ARROW_DS_EXPORT FileFragment : public Fragment {
185+
class ARROW_DS_EXPORT FileFragment : public Fragment,
186+
public util::EqualityComparable<FileFragment> {
183187
public:
184188
Result<RecordBatchGenerator> ScanBatchesAsync(
185189
const std::shared_ptr<ScanOptions>& options) override;
@@ -193,6 +197,8 @@ class ARROW_DS_EXPORT FileFragment : public Fragment {
193197
const FileSource& source() const { return source_; }
194198
const std::shared_ptr<FileFormat>& format() const { return format_; }
195199

200+
bool Equals(const FileFragment& other) const;
201+
196202
protected:
197203
FileFragment(FileSource source, std::shared_ptr<FileFormat> format,
198204
compute::Expression partition_expression,

cpp/src/arrow/dataset/file_csv_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ TEST_P(TestCsvFileFormat, WriteRecordBatchReaderCustomOptions) {
359359

360360
TEST_P(TestCsvFileFormat, CountRows) { TestCountRows(); }
361361

362+
TEST_P(TestCsvFileFormat, FragmentEquals) { TestFragmentEquals(); }
363+
362364
INSTANTIATE_TEST_SUITE_P(TestUncompressedCsv, TestCsvFileFormat,
363365
::testing::Values(Compression::UNCOMPRESSED));
364366
#ifdef ARROW_WITH_BZ2

cpp/src/arrow/dataset/file_ipc_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ TEST_F(TestIpcFileFormat, InspectFailureWithRelevantError) {
8989
TEST_F(TestIpcFileFormat, Inspect) { TestInspect(); }
9090
TEST_F(TestIpcFileFormat, IsSupported) { TestIsSupported(); }
9191
TEST_F(TestIpcFileFormat, CountRows) { TestCountRows(); }
92+
TEST_F(TestIpcFileFormat, FragmentEquals) { TestFragmentEquals(); }
9293

9394
class TestIpcFileSystemDataset : public testing::Test,
9495
public WriteFileSystemDatasetMixin {

cpp/src/arrow/dataset/file_orc_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ TEST_F(TestOrcFileFormat, InspectFailureWithRelevantError) {
6262
TEST_F(TestOrcFileFormat, Inspect) { TestInspect(); }
6363
TEST_F(TestOrcFileFormat, IsSupported) { TestIsSupported(); }
6464
TEST_F(TestOrcFileFormat, CountRows) { TestCountRows(); }
65+
TEST_F(TestOrcFileFormat, FragmentEquals) { TestFragmentEquals(); }
6566

6667
// TODO add TestOrcFileSystemDataset if write support is added
6768

cpp/src/arrow/dataset/file_parquet_test.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ TEST_F(TestParquetFileFormat, WriteRecordBatchReaderCustomOptions) {
218218

219219
TEST_F(TestParquetFileFormat, CountRows) { TestCountRows(); }
220220

221+
TEST_F(TestParquetFileFormat, FragmentEquals) { TestFragmentEquals(); }
222+
221223
TEST_F(TestParquetFileFormat, CountRowsPredicatePushdown) {
222224
constexpr int64_t kNumRowGroups = 16;
223225
constexpr int64_t kTotalNumRows = kNumRowGroups * (kNumRowGroups + 1) / 2;

cpp/src/arrow/dataset/test_util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,20 @@ class FileFormatFixtureMixin : public ::testing::Test {
555555
ASSERT_OK_AND_ASSIGN(predicate, predicate.Bind(*full_schema));
556556
ASSERT_FINISHES_OK_AND_EQ(util::nullopt, fragment->CountRows(predicate, options));
557557
}
558+
void TestFragmentEquals() {
559+
auto options = std::make_shared<ScanOptions>();
560+
auto this_schema = schema({field("f64", float64())});
561+
auto other_schema = schema({field("f32", float32())});
562+
auto reader = this->GetRecordBatchReader(this_schema);
563+
auto other_reader = this->GetRecordBatchReader(other_schema);
564+
auto source = this->GetFileSource(reader.get());
565+
auto other_source = this->GetFileSource(other_reader.get());
566+
567+
auto fragment = this->MakeFragment(*source);
568+
EXPECT_TRUE(fragment->Equals(*fragment));
569+
auto other = this->MakeFragment(*other_source);
570+
EXPECT_FALSE(fragment->Equals(*other));
571+
}
558572

559573
protected:
560574
std::shared_ptr<typename FormatHelper::FormatType> format_;

0 commit comments

Comments
 (0)