Skip to content

Commit e86fccb

Browse files
committed
copy of changes made in ydb-platform#353
1 parent 9e0dd6a commit e86fccb

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

table/src/main/java/tech/ydb/table/values/PrimitiveValue.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,18 @@ public static PrimitiveValue newDatetime(LocalDateTime value) {
291291

292292
public static PrimitiveValue newTimestamp(long microsSinceEpoch) {
293293
if (microsSinceEpoch < 0) {
294-
throw new IllegalArgumentException("negative microsSinceEpoch: " + microsSinceEpoch);
294+
throw new IllegalArgumentException("Negative microsSinceEpoch: " + microsSinceEpoch);
295295
}
296296
return new InstantValue(PrimitiveType.Timestamp, microsSinceEpoch);
297297
}
298298

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

table/src/test/java/tech/ydb/table/integration/TtlTableTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,33 @@ public void alterTableTest() {
112112
Assert.assertEquals(TableTtl.TtlUnit.UNSPECIFIED, ttl.getTtlUnit());
113113
Assert.assertNull(ttl.getRunIntervaelSeconds());
114114
}
115+
116+
@Test
117+
public void noTtlTableTest() {
118+
// --------------------- create table -----------------------------
119+
TableDescription createTableDesc = TableDescription.newBuilder()
120+
.addNonnullColumn("id", PrimitiveType.Uint64)
121+
.addNullableColumn("date", PrimitiveType.Datetime)
122+
.addNullableColumn("value", PrimitiveType.Uint64)
123+
.setPrimaryKey("id")
124+
.build();
125+
126+
Status createStatus = ctx.supplyStatus(
127+
session -> session.createTable(tablePath, createTableDesc, new CreateTableSettings())
128+
).join();
129+
Assert.assertTrue("Create table ttl " + createStatus, createStatus.isSuccess());
130+
131+
// --------------------- describe table after creating -----------------------------
132+
Result<TableDescription> describeResult = ctx.supplyResult(session ->session.describeTable(tablePath)).join();
133+
Assert.assertTrue("Describe table with ttl " + describeResult.getStatus(), describeResult.isSuccess());
134+
135+
TableTtl ttl = describeResult.getValue().getTableTtl();
136+
137+
Assert.assertNotNull(ttl);
138+
Assert.assertEquals(TableTtl.TtlMode.NOT_SET, ttl.getTtlMode());
139+
Assert.assertEquals("", ttl.getDateTimeColumn());
140+
Assert.assertEquals(Integer.valueOf(0), ttl.getExpireAfterSeconds());
141+
Assert.assertEquals(TableTtl.TtlUnit.UNSPECIFIED, ttl.getTtlUnit());
142+
Assert.assertNull(ttl.getRunIntervaelSeconds());
143+
}
115144
}

table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.ydb.table.integration;
22

3+
import java.time.Instant;
34
import java.util.UUID;
45

56
import org.junit.Assert;
@@ -101,4 +102,41 @@ public void uuidReadTest() {
101102
Assert.assertEquals(0x9cfbb7462d9e498bL, v2.getUuidLow());
102103
Assert.assertEquals(0x6e73b41c4ede4d08L, v2.getUuidHigh());
103104
}
105+
106+
private void assertTimestamp(ValueReader vr, boolean optional, Instant expected) {
107+
Assert.assertNotNull(vr);
108+
if (optional) {
109+
Assert.assertSame(Type.Kind.OPTIONAL, vr.getType().getKind());
110+
Assert.assertSame(PrimitiveType.Timestamp, vr.getType().unwrapOptional());
111+
} else {
112+
Assert.assertSame(PrimitiveType.Timestamp, vr.getType());
113+
}
114+
115+
Assert.assertEquals(expected, vr.getTimestamp());
116+
}
117+
118+
@Test
119+
public void timestampReadTest() {
120+
DataQueryResult result = CTX.supplyResult(
121+
s -> s.executeDataQuery("SELECT "
122+
+ "DateTime::MakeTimestamp(DateTime::FromMilliseconds(0ul)) as t1,"
123+
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(1000ul)) as t2,"
124+
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(4291747199999999ul)) as t3,"
125+
+ "Timestamp('1970-01-01T00:00:00.000000Z') as t4,"
126+
+ "Timestamp('2105-12-31T23:59:59.999999Z') as t5;",
127+
TxControl.serializableRw()
128+
)
129+
).join().getValue();
130+
131+
Assert.assertEquals(1, result.getResultSetCount());
132+
133+
ResultSetReader rs = result.getResultSet(0);
134+
Assert.assertTrue(rs.next());
135+
136+
assertTimestamp(rs.getColumn("t1"), true, Instant.EPOCH);
137+
assertTimestamp(rs.getColumn("t2"), true, Instant.EPOCH.plusMillis(1));
138+
assertTimestamp(rs.getColumn("t3"), true, Instant.parse("2105-12-31T23:59:59.999999Z"));
139+
assertTimestamp(rs.getColumn("t4"), false, Instant.ofEpochSecond(0, 0));
140+
assertTimestamp(rs.getColumn("t5"), false, Instant.ofEpochSecond(4291747199l, 999999000l));
141+
}
104142
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tech.ydb.table.types;
2+
3+
import java.time.Instant;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import tech.ydb.proto.ValueProtos.Value;
9+
import tech.ydb.table.values.PrimitiveValue;
10+
11+
/**
12+
*
13+
* @author Aleksandr Gorshenin
14+
*/
15+
public class PrimitiveTypeTest {
16+
17+
@Test
18+
public void timestampTest() {
19+
PrimitiveValue min = PrimitiveValue.newTimestamp(Instant.EPOCH);
20+
Assert.assertEquals(min, PrimitiveValue.newTimestamp(0));
21+
Value minValue = min.toPb();
22+
23+
Assert.assertEquals(0, minValue.getUint32Value());
24+
Assert.assertEquals(0, minValue.getUint64Value());
25+
Assert.assertEquals(0, minValue.getInt32Value());
26+
Assert.assertEquals(0, minValue.getInt64Value());
27+
Assert.assertEquals(0, minValue.getLow128());
28+
Assert.assertEquals(0, minValue.getHigh128());
29+
30+
PrimitiveValue max = PrimitiveValue.newTimestamp(Instant.parse("2105-12-31T23:59:59.999999Z"));
31+
Assert.assertEquals(max, PrimitiveValue.newTimestamp(4291747199999999l));
32+
Value maxValue = max.toPb();
33+
34+
Assert.assertEquals(0, maxValue.getUint32Value());
35+
Assert.assertEquals(4291747199999999l, maxValue.getUint64Value());
36+
Assert.assertEquals(0, maxValue.getInt32Value());
37+
Assert.assertEquals(0, maxValue.getInt64Value());
38+
Assert.assertEquals(0, maxValue.getLow128());
39+
Assert.assertEquals(0, maxValue.getHigh128());
40+
41+
IllegalArgumentException err1 = Assert.assertThrows(
42+
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(-1)
43+
);
44+
Assert.assertEquals("Negative microsSinceEpoch: -1", err1.getMessage());
45+
46+
IllegalArgumentException err2 = Assert.assertThrows(
47+
IllegalArgumentException.class, () -> PrimitiveValue.newTimestamp(Instant.EPOCH.minusNanos(1))
48+
);
49+
Assert.assertEquals("Instant before epoch: 1969-12-31T23:59:59.999999999Z", err2.getMessage());
50+
}
51+
}

0 commit comments

Comments
 (0)