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