@@ -97,6 +97,14 @@ private static Setters buildToValueImpl(Type type) {
97
97
return x -> PrimitiveValue .newTzDatetime (castAsZonedDateTime (id , x ));
98
98
case TzTimestamp :
99
99
return x -> PrimitiveValue .newTzTimestamp (castAsZonedDateTime (id , x ));
100
+ case Date32 :
101
+ return x -> castToDate32 (id , x );
102
+ case Datetime64 :
103
+ return x -> castToDateTime64 (id , x );
104
+ case Timestamp64 :
105
+ return x -> castToTimestamp64 (id , x );
106
+ case Interval64 :
107
+ return x -> castToInterval64 (id , x );
100
108
default :
101
109
return x -> {
102
110
throw castNotSupported (id , x );
@@ -502,6 +510,108 @@ private static PrimitiveValue castToTimestamp(PrimitiveType type, Object x) thro
502
510
throw castNotSupported (type , x );
503
511
}
504
512
513
+ private static PrimitiveValue castToInterval64 (PrimitiveType type , Object x ) throws SQLException {
514
+ if (x instanceof Duration ) {
515
+ return PrimitiveValue .newInterval64 ((Duration ) x );
516
+ } else if (x instanceof Long ) {
517
+ return PrimitiveValue .newInterval64 ((Long ) x );
518
+ } else if (x instanceof String ) {
519
+ Duration parsed ;
520
+ try {
521
+ parsed = Duration .parse ((String ) x );
522
+ } catch (DateTimeParseException e ) {
523
+ throw castNotSupported (type , x , e );
524
+ }
525
+ return PrimitiveValue .newInterval64 (parsed );
526
+ }
527
+ throw castNotSupported (type , x );
528
+ }
529
+
530
+ private static PrimitiveValue castToDate32 (PrimitiveType type , Object x ) throws SQLException {
531
+ if (x instanceof Instant ) {
532
+ return PrimitiveValue .newDate32 (((Instant ) x ).atZone (ZoneId .systemDefault ()).toLocalDate ());
533
+ } else if (x instanceof LocalDateTime ) {
534
+ return PrimitiveValue .newDate32 (((LocalDateTime ) x ).toLocalDate ());
535
+ } else if (x instanceof LocalDate ) {
536
+ return PrimitiveValue .newDate32 ((LocalDate ) x );
537
+ } else if (x instanceof Integer ) {
538
+ return PrimitiveValue .newDate32 (LocalDate .ofEpochDay ((Integer ) x ));
539
+ } else if (x instanceof Long ) {
540
+ return PrimitiveValue .newDate32 (LocalDate .ofEpochDay ((Long ) x ));
541
+ } else if (x instanceof Timestamp ) {
542
+ // Normalize date - use system timezone to detect correct date
543
+ Instant instant = Instant .ofEpochMilli (((Timestamp ) x ).getTime ());
544
+ LocalDate ld = instant .atZone (ZoneId .systemDefault ()).toLocalDate ();
545
+ return PrimitiveValue .newDate32 (ld );
546
+ } else if (x instanceof Date ) {
547
+ // Normalize date - use system timezone to detect correct date
548
+ Instant instant = Instant .ofEpochMilli (((Date ) x ).getTime ());
549
+ LocalDate ld = instant .atZone (ZoneId .systemDefault ()).toLocalDate ();
550
+ return PrimitiveValue .newDate32 (ld );
551
+ } else if (x instanceof String ) {
552
+ try {
553
+ return PrimitiveValue .newDate32 (LocalDate .parse ((String ) x ));
554
+ } catch (DateTimeParseException e ) {
555
+ throw castNotSupported (type , x , e );
556
+ }
557
+ }
558
+ throw castNotSupported (type , x );
559
+ }
560
+
561
+ private static PrimitiveValue castToDateTime64 (PrimitiveType type , Object x ) throws SQLException {
562
+ if (x instanceof Instant ) {
563
+ return PrimitiveValue .newDatetime64 (((Instant ) x ).atZone (ZoneId .systemDefault ()).toLocalDateTime ());
564
+ } else if (x instanceof LocalDateTime ) {
565
+ return PrimitiveValue .newDatetime64 (((LocalDateTime ) x ));
566
+ } else if (x instanceof LocalDate ) {
567
+ return PrimitiveValue .newDatetime64 (((LocalDate ) x ).atStartOfDay ());
568
+ } else if (x instanceof Long ) {
569
+ return PrimitiveValue .newDatetime64 (LocalDateTime .ofEpochSecond ((Long ) x , 0 , ZoneOffset .UTC ));
570
+ } else if (x instanceof Timestamp ) {
571
+ // Normalize date - use system timezone to detect correct date
572
+ Instant instant = Instant .ofEpochMilli (((Timestamp ) x ).getTime ());
573
+ LocalDateTime ldt = instant .atZone (ZoneId .systemDefault ()).toLocalDateTime ();
574
+ return PrimitiveValue .newDatetime64 (ldt );
575
+ } else if (x instanceof Date ) {
576
+ // Normalize date - use system timezone to detect correct date
577
+ Instant instant = Instant .ofEpochMilli (((Date ) x ).getTime ());
578
+ LocalDate ld = instant .atZone (ZoneId .systemDefault ()).toLocalDate ();
579
+ return PrimitiveValue .newDatetime64 (ld .atStartOfDay ());
580
+ } else if (x instanceof String ) {
581
+ try {
582
+ return PrimitiveValue .newDatetime64 (LocalDateTime .parse ((String ) x ));
583
+ } catch (DateTimeParseException e ) {
584
+ throw castNotSupported (type , x , e );
585
+ }
586
+ }
587
+ throw castNotSupported (type , x );
588
+ }
589
+
590
+ private static PrimitiveValue castToTimestamp64 (PrimitiveType type , Object x ) throws SQLException {
591
+ if (x instanceof Instant ) {
592
+ return PrimitiveValue .newTimestamp64 ((Instant ) x );
593
+ } else if (x instanceof Long ) {
594
+ return PrimitiveValue .newTimestamp64 (Instant .ofEpochMilli ((Long ) x ));
595
+ } else if (x instanceof LocalDate ) {
596
+ return PrimitiveValue .newTimestamp64 (((LocalDate ) x ).atStartOfDay ().toInstant (ZoneOffset .UTC ));
597
+ } else if (x instanceof LocalDateTime ) {
598
+ long epochSeconds = ((LocalDateTime ) x ).toEpochSecond (ZoneOffset .UTC );
599
+ return PrimitiveValue .newTimestamp64 (Instant .ofEpochSecond (epochSeconds ));
600
+ } else if (x instanceof Timestamp ) {
601
+ return PrimitiveValue .newTimestamp64 (((Timestamp ) x ).toInstant ());
602
+ } else if (x instanceof Date ) {
603
+ Instant instant = ((Date ) x ).toLocalDate ().atStartOfDay ().toInstant (ZoneOffset .UTC );
604
+ return PrimitiveValue .newTimestamp64 (instant );
605
+ } else if (x instanceof String ) {
606
+ try {
607
+ return PrimitiveValue .newTimestamp64 (Instant .parse ((String ) x ));
608
+ } catch (DateTimeParseException e ) {
609
+ throw castNotSupported (type , x , e );
610
+ }
611
+ }
612
+ throw castNotSupported (type , x );
613
+ }
614
+
505
615
private static DecimalValue validateValue (DecimalType type , DecimalValue value , Object x ) throws SQLException {
506
616
if (value .isNan ()) {
507
617
throw new SQLException (String .format (YdbConst .UNABLE_TO_CAST_TO_DECIMAL , type , toString (x ), "NaN" ));
0 commit comments