(Doc section: Projecting columns) Read a dataset, but with column projection. This is useful to derive new columns from existing data. For example, here we demonstrate casting a column to a different type, and turning a numeric column into a boolean column based on a predicate. You could also rename columns or perform computations involving multiple columns.
| 214 | // boolean column based on a predicate. You could also rename columns or perform |
| 215 | // computations involving multiple columns. |
| 216 | arrow::Result<std::shared_ptr<arrow::Table>> ProjectDataset( |
| 217 | const std::shared_ptr<fs::FileSystem>& filesystem, |
| 218 | const std::shared_ptr<ds::FileFormat>& format, const std::string& base_dir) { |
| 219 | fs::FileSelector selector; |
| 220 | selector.base_dir = base_dir; |
| 221 | ARROW_ASSIGN_OR_RAISE( |
| 222 | auto factory, ds::FileSystemDatasetFactory::Make(filesystem, selector, format, |
| 223 | ds::FileSystemFactoryOptions())); |
| 224 | ARROW_ASSIGN_OR_RAISE(auto dataset, factory->Finish()); |
| 225 | // Read specified columns with a row filter |
| 226 | ARROW_ASSIGN_OR_RAISE(auto scan_builder, dataset->NewScan()); |
| 227 | ARROW_RETURN_NOT_OK(scan_builder->Project( |
| 228 | { |
| 229 | // Leave column "a" as-is. |
| 230 | cp::field_ref("a"), |
| 231 | // Cast column "b" to float32. |
| 232 | cp::call("cast", {cp::field_ref("b")}, |
| 233 | arrow::compute::CastOptions::Safe(arrow::float32())), |
| 234 | // Derive a boolean column from "c". |
| 235 | cp::equal(cp::field_ref("c"), cp::literal(1)), |
| 236 | }, |
| 237 | {"a_renamed", "b_as_float32", "c_1"})); |
| 238 | ARROW_ASSIGN_OR_RAISE(auto scanner, scan_builder->Finish()); |
| 239 | return scanner->ToTable(); |
| 240 | } |
| 241 | // (Doc section: Projecting columns) |
| 242 | |
| 243 | // (Doc section: Projecting columns #2) |