@@ -19,7 +19,6 @@ use itertools::Itertools;
1919use std:: ops:: Deref ;
2020use std:: sync:: Arc ;
2121
22- use arrow_buffer:: ToByteSlice ;
2322use datafusion:: arrow:: datatypes:: IntervalUnit ;
2423use datafusion:: logical_expr:: {
2524 CrossJoin , Distinct , Like , Partitioning , WindowFrameUnits ,
@@ -37,8 +36,7 @@ use crate::variation_const::{
3736 DATE_32_TYPE_VARIATION_REF , DATE_64_TYPE_VARIATION_REF ,
3837 DECIMAL_128_TYPE_VARIATION_REF , DECIMAL_256_TYPE_VARIATION_REF ,
3938 DEFAULT_CONTAINER_TYPE_VARIATION_REF , DEFAULT_TYPE_VARIATION_REF ,
40- INTERVAL_MONTH_DAY_NANO_TYPE_NAME , LARGE_CONTAINER_TYPE_VARIATION_REF ,
41- UNSIGNED_INTEGER_TYPE_VARIATION_REF ,
39+ LARGE_CONTAINER_TYPE_VARIATION_REF , UNSIGNED_INTEGER_TYPE_VARIATION_REF ,
4240} ;
4341use datafusion:: arrow:: array:: { Array , GenericListArray , OffsetSizeTrait } ;
4442use datafusion:: common:: {
@@ -56,8 +54,8 @@ use substrait::proto::exchange_rel::{ExchangeKind, RoundRobin, ScatterFields};
5654use substrait:: proto:: expression:: literal:: interval_day_to_second:: PrecisionMode ;
5755use substrait:: proto:: expression:: literal:: map:: KeyValue ;
5856use substrait:: proto:: expression:: literal:: {
59- user_defined , IntervalDayToSecond , IntervalYearToMonth , List , Map ,
60- PrecisionTimestamp , Struct , UserDefined ,
57+ IntervalCompound , IntervalDayToSecond , IntervalYearToMonth , List , Map ,
58+ PrecisionTimestamp , Struct ,
6159} ;
6260use substrait:: proto:: expression:: subquery:: InPredicate ;
6361use substrait:: proto:: expression:: window_function:: BoundsType ;
@@ -1429,16 +1427,14 @@ fn to_substrait_type(
14291427 } ) ) ,
14301428 } ) ,
14311429 IntervalUnit :: MonthDayNano => {
1432- // Substrait doesn't currently support this type, so we represent it as a UDT
14331430 Ok ( substrait:: proto:: Type {
1434- kind : Some ( r#type:: Kind :: UserDefined ( r#type:: UserDefined {
1435- type_reference : extensions. register_type (
1436- INTERVAL_MONTH_DAY_NANO_TYPE_NAME . to_string ( ) ,
1437- ) ,
1438- type_variation_reference : DEFAULT_TYPE_VARIATION_REF ,
1439- nullability,
1440- type_parameters : vec ! [ ] ,
1441- } ) ) ,
1431+ kind : Some ( r#type:: Kind :: IntervalCompound (
1432+ r#type:: IntervalCompound {
1433+ type_variation_reference : DEFAULT_TYPE_VARIATION_REF ,
1434+ nullability,
1435+ precision : 9 , // nanos
1436+ } ,
1437+ ) ) ,
14421438 } )
14431439 }
14441440 }
@@ -1880,23 +1876,21 @@ fn to_substrait_literal(
18801876 } ) ,
18811877 DEFAULT_TYPE_VARIATION_REF ,
18821878 ) ,
1883- ScalarValue :: IntervalMonthDayNano ( Some ( i) ) => {
1884- // IntervalMonthDayNano is internally represented as a 128-bit integer, containing
1885- // months (32bit), days (32bit), and nanoseconds (64bit)
1886- let bytes = i. to_byte_slice ( ) ;
1887- (
1888- LiteralType :: UserDefined ( UserDefined {
1889- type_reference : extensions
1890- . register_type ( INTERVAL_MONTH_DAY_NANO_TYPE_NAME . to_string ( ) ) ,
1891- type_parameters : vec ! [ ] ,
1892- val : Some ( user_defined:: Val :: Value ( ProtoAny {
1893- type_url : INTERVAL_MONTH_DAY_NANO_TYPE_NAME . to_string ( ) ,
1894- value : bytes. to_vec ( ) . into ( ) ,
1895- } ) ) ,
1879+ ScalarValue :: IntervalMonthDayNano ( Some ( i) ) => (
1880+ LiteralType :: IntervalCompound ( IntervalCompound {
1881+ interval_year_to_month : Some ( IntervalYearToMonth {
1882+ years : 0 ,
1883+ months : i. months ,
18961884 } ) ,
1897- DEFAULT_TYPE_VARIATION_REF ,
1898- )
1899- }
1885+ interval_day_to_second : Some ( IntervalDayToSecond {
1886+ days : i. days ,
1887+ seconds : 0 ,
1888+ subseconds : i. nanoseconds ,
1889+ precision_mode : Some ( PrecisionMode :: Precision ( 9 ) ) , // nanoseconds
1890+ } ) ,
1891+ } ) ,
1892+ DEFAULT_TYPE_VARIATION_REF ,
1893+ ) ,
19001894 ScalarValue :: IntervalDayTime ( Some ( i) ) => (
19011895 LiteralType :: IntervalDayToSecond ( IntervalDayToSecond {
19021896 days : i. days ,
@@ -2161,7 +2155,6 @@ mod test {
21612155 } ;
21622156 use datafusion:: arrow:: datatypes:: Field ;
21632157 use datafusion:: common:: scalar:: ScalarStructBuilder ;
2164- use std:: collections:: HashMap ;
21652158
21662159 #[ test]
21672160 fn round_trip_literals ( ) -> Result < ( ) > {
@@ -2292,39 +2285,6 @@ mod test {
22922285 Ok ( ( ) )
22932286 }
22942287
2295- #[ test]
2296- fn custom_type_literal_extensions ( ) -> Result < ( ) > {
2297- let mut extensions = Extensions :: default ( ) ;
2298- // IntervalMonthDayNano is represented as a custom type in Substrait
2299- let scalar = ScalarValue :: IntervalMonthDayNano ( Some ( IntervalMonthDayNano :: new (
2300- 17 , 25 , 1234567890 ,
2301- ) ) ) ;
2302- let substrait_literal = to_substrait_literal ( & scalar, & mut extensions) ?;
2303- let roundtrip_scalar =
2304- from_substrait_literal_without_names ( & substrait_literal, & extensions) ?;
2305- assert_eq ! ( scalar, roundtrip_scalar) ;
2306-
2307- assert_eq ! (
2308- extensions,
2309- Extensions {
2310- functions: HashMap :: new( ) ,
2311- types: HashMap :: from( [ (
2312- 0 ,
2313- INTERVAL_MONTH_DAY_NANO_TYPE_NAME . to_string( )
2314- ) ] ) ,
2315- type_variations: HashMap :: new( ) ,
2316- }
2317- ) ;
2318-
2319- // Check we fail if we don't propagate extensions
2320- assert ! ( from_substrait_literal_without_names(
2321- & substrait_literal,
2322- & Extensions :: default ( )
2323- )
2324- . is_err( ) ) ;
2325- Ok ( ( ) )
2326- }
2327-
23282288 #[ test]
23292289 fn round_trip_types ( ) -> Result < ( ) > {
23302290 round_trip_type ( DataType :: Boolean ) ?;
@@ -2403,35 +2363,4 @@ mod test {
24032363 assert_eq ! ( dt, roundtrip_dt) ;
24042364 Ok ( ( ) )
24052365 }
2406-
2407- #[ test]
2408- fn custom_type_extensions ( ) -> Result < ( ) > {
2409- let mut extensions = Extensions :: default ( ) ;
2410- // IntervalMonthDayNano is represented as a custom type in Substrait
2411- let dt = DataType :: Interval ( IntervalUnit :: MonthDayNano ) ;
2412-
2413- let substrait = to_substrait_type ( & dt, true , & mut extensions) ?;
2414- let roundtrip_dt = from_substrait_type_without_names ( & substrait, & extensions) ?;
2415- assert_eq ! ( dt, roundtrip_dt) ;
2416-
2417- assert_eq ! (
2418- extensions,
2419- Extensions {
2420- functions: HashMap :: new( ) ,
2421- types: HashMap :: from( [ (
2422- 0 ,
2423- INTERVAL_MONTH_DAY_NANO_TYPE_NAME . to_string( )
2424- ) ] ) ,
2425- type_variations: HashMap :: new( ) ,
2426- }
2427- ) ;
2428-
2429- // Check we fail if we don't propagate extensions
2430- assert ! (
2431- from_substrait_type_without_names( & substrait, & Extensions :: default ( ) )
2432- . is_err( )
2433- ) ;
2434-
2435- Ok ( ( ) )
2436- }
24372366}
0 commit comments