@@ -24,14 +24,17 @@ use crate::common::seed::{
2424 time64_values,
2525} ;
2626use crate :: common:: stack:: { CatalogConnectionPassword , seaweedfs_url_internal} ;
27+ use crate :: common:: timestamp_precision:: {
28+ assert_filters_match_native_copy, engine_honors_declared_precision,
29+ } ;
2730use crate :: fixtures:: { stack, vs_table} ;
2831
2932use arrow:: array:: {
3033 Array , ArrayRef , BooleanArray , Date32Array , Decimal128Array , Decimal256Array ,
3134 DurationMicrosecondArray , Float32Array , Float64Array , Int32Array , Int64Array ,
3235 IntervalDayTimeArray , ListBuilder , MapArray , StringArray , StringBuilder , StructArray ,
33- Time32MillisecondArray , TimestampMicrosecondArray , UInt8Array , UInt16Array , UInt32Array ,
34- UInt64Array ,
36+ Time32MillisecondArray , TimestampMicrosecondArray , TimestampNanosecondArray , UInt8Array ,
37+ UInt16Array , UInt32Array , UInt64Array ,
3538} ;
3639use arrow:: buffer:: { NullBuffer , OffsetBuffer } ;
3740use arrow:: compute:: cast;
@@ -533,10 +536,58 @@ fn all_types_batch() -> RecordBatch {
533536 ( "c_largebinary" , binary_values( & DataType :: LargeBinary ) , true ) ,
534537 ( "c_fixedsizebinary" , fixed_16_values( ) , true ) ,
535538 ( "c_struct_binary" , struct_binary_values( None ) , true ) ,
539+ (
540+ "c_ts_ms" ,
541+ timestamp_unit_values( TimeUnit :: Millisecond , None ) ,
542+ true ,
543+ ) ,
544+ (
545+ "c_ts_us" ,
546+ timestamp_unit_values( TimeUnit :: Microsecond , None ) ,
547+ true ,
548+ ) ,
549+ (
550+ "c_ts_ns" ,
551+ timestamp_unit_values( TimeUnit :: Nanosecond , None ) ,
552+ true ,
553+ ) ,
554+ (
555+ "c_tstz_ms" ,
556+ timestamp_unit_values( TimeUnit :: Millisecond , Some ( "UTC" ) ) ,
557+ true ,
558+ ) ,
559+ (
560+ "c_tstz_us" ,
561+ timestamp_unit_values( TimeUnit :: Microsecond , Some ( "UTC" ) ) ,
562+ true ,
563+ ) ,
564+ (
565+ "c_tstz_ns" ,
566+ timestamp_unit_values( TimeUnit :: Nanosecond , Some ( "UTC" ) ) ,
567+ true ,
568+ ) ,
536569 ] )
537570 . expect ( "all_types batch" )
538571}
539572
573+ /// 2025-06-01 00:00:00.123456789 UTC: every unit's own digits differ from the coarser units'.
574+ const TIMESTAMP_UNITS_NANOS : i64 = 1_748_736_000_123_456_789 ;
575+
576+ /// `TIMESTAMP_UNITS_NANOS` truncated to `unit`, the epoch, and NULL.
577+ fn timestamp_unit_values ( unit : TimeUnit , tz : Option < & str > ) -> ArrayRef {
578+ let divisor = match unit {
579+ TimeUnit :: Millisecond => 1_000_000 ,
580+ TimeUnit :: Microsecond => 1_000 ,
581+ _ => 1 ,
582+ } ;
583+ let nanos: ArrayRef = Arc :: new ( TimestampNanosecondArray :: from ( vec ! [
584+ Some ( TIMESTAMP_UNITS_NANOS / divisor * divisor) ,
585+ Some ( 0 ) ,
586+ None ,
587+ ] ) ) ;
588+ cast ( & nanos, & DataType :: Timestamp ( unit, tz. map ( Into :: into) ) ) . expect ( "timestamp unit cast" )
589+ }
590+
540591/// Types only a Parquet logical-type annotation, or its absence, can declare, written with
541592/// no embedded Arrow schema so the reader sees the annotation alone.
542593fn annotated_types_bytes ( ) -> Bytes {
@@ -1099,10 +1150,10 @@ fn events_directory_declares_and_returns_mixed_types_across_both_files() {
10991150 "EVENTS.{column}"
11001151 ) ;
11011152 }
1102- let event_ts = declared_type ( & mut conn , VS_DIRECT , "EVENTS" , "EVENT_TS" ) ;
1103- assert ! (
1104- event_ts . starts_with ( "TIMESTAMP" ) ,
1105- "EVENTS.EVENT_TS must declare a TIMESTAMP of the engine's precision, got {event_ts} "
1153+ assert_eq ! (
1154+ declared_type ( & mut conn , VS_DIRECT , "EVENTS" , "EVENT_TS" ) ,
1155+ TimestampUnitExpectations :: for_engine ( & mut conn ) . us . 0 ,
1156+ "EVENTS.EVENT_TS is a microsecond Parquet timestamp "
11061157 ) ;
11071158
11081159 let cols = conn. query_columns ( & format ! (
@@ -1209,12 +1260,15 @@ fn complex_directory_declares_varchar_and_returns_parseable_json() {
12091260/// Scenario: Out-of-range Decimal128 falls back to VARCHAR via JSON
12101261/// Scenario: A Parquet-sourced column maps through the Arrow-input direction
12111262/// Scenario: The listing declares a binary column
1263+ /// Scenario: A direct-storage Parquet timestamp declares the precision of its unit
1264+ /// Scenario: A timestamp filter compares at full precision
12121265#[ test]
12131266fn all_types_directories_declare_and_return_their_mapped_values ( ) {
12141267 setup ( ) ;
12151268 let mut conn = exa_conn ( ) ;
12161269
12171270 let binary: & [ & str ] = & [ "type 'binary'" , "#351" ] ;
1271+ let units = TimestampUnitExpectations :: for_engine ( & mut conn) ;
12181272 assert_type_matrix (
12191273 & mut conn,
12201274 VS_DIRECT ,
@@ -1286,6 +1340,12 @@ fn all_types_directories_declare_and_return_their_mapped_values() {
12861340 VARCHAR_JSON ,
12871341 & [ "member 'c_struct_binary.x'" , "type 'binary'" , "#351" ] ,
12881342 ) ,
1343+ reads ( "C_TS_MS" , units. ms . 0 , units. ms . 1 ) ,
1344+ reads ( "C_TS_US" , units. us . 0 , units. us . 1 ) ,
1345+ reads ( "C_TS_NS" , units. ns . 0 , units. ns . 1 ) ,
1346+ reads ( "C_TSTZ_MS" , units. ms . 0 , units. ms . 1 ) ,
1347+ reads ( "C_TSTZ_US" , units. us . 0 , units. us . 1 ) ,
1348+ reads ( "C_TSTZ_NS" , units. ns . 0 , units. ns . 1 ) ,
12891349 ] ,
12901350 ) ;
12911351 assert_decimal_orders_numerically ( & mut conn, VS_DIRECT , "ALL_TYPES" , "C_DECIMAL128_18_0" ) ;
@@ -1300,7 +1360,7 @@ fn all_types_directories_declare_and_return_their_mapped_values() {
13001360 refuses ( "C_UUID" , VARCHAR_JSON , & [ "type 'uuid'" , "#351" ] ) ,
13011361 reads (
13021362 "C_INT96" ,
1303- "TIMESTAMP(3)" ,
1363+ units . us . 0 ,
13041364 [
13051365 Some ( "2024-01-15 10:30:45.000000" ) ,
13061366 Some ( "1970-01-01 00:00:00.000000" ) ,
@@ -1315,6 +1375,79 @@ fn all_types_directories_declare_and_return_their_mapped_values() {
13151375 ) ,
13161376 ] ,
13171377 ) ;
1378+
1379+ if engine_honors_declared_precision ( & mut conn) {
1380+ let all_types = vs_table ( VS_DIRECT , "ALL_TYPES" ) ;
1381+ assert_filters_match_native_copy (
1382+ & mut conn,
1383+ & all_types,
1384+ "C_TS_MS, C_TS_US, C_TS_NS, C_TSTZ_MS, C_TSTZ_NS" ,
1385+ TIMESTAMP_UNIT_FILTERS ,
1386+ ) ;
1387+ conn. execute ( "ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS.FF9'" ) ;
1388+ let nanos = conn. query_columns ( & format ! (
1389+ "SELECT C_TS_NS, C_TSTZ_NS FROM {all_types} WHERE ID = 1"
1390+ ) ) ;
1391+ for column in & nanos {
1392+ assert_eq ! ( value_to_string( & column[ 0 ] ) , "2025-06-01 00:00:00.123456789" ) ;
1393+ }
1394+ }
1395+ }
1396+
1397+ const TIMESTAMP_UNIT_FILTERS : & [ & str ] = & [
1398+ "C_TS_MS = TIMESTAMP '2025-06-01 00:00:00.123000000'" ,
1399+ "C_TS_MS = TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1400+ "C_TS_MS <> TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1401+ "NOT (C_TS_MS = TIMESTAMP '2025-06-01 00:00:00.123500')" ,
1402+ "C_TS_MS < TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1403+ "C_TS_MS <= TIMESTAMP '2025-06-01 00:00:00.122500'" ,
1404+ "C_TS_MS > TIMESTAMP '2025-06-01 00:00:00.122500'" ,
1405+ "C_TS_MS >= TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1406+ "TIMESTAMP '2025-06-01 00:00:00.123500' > C_TS_MS" ,
1407+ "C_TS_MS BETWEEN TIMESTAMP '2025-06-01 00:00:00.122500' \
1408+ AND TIMESTAMP '2025-06-01 00:00:00.123500'",
1409+ "C_TS_MS NOT BETWEEN TIMESTAMP '2025-06-01 00:00:00.123100' \
1410+ AND TIMESTAMP '2025-06-01 00:00:00.123500'",
1411+ "C_TS_MS IN (TIMESTAMP '2025-06-01 00:00:00.123500', TIMESTAMP '1970-01-01 00:00:00.000500')" ,
1412+ "C_TS_MS NOT IN (TIMESTAMP '2025-06-01 00:00:00.123500', TIMESTAMP '1970-01-01 00:00:00')" ,
1413+ "C_TS_MS IS NULL OR C_TS_MS = TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1414+ "C_TS_US = TIMESTAMP '2025-06-01 00:00:00.123456'" ,
1415+ "C_TS_US = TIMESTAMP '2025-06-01 00:00:00.123456789'" ,
1416+ "C_TS_US < TIMESTAMP '2025-06-01 00:00:00.123456001'" ,
1417+ "C_TS_US >= TIMESTAMP '2025-06-01 00:00:00.123456001'" ,
1418+ "C_TS_US IN (TIMESTAMP '2025-06-01 00:00:00.123456789', TIMESTAMP '1970-01-01 00:00:00')" ,
1419+ "C_TS_NS = TIMESTAMP '2025-06-01 00:00:00.123456789'" ,
1420+ "C_TS_NS = TIMESTAMP '2025-06-01 00:00:00.123456'" ,
1421+ "C_TS_NS > TIMESTAMP '2025-06-01 00:00:00.123456'" ,
1422+ "C_TSTZ_MS = TIMESTAMP '2025-06-01 00:00:00.123500'" ,
1423+ "C_TSTZ_NS = TIMESTAMP '2025-06-01 00:00:00.123456789'" ,
1424+ "C_TS_MS = C_TS_NS" ,
1425+ "C_TS_US < C_TS_NS" ,
1426+ ] ;
1427+
1428+ type DeclaredAndValues = ( & ' static str , [ Option < & ' static str > ; 3 ] ) ;
1429+
1430+ /// Declared type and six-digit wire render of each [`timestamp_unit_values`] unit.
1431+ struct TimestampUnitExpectations {
1432+ ms : DeclaredAndValues ,
1433+ us : DeclaredAndValues ,
1434+ ns : DeclaredAndValues ,
1435+ }
1436+
1437+ impl TimestampUnitExpectations {
1438+ fn for_engine ( conn : & mut ExaConn ) -> Self {
1439+ let read = |first| [ Some ( first) , Some ( "1970-01-01 00:00:00.000000" ) , None ] ;
1440+ let ms = ( "TIMESTAMP(3)" , read ( "2025-06-01 00:00:00.123000" ) ) ;
1441+ if !engine_honors_declared_precision ( conn) {
1442+ return Self { ms, us : ms, ns : ms } ;
1443+ }
1444+ Self {
1445+ ms,
1446+ us : ( "TIMESTAMP(6)" , read ( "2025-06-01 00:00:00.123456" ) ) ,
1447+ // The wire renders six digits; the FF9 check reads the rest.
1448+ ns : ( "TIMESTAMP(9)" , read ( "2025-06-01 00:00:00.123456" ) ) ,
1449+ }
1450+ }
13181451}
13191452
13201453#[ test]
0 commit comments