MCPcopy Create free account
hub / github.com/apache/arrow / SelectAndProjectDataset

Function SelectAndProjectDataset

cpp/examples/arrow/dataset_documentation_example.cc:249–273  ·  view source on GitHub ↗

(Doc section: Projecting columns #2) Read a dataset, but with column projection. This time, we read all original columns plus one derived column. This simply combines the previous two examples: selecting a subset of columns by name, and deriving new columns with an expression.

Source from the content-addressed store, hash-verified

247// the previous two examples: selecting a subset of columns by name, and deriving new
248// columns with an expression.
249arrow::Result<std::shared_ptr<arrow::Table>> SelectAndProjectDataset(
250 const std::shared_ptr<fs::FileSystem>& filesystem,
251 const std::shared_ptr<ds::FileFormat>& format, const std::string& base_dir) {
252 fs::FileSelector selector;
253 selector.base_dir = base_dir;
254 ARROW_ASSIGN_OR_RAISE(
255 auto factory, ds::FileSystemDatasetFactory::Make(filesystem, selector, format,
256 ds::FileSystemFactoryOptions()));
257 ARROW_ASSIGN_OR_RAISE(auto dataset, factory->Finish());
258 // Read specified columns with a row filter
259 ARROW_ASSIGN_OR_RAISE(auto scan_builder, dataset->NewScan());
260 std::vector<std::string> names;
261 std::vector<cp::Expression> exprs;
262 // Read all the original columns.
263 for (const auto& field : dataset->schema()->fields()) {
264 names.push_back(field->name());
265 exprs.push_back(cp::field_ref(field->name()));
266 }
267 // Also derive a new column.
268 names.emplace_back("b_large");
269 exprs.push_back(cp::greater(cp::field_ref("b"), cp::literal(1)));
270 ARROW_RETURN_NOT_OK(scan_builder->Project(exprs, names));
271 ARROW_ASSIGN_OR_RAISE(auto scanner, scan_builder->Finish());
272 return scanner->ToTable();
273}
274// (Doc section: Projecting columns #2)
275
276// (Doc section: Reading and writing partitioned data #2)

Callers 1

RunDatasetDocumentationFunction · 0.85

Calls 10

field_refFunction · 0.85
greaterFunction · 0.85
push_backMethod · 0.80
emplace_backMethod · 0.80
literalFunction · 0.50
fieldsMethod · 0.45
schemaMethod · 0.45
nameMethod · 0.45
ProjectMethod · 0.45
ToTableMethod · 0.45

Tested by

no test coverage detected