Builds a schema from column names and their possible data types. # Arguments `names` - Vector of column names `types` - Vector of possible data types for each column (as HashSets) `disable_inference` - When true, forces all columns with no inferred types to be Utf8. This should be set to true when `schema_infer_max_rec` is explicitly set to 0, indicating the user wants to skip type inference and
(
names: Vec<String>,
types: Vec<HashSet<DataType>>,
disable_inference: bool,
)
| 638 | /// all fields as strings. When false, columns with no inferred types |
| 639 | /// will be set to Null, allowing schema merging to work properly. |
| 640 | fn build_schema_helper( |
| 641 | names: Vec<String>, |
| 642 | types: Vec<HashSet<DataType>>, |
| 643 | disable_inference: bool, |
| 644 | ) -> Schema { |
| 645 | let fields = names |
| 646 | .into_iter() |
| 647 | .zip(types) |
| 648 | .map(|(field_name, mut data_type_possibilities)| { |
| 649 | // ripped from arrow::csv::reader::infer_reader_schema_with_csv_options |
| 650 | // determine data type based on possible types |
| 651 | // if there are incompatible types, use DataType::Utf8 |
| 652 | |
| 653 | // ignore nulls, to avoid conflicting datatypes (e.g. [nulls, int]) being inferred as Utf8. |
| 654 | data_type_possibilities.remove(&DataType::Null); |
| 655 | |
| 656 | match data_type_possibilities.len() { |
| 657 | // When no types were inferred (empty HashSet): |
| 658 | // - If schema_infer_max_rec was explicitly set to 0, return Utf8 |
| 659 | // - Otherwise return Null (whether from reading null values or empty files) |
| 660 | // This allows schema merging to work when reading folders with empty files |
| 661 | 0 => { |
| 662 | if disable_inference { |
| 663 | Field::new(field_name, DataType::Utf8, true) |
| 664 | } else { |
| 665 | Field::new(field_name, DataType::Null, true) |
| 666 | } |
| 667 | } |
| 668 | 1 => Field::new( |
| 669 | field_name, |
| 670 | data_type_possibilities.iter().next().unwrap().clone(), |
| 671 | true, |
| 672 | ), |
| 673 | 2 => { |
| 674 | if data_type_possibilities.contains(&DataType::Int64) |
| 675 | && data_type_possibilities.contains(&DataType::Float64) |
| 676 | { |
| 677 | // we have an integer and double, fall down to double |
| 678 | Field::new(field_name, DataType::Float64, true) |
| 679 | } else { |
| 680 | // default to Utf8 for conflicting datatypes (e.g bool and int) |
| 681 | Field::new(field_name, DataType::Utf8, true) |
| 682 | } |
| 683 | } |
| 684 | _ => Field::new(field_name, DataType::Utf8, true), |
| 685 | } |
| 686 | }) |
| 687 | .collect::<Fields>(); |
| 688 | Schema::new(fields) |
| 689 | } |
| 690 | |
| 691 | impl Default for CsvSerializer { |
| 692 | fn default() -> Self { |
searching dependent graphs…