tustvold commented on code in PR #3746: URL: https://github.com/apache/arrow-rs/pull/3746#discussion_r1114894599
########## arrow-csv/src/reader/mod.rs: ########## @@ -62,44 +61,68 @@ use crate::reader::records::{RecordDecoder, StringRecords}; use csv::StringRecord; lazy_static! { + /// Order should match [`InferredDataType`] static ref REGEX_SET: RegexSet = RegexSet::new([ r"(?i)^(true)$|^(false)$(?-i)", //BOOLEAN - r"^-?((\d*\.\d+|\d+\.\d*)([eE]-?\d+)?|\d+([eE]-?\d+))$", //DECIMAL r"^-?(\d+)$", //INTEGER + r"^-?((\d*\.\d+|\d+\.\d*)([eE]-?\d+)?|\d+([eE]-?\d+))$", //DECIMAL r"^\d{4}-\d\d-\d\d$", //DATE32 - r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d$", //DATE64 + r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d$", //Timestamp(Second) + r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d.\d{1,3}$", //Timestamp(Millisecond) + r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d.\d{1,6}$", //Timestamp(Microsecond) + r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d.\d{1,9}$", //Timestamp(Nanosecond) ]).unwrap(); - //The order should match with REGEX_SET - static ref MATCH_DATA_TYPE: Vec<DataType> = vec![ - DataType::Boolean, - DataType::Float64, - DataType::Int64, - DataType::Date32, - DataType::Date64, - ]; static ref PARSE_DECIMAL_RE: Regex = Regex::new(r"^-?(\d+\.?\d*|\d*\.?\d+)$").unwrap(); - static ref DATETIME_RE: Regex = - Regex::new(r"^\d{4}-\d\d-\d\d[T ]\d\d:\d\d:\d\d\.\d{1,9}$").unwrap(); } -/// Infer the data type of a record -fn infer_field_schema(string: &str, datetime_re: Option<Regex>) -> DataType { - // when quoting is enabled in the reader, these quotes aren't escaped, we default to - // Utf8 for them - if string.starts_with('"') { - return DataType::Utf8; - } - let matches = REGEX_SET.matches(string).into_iter().next(); - // match regex in a particular order - match matches { - Some(ix) => MATCH_DATA_TYPE[ix].clone(), - None => { - let datetime_re = datetime_re.unwrap_or_else(|| DATETIME_RE.clone()); - if datetime_re.is_match(string) { - DataType::Timestamp(TimeUnit::Nanosecond, None) - } else { - DataType::Utf8 +#[derive(Default, Copy, Clone)] +struct InferredDataType { Review Comment: This logic should be significantly faster as an added bonus -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org