2828#include < cstdlib>
2929#include < iostream>
3030
31+ /* *
32+ * \brief Run Example
33+ *
34+ * Make sure there is a parquet dataset with the column_names
35+ * mentioned in the Congiguration struct.
36+ *
37+ * Example run:
38+ * ./dataset_parquet_scan_example file:///<some-path>/data.parquet
39+ *
40+ */
41+
3142using arrow::field;
3243using arrow::int16;
3344using arrow::Schema;
@@ -39,15 +50,6 @@ namespace ds = arrow::dataset;
3950
4051namespace cp = arrow::compute;
4152
42- #define ABORT_ON_FAILURE (expr ) \
43- do { \
44- arrow::Status status_ = (expr); \
45- if (!status_.ok ()) { \
46- std::cerr << status_.message () << std::endl; \
47- abort (); \
48- } \
49- } while (0 );
50-
5153struct Configuration {
5254 // Increase the ds::DataSet by repeating `repeat` times the ds::Dataset.
5355 size_t repeat = 1 ;
@@ -62,19 +64,18 @@ struct Configuration {
6264
6365 // Indicates the filter by which rows will be filtered. This optimization can
6466 // make use of partition information and/or file metadata if possible.
65- cp::Expression filter =
66- cp::greater (cp::field_ref(" total_amount" ), cp::literal(1000 .0f ));
67+ cp::Expression filter = cp::greater(cp::field_ref(" total_amount" ), cp::literal(100 .0f ));
6768
6869 ds::InspectOptions inspect_options{};
6970 ds::FinishOptions finish_options{};
7071} conf;
7172
72- std::shared_ptr<fs::FileSystem> GetFileSystemFromUri (const std::string& uri,
73- std::string* path) {
74- return fs::FileSystemFromUri (uri, path). ValueOrDie () ;
73+ arrow::Result< std::shared_ptr<fs::FileSystem>> GetFileSystemFromUri (
74+ const std::string& uri, std::string* path) {
75+ return fs::FileSystemFromUri (uri, path);
7576}
7677
77- std::shared_ptr<ds::Dataset> GetDatasetFromDirectory (
78+ arrow::Result< std::shared_ptr<ds::Dataset> > GetDatasetFromDirectory (
7879 std::shared_ptr<fs::FileSystem> fs, std::shared_ptr<ds::ParquetFileFormat> format,
7980 std::string dir) {
8081 // Find all files under `path`
@@ -84,54 +85,53 @@ std::shared_ptr<ds::Dataset> GetDatasetFromDirectory(
8485
8586 ds::FileSystemFactoryOptions options;
8687 // The factory will try to build a child dataset.
87- auto factory = ds::FileSystemDatasetFactory::Make (fs, s, format, options).ValueOrDie ();
88+ ARROW_ASSIGN_OR_RAISE (auto factory,
89+ ds::FileSystemDatasetFactory::Make (fs, s, format, options));
8890
8991 // Try to infer a common schema for all files.
90- auto schema = factory->Inspect (conf.inspect_options ). ValueOrDie ( );
92+ ARROW_ASSIGN_OR_RAISE ( auto schema, factory->Inspect (conf.inspect_options ));
9193 // Caller can optionally decide another schema as long as it is compatible
9294 // with the previous one, e.g. `factory->Finish(compatible_schema)`.
93- auto child = factory->Finish (conf.finish_options ). ValueOrDie ( );
95+ ARROW_ASSIGN_OR_RAISE ( auto child, factory->Finish (conf.finish_options ));
9496
9597 ds::DatasetVector children{conf.repeat , child};
9698 auto dataset = ds::UnionDataset::Make (std::move (schema), std::move (children));
9799
98- return dataset. ValueOrDie () ;
100+ return dataset;
99101}
100102
101- std::shared_ptr<ds::Dataset> GetParquetDatasetFromMetadata (
103+ arrow::Result< std::shared_ptr<ds::Dataset> > GetParquetDatasetFromMetadata (
102104 std::shared_ptr<fs::FileSystem> fs, std::shared_ptr<ds::ParquetFileFormat> format,
103105 std::string metadata_path) {
104106 ds::ParquetFactoryOptions options;
105- auto factory =
106- ds::ParquetDatasetFactory::Make (metadata_path, fs, format, options). ValueOrDie ( );
107- return factory->Finish (). ValueOrDie () ;
107+ ARROW_ASSIGN_OR_RAISE (
108+ auto factory, ds::ParquetDatasetFactory::Make (metadata_path, fs, format, options));
109+ return factory->Finish ();
108110}
109111
110- std::shared_ptr<ds::Dataset> GetDatasetFromFile (
112+ arrow::Result< std::shared_ptr<ds::Dataset> > GetDatasetFromFile (
111113 std::shared_ptr<fs::FileSystem> fs, std::shared_ptr<ds::ParquetFileFormat> format,
112114 std::string file) {
113115 ds::FileSystemFactoryOptions options;
114116 // The factory will try to build a child dataset.
115- auto factory =
116- ds::FileSystemDatasetFactory::Make (fs, {file}, format, options). ValueOrDie ( );
117+ ARROW_ASSIGN_OR_RAISE ( auto factory,
118+ ds::FileSystemDatasetFactory::Make (fs, {file}, format, options));
117119
118120 // Try to infer a common schema for all files.
119- auto schema = factory->Inspect (conf.inspect_options ). ValueOrDie ( );
121+ ARROW_ASSIGN_OR_RAISE ( auto schema, factory->Inspect (conf.inspect_options ));
120122 // Caller can optionally decide another schema as long as it is compatible
121123 // with the previous one, e.g. `factory->Finish(compatible_schema)`.
122- auto child = factory->Finish (conf.finish_options ). ValueOrDie ( );
124+ ARROW_ASSIGN_OR_RAISE ( auto child, factory->Finish (conf.finish_options ));
123125
124126 ds::DatasetVector children;
125127 children.resize (conf.repeat , child);
126- auto dataset = ds::UnionDataset::Make (std::move (schema), std::move (children));
127-
128- return dataset.ValueOrDie ();
128+ return ds::UnionDataset::Make (std::move (schema), std::move (children));
129129}
130130
131- std::shared_ptr<ds::Dataset> GetDatasetFromPath (
131+ arrow::Result< std::shared_ptr<ds::Dataset> > GetDatasetFromPath (
132132 std::shared_ptr<fs::FileSystem> fs, std::shared_ptr<ds::ParquetFileFormat> format,
133133 std::string path) {
134- auto info = fs->GetFileInfo (path). ValueOrDie ( );
134+ ARROW_ASSIGN_OR_RAISE ( auto info, fs->GetFileInfo (path));
135135 if (info.IsDirectory ()) {
136136 return GetDatasetFromDirectory (fs, format, path);
137137 }
@@ -146,45 +146,52 @@ std::shared_ptr<ds::Dataset> GetDatasetFromPath(
146146 return GetDatasetFromFile (fs, format, path);
147147}
148148
149- std::shared_ptr<ds::Scanner> GetScannerFromDataset (std::shared_ptr<ds::Dataset> dataset,
150- std::vector<std::string> columns,
151- cp::Expression filter,
152- bool use_threads) {
153- auto scanner_builder = dataset->NewScan ().ValueOrDie ();
149+ arrow::Result<std::shared_ptr<ds::Scanner>> GetScannerFromDataset (
150+ std::shared_ptr<ds::Dataset> dataset, std::vector<std::string> columns,
151+ cp::Expression filter, bool use_threads) {
152+ ARROW_ASSIGN_OR_RAISE (auto scanner_builder, dataset->NewScan ());
154153
155154 if (!columns.empty ()) {
156- ABORT_ON_FAILURE (scanner_builder->Project (columns));
155+ ARROW_RETURN_NOT_OK (scanner_builder->Project (columns));
157156 }
158157
159- ABORT_ON_FAILURE (scanner_builder->Filter (filter));
158+ ARROW_RETURN_NOT_OK (scanner_builder->Filter (filter));
160159
161- ABORT_ON_FAILURE (scanner_builder->UseThreads (use_threads));
160+ ARROW_RETURN_NOT_OK (scanner_builder->UseThreads (use_threads));
162161
163- return scanner_builder->Finish (). ValueOrDie () ;
162+ return scanner_builder->Finish ();
164163}
165164
166- std::shared_ptr<Table> GetTableFromScanner (std::shared_ptr<ds::Scanner> scanner) {
167- return scanner->ToTable ().ValueOrDie ();
165+ arrow::Result<std::shared_ptr<Table>> GetTableFromScanner (
166+ std::shared_ptr<ds::Scanner> scanner) {
167+ return scanner->ToTable ();
168168}
169169
170- int main (int argc, char ** argv) {
170+ arrow::Status RunDatasetParquetScan (char ** argv) {
171+ std::string path;
171172 auto format = std::make_shared<ds::ParquetFileFormat>();
173+ ARROW_ASSIGN_OR_RAISE (auto fs, GetFileSystemFromUri (argv[1 ], &path));
174+ ARROW_ASSIGN_OR_RAISE (auto dataset, GetDatasetFromPath (fs, format, path));
175+
176+ ARROW_ASSIGN_OR_RAISE (
177+ auto scanner, GetScannerFromDataset (dataset, conf.projected_columns , conf.filter ,
178+ conf.use_threads ));
179+
180+ ARROW_ASSIGN_OR_RAISE (auto table, GetTableFromScanner (scanner));
181+ std::cout << " Table size: " << table->num_rows () << " \n " ;
182+ return arrow::Status::OK ();
183+ }
172184
185+ int main (int argc, char ** argv) {
173186 if (argc != 2 ) {
174187 // Fake success for CI purposes.
175188 return EXIT_SUCCESS ;
176189 }
177190
178- std::string path;
179- auto fs = GetFileSystemFromUri (argv[1 ], &path);
180-
181- auto dataset = GetDatasetFromPath (fs, format, path);
182-
183- auto scanner = GetScannerFromDataset (dataset, conf.projected_columns , conf.filter ,
184- conf.use_threads );
185-
186- auto table = GetTableFromScanner (scanner);
187- std::cout << " Table size: " << table->num_rows () << " \n " ;
188-
191+ auto status = RunDatasetParquetScan (argv);
192+ if (!status.ok ()) {
193+ std::cerr << status.ToString () << std::endl;
194+ return EXIT_FAILURE ;
195+ }
189196 return EXIT_SUCCESS ;
190197}
0 commit comments