Skip to content

Commit 10219d0

Browse files
authored
Merge pull request #353 from alex268/master
Fixed PrimitiveValue.newTimestamp method
2 parents 9e0dd6a + 357910f commit 10219d0

File tree

14 files changed

+268
-187
lines changed

14 files changed

+268
-187
lines changed

coordination/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,4 @@
4141
<scope>test</scope>
4242
</dependency>
4343
</dependencies>
44-
45-
<build>
46-
<plugins>
47-
<plugin>
48-
<groupId>org.apache.maven.plugins</groupId>
49-
<artifactId>maven-surefire-plugin</artifactId>
50-
<configuration>
51-
<environmentVariables>
52-
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
53-
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
54-
</environmentVariables>
55-
</configuration>
56-
</plugin>
57-
</plugins>
58-
</build>
5944
</project>

query/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,4 @@
3636
<scope>test</scope>
3737
</dependency>
3838
</dependencies>
39-
40-
<build>
41-
<plugins>
42-
<plugin>
43-
<groupId>org.apache.maven.plugins</groupId>
44-
<artifactId>maven-surefire-plugin</artifactId>
45-
<configuration>
46-
<environmentVariables>
47-
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
48-
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
49-
</environmentVariables>
50-
</configuration>
51-
</plugin>
52-
</plugins>
53-
</build>
5439
</project>

query/src/test/java/tech/ydb/query/QueryExampleTest.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ public void step02_upsertTablesData() {
116116
);
117117

