Skip to content

Commit

Permalink
rebase branch
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Jan 17, 2025
1 parent b437917 commit 7abfedc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 24 deletions.
16 changes: 16 additions & 0 deletions query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,20 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
<YDB_DOCKER_IMAGE>ydbplatform/local-ydb:trunk</YDB_DOCKER_IMAGE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
97 changes: 77 additions & 20 deletions table/src/main/java/tech/ydb/table/values/PrimitiveValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ public static PrimitiveValue newTimestamp(Instant value) {
}

public static PrimitiveValue newTimestamp64(long microsSinceEpoch) {
return new InstantValue(PrimitiveType.Timestamp, microsSinceEpoch);
return new InstantValue(PrimitiveType.Timestamp64, microsSinceEpoch);
}

public static PrimitiveValue newTimestamp64(Instant value) {
long micros = TimeUnit.SECONDS.toMicros(value.getEpochSecond()) +
TimeUnit.NANOSECONDS.toMicros(value.getNano());
return new InstantValue(PrimitiveType.Timestamp, micros);
return new InstantValue(PrimitiveType.Timestamp64, micros);
}

public static PrimitiveValue newInterval(long micros) {
Expand All @@ -380,7 +380,7 @@ public static PrimitiveValue newInterval64(long micros) {
}

public static PrimitiveValue newInterval64(Duration value) {
return newInterval(TimeUnit.NANOSECONDS.toMicros(value.toNanos()));
return newInterval64(TimeUnit.NANOSECONDS.toMicros(value.toNanos()));
}

public static PrimitiveValue newTzDate(ZonedDateTime dateTime) {
Expand Down Expand Up @@ -1018,8 +1018,8 @@ public int hashCode() {
@Override
public String toString() {
final int length = (value instanceof byte[])
? ((byte[]) value).length
: ((ByteString) value).size();
? ((byte[]) value).length
: ((ByteString) value).size();

if (length == 0) {
return "\"\"";
Expand Down Expand Up @@ -1094,9 +1094,9 @@ private static final class Text extends PrimitiveValue {
private static final Text EMPTY_JSON_DOCUMENT = new Text(PrimitiveType.JsonDocument, "");

private static final Escaper ESCAPER = Escapers.builder()
.addEscape('\\', "\\\\")
.addEscape('\"', "\\\"")
.build();
.addEscape('\\', "\\\\")
.addEscape('\"', "\\\"")
.build();

private final PrimitiveType type;
private final String value;
Expand Down Expand Up @@ -1198,6 +1198,24 @@ public Instant getTimestamp() {
return ProtoValue.toTimestamp(microsSinceEpoch);
}

@Override
public LocalDate getDate32() {
checkType(PrimitiveType.Date32, type);
return ProtoValue.toDate32(TimeUnit.MICROSECONDS.toDays(microsSinceEpoch));
}

@Override
public LocalDateTime getDatetime64() {
checkType(PrimitiveType.Datetime64, type);
return ProtoValue.toDatetime64(TimeUnit.MICROSECONDS.toSeconds(microsSinceEpoch));
}

@Override
public Instant getTimestamp64() {
checkType(PrimitiveType.Timestamp64, type);
return ProtoValue.toTimestamp64(microsSinceEpoch);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -1220,9 +1238,18 @@ public int hashCode() {
@Override
public String toString() {
switch (type) {
case Date: return DateTimeFormatter.ISO_DATE.format(getDate());
case Datetime: return DateTimeFormatter.ISO_DATE_TIME.format(getDatetime());
case Timestamp: return DateTimeFormatter.ISO_INSTANT.format(getTimestamp());
case Date:
return DateTimeFormatter.ISO_DATE.format(getDate());
case Datetime:
return DateTimeFormatter.ISO_DATE_TIME.format(getDatetime());
case Timestamp:
return DateTimeFormatter.ISO_INSTANT.format(getTimestamp());
case Date32:
return DateTimeFormatter.ISO_DATE.format(getDate32());
case Datetime64:
return DateTimeFormatter.ISO_DATE_TIME.format(getDatetime64());
case Timestamp64:
return DateTimeFormatter.ISO_INSTANT.format(getTimestamp64());
default:
throw new IllegalStateException("unsupported type: " + type);
}
Expand All @@ -1231,32 +1258,48 @@ public String toString() {
@Override
public ValueProtos.Value toPb() {
switch (type) {
case Date: return ProtoValue.fromDate(TimeUnit.MICROSECONDS.toDays(microsSinceEpoch));
case Datetime: return ProtoValue.fromDatetime(TimeUnit.MICROSECONDS.toSeconds(microsSinceEpoch));
case Timestamp: return ProtoValue.fromTimestamp(microsSinceEpoch);
case Date:
return ProtoValue.fromDate(TimeUnit.MICROSECONDS.toDays(microsSinceEpoch));
case Datetime:
return ProtoValue.fromDatetime(TimeUnit.MICROSECONDS.toSeconds(microsSinceEpoch));
case Timestamp:
return ProtoValue.fromTimestamp(microsSinceEpoch);
case Date32:
return ProtoValue.fromDate32(TimeUnit.MICROSECONDS.toDays(microsSinceEpoch));
case Datetime64:
return ProtoValue.fromDatetime64(TimeUnit.MICROSECONDS.toSeconds(microsSinceEpoch));
case Timestamp64:
return ProtoValue.fromTimestamp64(microsSinceEpoch);
default:
throw new IllegalStateException("unsupported type: " + type);
}
}
}

private static final class IntervalValue extends PrimitiveValue {
private final PrimitiveType type;
private final long micros;

IntervalValue(long micros) {
IntervalValue(PrimitiveType type, long micros) {
this.type = type;
this.micros = micros;
}

@Override
public PrimitiveType getType() {
return PrimitiveType.Interval;
return type;
}

@Override
public Duration getInterval() {
return Duration.of(micros, ChronoUnit.MICROS);
}

@Override
public Duration getInterval64() {
return Duration.of(micros, ChronoUnit.MICROS);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -1278,12 +1321,26 @@ public int hashCode() {

@Override
public String toString() {
return getInterval().toString();
switch (type) {
case Interval:
return getInterval().toString();
case Interval64:
return getInterval64().toString();
default:
throw new IllegalStateException("unsupported type: " + type);
}
}

@Override
public ValueProtos.Value toPb() {
return ProtoValue.fromInterval(micros);
switch (type) {
case Interval:
return ProtoValue.fromInterval(micros);
case Interval64:
return ProtoValue.fromInterval64(micros);
default:
throw new IllegalStateException("unsupported type: " + type);
}
}
}

Expand Down Expand Up @@ -1343,8 +1400,8 @@ public int hashCode() {
@Override
public String toString() {
String timeStr = (type == PrimitiveType.TzDate)
? dateTime.toLocalDate().toString()
: dateTime.toLocalDateTime().toString();
? dateTime.toLocalDate().toString()
: dateTime.toLocalDateTime().toString();

return timeStr + ',' + dateTime.getZone().getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,15 @@ public static LocalDate toDate(ValueProtos.Value value) {
}

public static ValueProtos.Value fromDate32(long daysSinceEpoch) {
int daysInt = (int) (daysSinceEpoch & 0xffffffffL);

return ValueProtos.Value.newBuilder().setInt32Value(daysInt).build();
return ValueProtos.Value.newBuilder().setInt32Value((int)daysSinceEpoch).build();
}

public static LocalDate toDate32(long daysSinceEpoch) {
return LocalDate.ofEpochDay(daysSinceEpoch);
}

public static LocalDate toDate32(ValueProtos.Value value) {
return toDate(value.getInt32Value());
return toDate32(value.getInt32Value());
}

// - datetime -
Expand Down

0 comments on commit 7abfedc

Please sign in to comment.