(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.
| 247 | // the previous two examples: selecting a subset of columns by name, and deriving new |
| 248 | // columns with an expression. |
| 249 | arrow::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) |
no test coverage detected