Make conversion schema from options and parsed CSV header
| 671 | |
| 672 | // Make conversion schema from options and parsed CSV header |
| 673 | Status MakeConversionSchema() { |
| 674 | // Append a column converted from CSV data |
| 675 | auto append_csv_column = [&](std::string col_name, int32_t col_index) { |
| 676 | // Does the named column have a fixed type? |
| 677 | auto it = convert_options_.column_types.find(col_name); |
| 678 | if (it == convert_options_.column_types.end()) { |
| 679 | // If not explicitly typed, respect default_column_type when provided |
| 680 | if (convert_options_.default_column_type != nullptr) { |
| 681 | conversion_schema_.columns.push_back(ConversionSchema::TypedColumn( |
| 682 | std::move(col_name), col_index, convert_options_.default_column_type)); |
| 683 | } else { |
| 684 | conversion_schema_.columns.push_back( |
| 685 | ConversionSchema::InferredColumn(std::move(col_name), col_index)); |
| 686 | } |
| 687 | } else { |
| 688 | conversion_schema_.columns.push_back( |
| 689 | ConversionSchema::TypedColumn(std::move(col_name), col_index, it->second)); |
| 690 | } |
| 691 | }; |
| 692 | |
| 693 | // Append a column of nulls |
| 694 | auto append_null_column = [&](std::string col_name) { |
| 695 | // If the named column has a fixed type, use it, otherwise use null() |
| 696 | std::shared_ptr<DataType> type; |
| 697 | auto it = convert_options_.column_types.find(col_name); |
| 698 | if (it == convert_options_.column_types.end()) { |
| 699 | type = null(); |
| 700 | } else { |
| 701 | type = it->second; |
| 702 | } |
| 703 | conversion_schema_.columns.push_back( |
| 704 | ConversionSchema::NullColumn(std::move(col_name), std::move(type))); |
| 705 | }; |
| 706 | |
| 707 | if (convert_options_.include_columns.empty()) { |
| 708 | // Include all columns in CSV file order |
| 709 | for (int32_t col_index = 0; col_index < num_csv_cols(); ++col_index) { |
| 710 | append_csv_column(column_names_[col_index], col_index); |
| 711 | } |
| 712 | } else { |
| 713 | // Include columns from `include_columns` (in that order) |
| 714 | // Compute indices of columns in the CSV file |
| 715 | std::unordered_map<std::string, int32_t> col_indices; |
| 716 | col_indices.reserve(column_names_.size()); |
| 717 | for (int32_t i = 0; i < static_cast<int32_t>(column_names_.size()); ++i) { |
| 718 | col_indices.emplace(column_names_[i], i); |
| 719 | } |
| 720 | |
| 721 | for (const auto& col_name : convert_options_.include_columns) { |
| 722 | auto it = col_indices.find(col_name); |
| 723 | if (it != col_indices.end()) { |
| 724 | append_csv_column(col_name, it->second); |
| 725 | } else if (convert_options_.include_missing_columns) { |
| 726 | append_null_column(col_name); |
| 727 | } else { |
| 728 | return Status::KeyError("Column '", col_name, |
| 729 | "' in include_columns " |
| 730 | "does not exist in CSV file"); |