@@ -481,6 +481,110 @@ impl From<DFParquetCompression> for parquet::basic::Compression {
481481 }
482482}
483483
484+ /// Time unit for reading Parquet INT96 timestamps
485+ ///
486+ /// This enum validates `coerce_int96` at configuration time, so only `ns`,
487+ /// `us`, `ms` or `s` can be set via `SET` commands or deserialization.
488+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
489+ pub enum DFTimeUnit {
490+ Nanosecond ,
491+ Microsecond ,
492+ Millisecond ,
493+ Second ,
494+ }
495+
496+ impl FromStr for DFTimeUnit {
497+ type Err = DataFusionError ;
498+
499+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
500+ match s. to_lowercase ( ) . as_str ( ) {
501+ "ns" => Ok ( Self :: Nanosecond ) ,
502+ "us" => Ok ( Self :: Microsecond ) ,
503+ "ms" => Ok ( Self :: Millisecond ) ,
504+ "s" => Ok ( Self :: Second ) ,
505+ other => Err ( DataFusionError :: Configuration ( format ! (
506+ "Invalid parquet coerce_int96 setting: {other}. Expected one of: ns, us, ms, s"
507+ ) ) ) ,
508+ }
509+ }
510+ }
511+
512+ impl Display for DFTimeUnit {
513+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
514+ let s = match self {
515+ Self :: Nanosecond => "ns" ,
516+ Self :: Microsecond => "us" ,
517+ Self :: Millisecond => "ms" ,
518+ Self :: Second => "s" ,
519+ } ;
520+ f. write_str ( s)
521+ }
522+ }
523+
524+ impl ConfigField for DFTimeUnit {
525+ fn visit < V : Visit > ( & self , v : & mut V , key : & str , description : & ' static str ) {
526+ v. some ( key, self , description)
527+ }
528+
529+ fn set ( & mut self , key : & str , value : & str ) -> Result < ( ) > {
530+ if !key. is_empty ( ) {
531+ return crate :: error:: _config_err!(
532+ "Config field parquet.coerce_int96 is a scalar DFTimeUnit and does not have nested field \" {}\" " ,
533+ key
534+ ) ;
535+ }
536+
537+ * self = Self :: from_str ( value) ?;
538+ Ok ( ( ) )
539+ }
540+ }
541+
542+ /// `ConfigField` for `Option<DFTimeUnit>` parses before assigning so an
543+ /// invalid value does not turn an unset option into the default.
544+ impl ConfigField for Option < DFTimeUnit > {
545+ fn visit < V : Visit > ( & self , v : & mut V , key : & str , description : & ' static str ) {
546+ match self {
547+ Some ( time_unit) => time_unit. visit ( v, key, description) ,
548+ None => v. none ( key, description) ,
549+ }
550+ }
551+
552+ fn set ( & mut self , key : & str , value : & str ) -> Result < ( ) > {
553+ if !key. is_empty ( ) {
554+ return crate :: error:: _config_err!(
555+ "Config field parquet.coerce_int96 is a scalar Option<DFTimeUnit> and does not have nested field \" {}\" " ,
556+ key
557+ ) ;
558+ }
559+
560+ * self = Some ( DFTimeUnit :: from_str ( value) ?) ;
561+ Ok ( ( ) )
562+ }
563+
564+ fn reset ( & mut self , key : & str ) -> Result < ( ) > {
565+ if key. is_empty ( ) {
566+ * self = None ;
567+ Ok ( ( ) )
568+ } else {
569+ crate :: error:: _config_err!(
570+ "Config field parquet.coerce_int96 is a scalar Option<DFTimeUnit> and does not have nested field \" {}\" " ,
571+ key
572+ )
573+ }
574+ }
575+ }
576+
577+ impl From < DFTimeUnit > for arrow:: datatypes:: TimeUnit {
578+ fn from ( value : DFTimeUnit ) -> Self {
579+ match value {
580+ DFTimeUnit :: Nanosecond => Self :: Nanosecond ,
581+ DFTimeUnit :: Microsecond => Self :: Microsecond ,
582+ DFTimeUnit :: Millisecond => Self :: Millisecond ,
583+ DFTimeUnit :: Second => Self :: Second ,
584+ }
585+ }
586+ }
587+
484588#[ cfg( test) ]
485589mod tests {
486590 use super :: * ;
@@ -566,4 +670,25 @@ mod tests {
566670 }
567671 assert ! ( ZstdLevel :: try_new( * ZSTD_LEVELS . end( ) as i32 + 1 ) . is_err( ) ) ;
568672 }
673+
674+ #[ test]
675+ fn time_unit_parses_and_displays ( ) {
676+ for ( input, expected, displayed) in [
677+ ( "ns" , DFTimeUnit :: Nanosecond , "ns" ) ,
678+ ( "US" , DFTimeUnit :: Microsecond , "us" ) ,
679+ ( "ms" , DFTimeUnit :: Millisecond , "ms" ) ,
680+ ( "s" , DFTimeUnit :: Second , "s" ) ,
681+ ] {
682+ let parsed: DFTimeUnit = input. parse ( ) . unwrap ( ) ;
683+ assert_eq ! ( parsed, expected, "input: {input}" ) ;
684+ assert_eq ! ( parsed. to_string( ) , displayed) ;
685+ }
686+ for input in [ "nanos" , "" , "ns " ] {
687+ let err = input. parse :: < DFTimeUnit > ( ) . unwrap_err ( ) . to_string ( ) ;
688+ assert ! (
689+ err. contains( "Invalid parquet coerce_int96 setting" ) ,
690+ "input {input:?}: {err}"
691+ ) ;
692+ }
693+ }
569694}
0 commit comments