Skip to content

Commit ffd31d8

Browse files
authored
ARROW-17078: [C++] Clean up error handling in C++ Examples (apache#13598)
A simple util file was added to include a macro used in each example and may be in future we can include common utils like data generators, writers, etc in the here. This is just a suggestion. Authored-by: Vibhatha Abeykoon <vibhatha@gmail.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 4db3222 commit ffd31d8

10 files changed

Lines changed: 296 additions & 283 deletions

‎cpp/examples/arrow/compute_and_write_csv_example.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ arrow::Status RunMain(int argc, char** argv) {
106106
int main(int argc, char** argv) {
107107
arrow::Status status = RunMain(argc, argv);
108108
if (!status.ok()) {
109-
std::cerr << status << std::endl;
109+
std::cerr << status.ToString() << std::endl;
110110
return EXIT_FAILURE;
111111
}
112112
return EXIT_SUCCESS;

‎cpp/examples/arrow/compute_register_example.cc‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@
3131

3232
namespace cp = ::arrow::compute;
3333

34-
#define ABORT_ON_FAILURE(expr) \
35-
do { \
36-
arrow::Status status_ = (expr); \
37-
if (!status_.ok()) { \
38-
std::cerr << status_.message() << std::endl; \
39-
abort(); \
40-
} \
41-
} while (0);
42-
4334
class ExampleFunctionOptionsType : public cp::FunctionOptionsType {
4435
const char* type_name() const override { return "ExampleFunctionOptionsType"; }
4536
std::string Stringify(const cp::FunctionOptions&) const override {
@@ -124,23 +115,23 @@ const cp::FunctionDoc func_doc{
124115
{"x"},
125116
"ExampleFunctionOptions"};
126117

127-
int main(int argc, char** argv) {
118+
arrow::Status RunComputeRegister(int argc, char** argv) {
128119
const std::string name = "compute_register_example";
129120
auto func = std::make_shared<cp::ScalarFunction>(name, cp::Arity::Unary(), func_doc);
130121
cp::ScalarKernel kernel({arrow::int64()}, arrow::int64(), ExampleFunctionImpl);
131122
kernel.mem_allocation = cp::MemAllocation::NO_PREALLOCATE;
132-
ABORT_ON_FAILURE(func->AddKernel(std::move(kernel)));
123+
ARROW_RETURN_NOT_OK(func->AddKernel(std::move(kernel)));
133124

134125
auto registry = cp::GetFunctionRegistry();
135-
ABORT_ON_FAILURE(registry->AddFunction(std::move(func)));
126+
ARROW_RETURN_NOT_OK(registry->AddFunction(std::move(func)));
136127

137128
arrow::Int64Builder builder(arrow::default_memory_pool());
138129
std::shared_ptr<arrow::Array> arr;
139-
ABORT_ON_FAILURE(builder.Append(42));
140-
ABORT_ON_FAILURE(builder.Finish(&arr));
130+
ARROW_RETURN_NOT_OK(builder.Append(42));
131+
ARROW_RETURN_NOT_OK(builder.Finish(&arr));
141132
auto options = std::make_shared<ExampleFunctionOptions>();
142133
auto maybe_result = cp::CallFunction(name, {arr}, options.get());
143-
ABORT_ON_FAILURE(maybe_result.status());
134+
ARROW_RETURN_NOT_OK(maybe_result.status());
144135

145136
std::cout << maybe_result->make_array()->ToString() << std::endl;
146137

@@ -151,15 +142,15 @@ int main(int argc, char** argv) {
151142
std::cerr << maybe_serialized.status().ToString() << std::endl;
152143

153144
auto exec_registry = cp::default_exec_factory_registry();
154-
ABORT_ON_FAILURE(
145+
ARROW_RETURN_NOT_OK(
155146
exec_registry->AddFactory("compute_register_example", ExampleExecNodeFactory));
156147

157148
auto maybe_plan = cp::ExecPlan::Make();
158-
ABORT_ON_FAILURE(maybe_plan.status());
159-
auto plan = maybe_plan.ValueOrDie();
149+
ARROW_RETURN_NOT_OK(maybe_plan.status());
150+
ARROW_ASSIGN_OR_RAISE(auto plan, maybe_plan);
160151

161152
arrow::AsyncGenerator<arrow::util::optional<cp::ExecBatch>> source_gen, sink_gen;
162-
ABORT_ON_FAILURE(
153+
ARROW_RETURN_NOT_OK(
163154
cp::Declaration::Sequence(
164155
{
165156
{"source", cp::SourceNodeOptions{arrow::schema({}), source_gen}},
@@ -168,6 +159,14 @@ int main(int argc, char** argv) {
168159
})
169160
.AddToPlan(plan.get())
170161
.status());
162+
return arrow::Status::OK();
163+
}
171164

165+
int main(int argc, char** argv) {
166+
auto status = RunComputeRegister(argc, argv);
167+
if (!status.ok()) {
168+
std::cerr << status.ToString() << std::endl;
169+
return EXIT_FAILURE;
170+
}
172171
return EXIT_SUCCESS;
173172
}

‎cpp/examples/arrow/dataset_documentation_example.cc‎

Lines changed: 141 additions & 124 deletions
Large diffs are not rendered by default.

‎cpp/examples/arrow/dataset_parquet_scan_example.cc‎

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
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+
3142
using arrow::field;
3243
using arrow::int16;
3344
using arrow::Schema;
@@ -39,15 +50,6 @@ namespace ds = arrow::dataset;
3950

4051
namespace 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-
5153
struct 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

Comments
 (0)