Skip to content

Commit

Permalink
Merge pull request #353 from alex268/master
Browse files Browse the repository at this point in the history
Fixed PrimitiveValue.newTimestamp method
  • Loading branch information
alex268 authored Dec 23, 2024
2 parents 9e0dd6a + 357910f commit 10219d0
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 187 deletions.
15 changes: 0 additions & 15 deletions coordination/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
15 changes: 0 additions & 15 deletions query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
32 changes: 26 additions & 6 deletions query/src/test/java/tech/ydb/query/QueryExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ public void step02_upsertTablesData() {
);

// Upsert list of series to table
retryCtx.supplyResult(session -> session.createQuery(
"UPSERT INTO series SELECT * FROM AS_TABLE($values)",
retryCtx.supplyResult(session -> session.createQuery(""
+ "DECLARE $values AS List<Struct<"
+ " series_id: Uint64,"
+ " title: Text,"
+ " release_date: Date,"
+ " series_info: Text"
+ ">>;"
+ "UPSERT INTO series SELECT * FROM AS_TABLE($values)",
TxMode.SERIALIZABLE_RW,
Params.of("$values", seriesData)
).execute()).join().getStatus().expectSuccess("upsert problem");
Expand All @@ -143,8 +149,15 @@ public void step02_upsertTablesData() {
);

// Upsert list of seasons to table
retryCtx.supplyResult(session -> session.createQuery(
"UPSERT INTO seasons SELECT * FROM AS_TABLE($values)",
retryCtx.supplyResult(session -> session.createQuery(""
+ "DECLARE $values AS List<Struct<"
+ " series_id: Uint64,"
+ " season_id: Uint64,"
+ " title: Text,"
+ " first_aired: Date,"
+ " last_aired: Date"
+ ">>;"
+ "UPSERT INTO seasons SELECT * FROM AS_TABLE($values)",
TxMode.SERIALIZABLE_RW,
Params.of("$values", seasonsData)
).execute()).join().getStatus().expectSuccess("upsert problem");
Expand All @@ -170,8 +183,15 @@ public void step02_upsertTablesData() {
);

// Upsert list of series to episodes
retryCtx.supplyResult(session -> session.createQuery(
"UPSERT INTO episodes SELECT * FROM AS_TABLE($values)",
retryCtx.supplyResult(session -> session.createQuery(""
+ "DECLARE $values AS List<Struct<"
+ " series_id: Uint64,"
+ " season_id: Uint64,"
+ " episode_id: Uint64,"
+ " title: Text,"
+ " air_date: Date"
+ ">>;"
+ "UPSERT INTO episodes SELECT * FROM AS_TABLE($values)",
TxMode.SERIALIZABLE_RW,
Params.of("$values", episodesData)
).execute()).join().getStatus().expectSuccess("upsert problem");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ public void testSimpleCRUD() {

try (QueryClient client = QueryClient.newClient(ydbTransport).build()) {
for (Entity entity: entities) {
String query = "UPSERT INTO `" + TEST_TABLE + "` (id, name, payload, is_valid) "
String query = "DECLARE $id AS Int32;"
+ "DECLARE $name AS Text;"
+ "DECLARE $payload AS Bytes;"
+ "DECLARE $is_valid AS Bool;"
+ "UPSERT INTO `" + TEST_TABLE + "` (id, name, payload, is_valid) "
+ "VALUES ($id, $name, $payload, $is_valid)";

Params params = Params.of(
Expand Down Expand Up @@ -459,6 +463,12 @@ public void testMultiStatement() {
try (QueryClient client = QueryClient.newClient(ydbTransport).build()) {
try (QuerySession session = client.createSession(Duration.ofSeconds(5)).join().getValue()) {
String query = ""
+ "DECLARE $s1 AS Int32;"
+ "DECLARE $s2 AS Int32;"
+ "DECLARE $id1 AS Int32;"
+ "DECLARE $id2 AS Int32;"
+ "DECLARE $name1 AS Text;"
+ "DECLARE $name2 AS Text;"
+ "SELECT * FROM `" + TEST_TABLE + "` WHERE id = $s1;"
+ "INSERT INTO `" + TEST_TABLE + "` (id, name) VALUES ($id1, $name1);"
+ "SELECT * FROM `" + TEST_TABLE + "` WHERE id = $s2;"
Expand Down
15 changes: 0 additions & 15 deletions table/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<YDB_DOCKER_IMAGE>cr.yandex/yc/yandex-docker-local-ydb:trunk</YDB_DOCKER_IMAGE>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
21 changes: 18 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,29 @@ public static PrimitiveValue newDatetime(LocalDateTime value) {

public static PrimitiveValue newTimestamp(long microsSinceEpoch) {
if (microsSinceEpoch < 0) {
throw new IllegalArgumentException("negative microsSinceEpoch: " + microsSinceEpoch);
throw new IllegalArgumentException("microsSinceEpoch value is before "
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + microsSinceEpoch);
}
if (microsSinceEpoch >= 4291747200000000L) {
throw new IllegalArgumentException("microsSinceEpoch value is after "
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + 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 value is before "
+ "minimum timestamp(1970-01-01 00:00:00.000000): " + value);
}
int nanos = value.getNano();
long micros = seconds * 1000000L + nanos / 1000;
if (micros >= 4291747200000000L) {
throw new IllegalArgumentException("Instant value is after "
+ "maximum timestamp(2105-12-31 23:59:59.999999): " + value
);
}
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());
}
}
56 changes: 56 additions & 0 deletions table/src/test/java/tech/ydb/table/integration/ValuesReadTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package tech.ydb.table.integration;

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

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

import tech.ydb.core.Issue;
import tech.ydb.core.Status;
import tech.ydb.core.StatusCode;
import tech.ydb.table.SessionRetryContext;
import tech.ydb.table.impl.SimpleTableClient;
import tech.ydb.table.query.DataQueryResult;
Expand Down Expand Up @@ -101,4 +105,56 @@ 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));

Status invalid = CTX.supplyResult(
s -> s.executeDataQuery("SELECT "
+ "Timestamp('1969-12-31T23:59:59.999999Z') as t6,"
+ "Timestamp('2106-01-01T00:00:00.000000Z') as t7;",
TxControl.serializableRw()
)
).join().getStatus();

Assert.assertEquals(StatusCode.GENERIC_ERROR, invalid.getCode());
Issue[] issues = invalid.getIssues();
Assert.assertEquals(2, issues.length);
Assert.assertEquals("Invalid value \"1969-12-31T23:59:59.999999Z\" for type Timestamp", issues[0].getMessage());
Assert.assertEquals("Invalid value \"2106-01-01T00:00:00.000000Z\" for type Timestamp", issues[1].getMessage());

}
}
65 changes: 0 additions & 65 deletions table/src/test/java/tech/ydb/table/types/DecimalTypeTest.java

This file was deleted.

46 changes: 0 additions & 46 deletions table/src/test/java/tech/ydb/table/types/StructTypeTest.java

This file was deleted.

Loading

0 comments on commit 10219d0

Please sign in to comment.