118118
// Upsert list of series to table
119-
retryCtx.supplyResult(session -> session.createQuery(
120-
"UPSERT INTO series SELECT * FROM AS_TABLE($values)",
119+
retryCtx.supplyResult(session -> session.createQuery(""
120+
+ "DECLARE $values AS List<Struct<"
121+
+ " series_id: Uint64,"
122+
+ " title: Text,"
123+
+ " release_date: Date,"
124+
+ " series_info: Text"
125+
+ ">>;"
126+
+ "UPSERT INTO series SELECT * FROM AS_TABLE($values)",
121127
TxMode.SERIALIZABLE_RW,
122128
Params.of("$values", seriesData)
123129
).execute()).join().getStatus().expectSuccess("upsert problem");
@@ -143,8 +149,15 @@ public void step02_upsertTablesData() {
143149
);
144150

145151
// Upsert list of seasons to table
146-
retryCtx.supplyResult(session -> session.createQuery(
147-
"UPSERT INTO seasons SELECT * FROM AS_TABLE($values)",
152+
retryCtx.supplyResult(session -> session.createQuery(""
153+
+ "DECLARE $values AS List<Struct<"
154+
+ " series_id: Uint64,"
155+
+ " season_id: Uint64,"
156+
+ " title: Text,"
157+
+ " first_aired: Date,"
158+
+ " last_aired: Date"
159+
+ ">>;"
160+
+ "UPSERT INTO seasons SELECT * FROM AS_TABLE($values)",
148161
TxMode.SERIALIZABLE_RW,
149162
Params.of("$values", seasonsData)
150163
).execute()).join().getStatus().expectSuccess("upsert problem");
@@ -170,8 +183,15 @@ public void step02_upsertTablesData() {
170183
);
171184

172185
// Upsert list of series to episodes
173-
retryCtx.supplyResult(session -> session.createQuery(
174-
"UPSERT INTO episodes SELECT * FROM AS_TABLE($values)",
186+
retryCtx.supplyResult(session -> session.createQuery(""
187+
+ "DECLARE $values AS List<Struct<"
188+
+ " series_id: Uint64,"
189+
+ " season_id: Uint64,"
190+
+ " episode_id: Uint64,"
191+
+ " title: Text,"
192+
+ " air_date: Date"
193+
+ ">>;"
194+
+ "UPSERT INTO episodes SELECT * FROM AS_TABLE($values)",
175195
TxMode.SERIALIZABLE_RW,
176196
Params.of("$values", episodesData)
177197
).execute()).join().getStatus().expectSuccess("upsert problem");

query/src/test/java/tech/ydb/query/impl/QueryIntegrationTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,11 @@ public void testSimpleCRUD() {
252252

253253
try (QueryClient client = QueryClient.newClient(ydbTransport).build()) {
254254
for (Entity entity: entities) {
255-
String query = "UPSERT INTO `" + TEST_TABLE + "` (id, name, payload, is_valid) "
255+
String query = "DECLARE $id AS Int32;"
256+
+ "DECLARE $name AS Text;"
257+
+ "DECLARE $payload AS Bytes;"
258+
+ "DECLARE $is_valid AS Bool;"
259+
+ "UPSERT INTO `" + TEST_TABLE + "` (id, name, payload, is_valid) "
256260
+ "VALUES ($id, $name, $payload, $is_valid)";
257261

258262
Params params = Params.of(
@@ -459,6 +463,12 @@ public void testMultiStatement() {
459463
try (QueryClient client = QueryClient.newClient(ydbTransport).build()) {
460464
try (QuerySession session = client.createSession(Duration.ofSeconds(5)).join().getValue()) {
461465
String query = ""
466+
+ "DECLARE $s1 AS Int32;"
467+
+ "DECLARE $s2 AS Int32;"
468+
+ "DECLARE $id1 AS Int32;"
469+
+ "DECLARE $id2 AS Int32;"
470+
+ "DECLARE $name1 AS Text;"
471+
+ "DECLARE $name2 AS Text;"
462472
+ "SELECT * FROM `" + TEST_TABLE + "` WHERE id = $s1;"
463473
+ "INSERT INTO `" + TEST_TABLE + "` (id, name) VALUES ($id1, $name1);"
464474
+ "SELECT * FROM `" + TEST_TABLE + "` WHERE id = $s2;"

table/pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,4 @@
4141
<scope>test</scope>
4242
</dependency>
4343
</dependencies>
44-
45-
<build>
46-
<plugins>
47-
<plugin>
48-
<groupId>org.apache.maven.plugins</groupId>
49-
<artifactId>maven-surefire-plugin</artifactId>
50-
<configuration>
51-
<environmentVariables>
52-
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
53-
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
54-
</environmentVariables>
55-
</configuration>
56-
</plugin>
57-
</plugins>
58-
</build>
5944
</project>

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,29 @@ 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("microsSinceEpoch value is before "
295+
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + microsSinceEpoch);
296+
}
297+
if (microsSinceEpoch >= 4291747200000000L) {
298+
throw new IllegalArgumentException("microsSinceEpoch value is after "
299+
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + microsSinceEpoch);
295300
}
296301
return new InstantValue(PrimitiveType.Timestamp, microsSinceEpoch);
297302
}
298303

299304
public static PrimitiveValue newTimestamp(Instant value) {
300-
long micros = TimeUnit.SECONDS.toMicros(value.getEpochSecond()) +
301-
TimeUnit.NANOSECONDS.toMicros(value.getNano());
305+
long seconds = value.getEpochSecond();
306+
if (seconds < 0) {
307+
throw new IllegalArgumentException("Instant value is before "
308+
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + value);
309+
}
310+
int nanos = value.getNano();
311+
long micros = seconds * 1000000L + nanos / 1000;
312+
if (micros >= 4291747200000000L) {
313+
throw new IllegalArgumentException("Instant value is after "
314+
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + value
315+
);
316+
}
302317
return new InstantValue(PrimitiveType.Timestamp, micros);
303318
}
304319

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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package tech.ydb.table.integration;
22

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

56
import org.junit.Assert;
67
import org.junit.ClassRule;
78
import org.junit.Test;
89

10+
import tech.ydb.core.Issue;
11+
import tech.ydb.core.Status;
12+
import tech.ydb.core.StatusCode;
913
import tech.ydb.table.SessionRetryContext;
1014
import tech.ydb.table.impl.SimpleTableClient;
1115
import tech.ydb.table.query.DataQueryResult;
@@ -101,4 +105,56 @@ public void uuidReadTest() {
101105
Assert.assertEquals(0x9cfbb7462d9e498bL, v2.getUuidLow());
102106
Assert.assertEquals(0x6e73b41c4ede4d08L, v2.getUuidHigh());
103107
}
108+
109+
private void assertTimestamp(ValueReader vr, boolean optional, Instant expected) {
110+
Assert.assertNotNull(vr);
111+
if (optional) {
112+
Assert.assertSame(Type.Kind.OPTIONAL, vr.getType().getKind());
113+
Assert.assertSame(PrimitiveType.Timestamp, vr.getType().unwrapOptional());
114+
} else {
115+
Assert.assertSame(PrimitiveType.Timestamp, vr.getType());
116+
}
117+
118+
Assert.assertEquals(expected, vr.getTimestamp());
119+
}
120+
121+
@Test
122+
public void timestampReadTest() {
123+
DataQueryResult result = CTX.supplyResult(
124+
s -> s.executeDataQuery("SELECT "
125+
+ "DateTime::MakeTimestamp(DateTime::FromMilliseconds(0ul)) as t1,"
126+
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(1000ul)) as t2,"
127+
+ "DateTime::MakeTimestamp(DateTime::FromMicroseconds(4291747199999999ul)) as t3,"
128+
+ "Timestamp('1970-01-01T00:00:00.000000Z') as t4,"
129+
+ "Timestamp('2105-12-31T23:59:59.999999Z') as t5;",
130+
TxControl.serializableRw()
131+
)
132+
).join().getValue();
133+
134+
Assert.assertEquals(1, result.getResultSetCount());
135+
136+
ResultSetReader rs = result.getResultSet(0);
137+
Assert.assertTrue(rs.next());
138+
139+
assertTimestamp(rs.getColumn("t1"), true, Instant.EPOCH);
140+
assertTimestamp(rs.getColumn("t2"), true, Instant.EPOCH.plusMillis(1));
141+
assertTimestamp(rs.getColumn("t3"), true, Instant.parse("2105-12-31T23:59:59.999999Z"));
142+
assertTimestamp(rs.getColumn("t4"), false, Instant.ofEpochSecond(0, 0));
143+
assertTimestamp(rs.getColumn("t5"), false, Instant.ofEpochSecond(4291747199l, 999999000l));
144+
145+
Status invalid = CTX.supplyResult(
146+
s -> s.executeDataQuery("SELECT "
147+
+ "Timestamp('1969-12-31T23:59:59.999999Z') as t6,"
148+
+ "Timestamp('2106-01-01T00:00:00.000000Z') as t7;",
149+
TxControl.serializableRw()
150+
)
151+
).join().getStatus();
152+
153+
Assert.assertEquals(StatusCode.GENERIC_ERROR, invalid.getCode());
154+
Issue[] issues = invalid.getIssues();
155+
Assert.assertEquals(2, issues.length);
156+
Assert.assertEquals("Invalid value \"1969-12-31T23:59:59.999999Z\" for type Timestamp", issues[0].getMessage());
157+
Assert.assertEquals("Invalid value \"2106-01-01T00:00:00.000000Z\" for type Timestamp", issues[1].getMessage());
158+
159+
}
104160
}

table/src/test/java/tech/ydb/table/types/DecimalTypeTest.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

table/src/test/java/tech/ydb/table/types/StructTypeTest.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)