Skip to content

Commit

Permalink
copy of changes made in ydb-platform#353
Browse files Browse the repository at this point in the history
  • Loading branch information
zinal committed Dec 22, 2024
1 parent 9e0dd6a commit e86fccb
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
10 changes: 7 additions & 3 deletions table/src/main/java/tech/ydb/table/values/PrimitiveValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,18 @@ public static PrimitiveValue newDatetime(LocalDateTime value) {

public static PrimitiveValue newTimestamp(long microsSinceEpoch) {
if (microsSinceEpoch < 0) {
throw new IllegalArgumentException("negative microsSinceEpoch: " + microsSinceEpoch);
throw new IllegalArgumentException("Negative microsSinceEpoch: " + microsSinceEpoch);
}
return new InstantValue(PrimitiveType.Timestamp, microsSinceEpoch);
}

public static PrimitiveValue newTimestamp(Instant value) {
long micros = TimeUnit.SECONDS.toMicros(value.getEpochSecond()) +
TimeUnit.NANOSECONDS.toMicros(value.getNano());
long seconds = value.getEpochSecond();
if (seconds < 0) {
throw new IllegalArgumentException("Instant before epoch: " + value);
}
int nanos = value.getNano();
long micros = seconds * 1000000L + nanos / 1000;
return new InstantValue(PrimitiveType.Timestamp, micros);
}

Expand Down
29 changes: 29 additions & 0 deletions table/src/test/java/tech/ydb/table/integration/TtlTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,33 @@ public void alterTableTest() {
Assert.assertEquals(TableTtl.TtlUnit.UNSPECIFIED, ttl.getTtlUnit());
Assert.assertNull(ttl.getRunIntervaelSeconds());
}

@Test
public void noTtlTableTest() {
// --------------------- create table -----------------------------
TableDescription createTableDesc = TableDescription.newBuilder()
.addNonnullColumn("id", PrimitiveType.Uint64)
.addNullableColumn("date", PrimitiveType.Datetime)
.addNullableColumn("value", PrimitiveType.Uint64)
.setPrimaryKey("id")
.build();

Status createStatus = ctx.supplyStatus(
session -> session.createTable(tablePath, createTableDesc, new CreateTableSettings())
).join();
Assert.assertTrue("Create table ttl " + createStatus, createStatus.isSuccess());

// --------------------- describe table after creating -----------------------------
Result<TableDescription> describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
Assert.assertTrue("Describe table with ttl " + describeResult.getStatus(), describeResult.isSuccess());

TableTtl ttl = describeResult.getValue().getTableTtl();

Assert.assertNotNull(ttl);
Assert.assertEquals(TableTtl.TtlMode.NOT_SET, ttl.getTtlMode());
Assert.assertEquals("", ttl.getDateTimeColumn());
Assert.assertEquals(Integer.valueOf(0), ttl.getExpireAfterSeconds());
Assert.assertEquals(TableTtl.TtlUnit.UNSPECIFIED, ttl.getTtlUnit());
Assert.assertNull(ttl.getRunIntervaelSeconds());
}
}
38 changes: 38 additions & 0 deletions table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.table.integration;

import java.time.Instant;
import java.util.UUID;

import org.junit.Assert;
Expand Down Expand Up @@ -101,4 +102,41 @@ public void uuidReadTest() {
Assert.assertEquals(0x9cfbb7462d9e498bL, v2.getUuidLow());
Assert.assertEquals(0x6e73b41c4ede4d08L, v2.getUuidHigh());
}

private void assertTimestamp(ValueReader vr, boolean optional, Instant expected) {
Assert.assertNotNull(vr);
if (optional) {
Assert.assertSame(Type.Kind.OPTIONAL, vr.getType().getKind());
Assert.assertSame(PrimitiveType.Timestamp, vr.getType().unwrapOptional());
} else {
Assert.assertSame(PrimitiveType.Timestamp, vr.getType());
}

Assert.assertEquals(expected, vr.getTimestamp());
}

@Test
public void timestampReadTest() {
DataQueryResult result = CTX.supplyResult(
s -> s.executeDataQuery("SELECT "
+ "DateTime::MakeTimestamp(DateTime::FromMilliseconds(0ul)) as t1,"
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(1000ul)) as t2,"
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(4291747199999999ul)) as t3,"
+ "Timestamp('1970-01-01T00:00:00.000000Z') as t4,"
+ "Timestamp('2105-12-31T23:59:59.999999Z') as t5;",
TxControl.serializableRw()
)
).join().getValue();

Assert.assertEquals(1, result.getResultSetCount());

ResultSetReader rs = result.getResultSet(0);
Assert.assertTrue(rs.next());

assertTimestamp(rs.getColumn("t1"), true, Instant.EPOCH);
assertTimestamp(rs.getColumn("t2"), true, Instant.EPOCH.plusMillis(1));
assertTimestamp(rs.getColumn("t3"), true, Instant.parse("2105-12-31T23:59:59.999999Z"));
assertTimestamp(rs.getColumn("t4"), false, Instant.ofEpochSecond(0, 0));
assertTimestamp(rs.getColumn("t5"), false, Instant.ofEpochSecond(4291747199l, 999999000l));
}
}
51 changes: 51 additions & 0 deletions table/src/test/java/tech/ydb/table/types/PrimitiveTypeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package tech.ydb.table.types;

import java.time.Instant;

import org.junit.Assert;
import org.junit.Test;

import tech.ydb.proto.ValueProtos.Value;
import tech.ydb.table.values.PrimitiveValue;

/**
*
* @author Aleksandr Gorshenin
*/
public class PrimitiveTypeTest {

@Test
public void timestampTest() {
PrimitiveValue min = PrimitiveValue.newTimestamp(Instant.EPOCH);
Assert.assertEquals(min, PrimitiveValue.newTimestamp(0));
Value minValue = min.toPb();

Assert.assertEquals(0, minValue.getUint32Value());
Assert.assertEquals(0, minValue.getUint64Value());
Assert.assertEquals(0, minValue.getInt32Value());
Assert.assertEquals(0, minValue.getInt64Value());
Assert.assertEquals(0, minValue.getLow128());
Assert.assertEquals(0, minValue.getHigh128());

PrimitiveValue max = PrimitiveValue.newTimestamp(Instant.parse("2105-12-31T23:59:59.999999Z"));
Assert.assertEquals(max, PrimitiveValue.newTimestamp(4291747199999999l));
Value maxValue = max.toPb();

Assert.assertEquals(0, maxValue.getUint32Value());
Assert.assertEquals(4291747199999999l, maxValue.getUint64Value());
Assert.assertEquals(0, maxValue.getInt32Value());
Assert.assertEquals(0, maxValue.getInt64Value());
Assert.assertEquals(0, maxValue.getLow128());
Assert.assertEquals(0, maxValue.getHigh128());

IllegalArgumentException err1 = Assert.assertThrows(
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(-1)
);
Assert.assertEquals("Negative microsSinceEpoch: -1", err1.getMessage());

IllegalArgumentException err2 = Assert.assertThrows(
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(Instant.EPOCH.minusNanos(1))
);
Assert.assertEquals("Instant before epoch: 1969-12-31T23:59:59.999999999Z", err2.getMessage());
}
}

0 comments on commit e86fccb

Please sign in to comment.