Skip to content

Commit de57c20

Browse files
committed
fix: NANOS_PER_MICROSECONDS had wrong value
1 parent 9e35f57 commit de57c20

File tree

5 files changed

+79
-16
lines changed

5 files changed

+79
-16
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Interval.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public class Interval implements Serializable {
4848
public static final long MICROS_PER_SECOND = MICROS_PER_MILLI * MILLIS_PER_SECOND;
4949
public static final long MICROS_PER_MINUTE = SECONDS_PER_MINUTE * MICROS_PER_SECOND;
5050
public static final long MICROS_PER_HOUR = SECONDS_PER_HOUR * MICROS_PER_SECOND;
51-
public static final BigInteger NANOS_PER_MICROSECOND =
52-
BigInteger.valueOf(MICROS_PER_SECOND * NANOS_PER_MICRO);
51+
public static final BigInteger NANOS_PER_MICROSECOND = BigInteger.valueOf(NANOS_PER_MICRO);
5352
public static final BigInteger NANOS_PER_MILLISECOND =
5453
BigInteger.valueOf(MILLIS_PER_SECOND * NANOS_PER_MICRO);
5554
public static final BigInteger NANOS_PER_SECOND =

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4708,6 +4708,7 @@ public void testGetAllTypesAsString() {
47084708
col++);
47094709
assertAsString("2023-01-11", resultSet, col++);
47104710
assertAsString("2023-01-11T11:55:18.123456789Z", resultSet, col++);
4711+
assertAsString("P-6Y-8M-198DT23H59M12.345678900S", resultSet, col++);
47114712
if (dialect == Dialect.POSTGRESQL) {
47124713
// Check PG_OID value
47134714
assertAsString("100", resultSet, col++);
@@ -4752,6 +4753,8 @@ public void testGetAllTypesAsString() {
47524753
ImmutableList.of("2023-01-11T11:55:18.123456789Z", "NULL", "2023-01-12T11:55:18Z"),
47534754
resultSet,
47544755
col++);
4756+
assertAsString(
4757+
ImmutableList.of("P1Y2M3DT-1H-2M-3.456789S", "NULL", "P1Y"), resultSet, col++);
47554758
if (dialect == Dialect.GOOGLE_STANDARD_SQL) {
47564759
assertAsString(Base64.getEncoder().encodeToString(info.toByteArray()), resultSet, col++);
47574760
assertAsString(String.valueOf(Genre.JAZZ_VALUE), resultSet, col++);
@@ -5285,6 +5288,10 @@ private ListValue getRows(Dialect dialect) {
52855288
.addValues(
52865289
com.google.protobuf.Value.newBuilder()
52875290
.setStringValue("2023-01-11T11:55:18.123456789Z")
5291+
.build())
5292+
.addValues(
5293+
com.google.protobuf.Value.newBuilder()
5294+
.setStringValue("P-7Y4M-198DT24H-1M12.3456789S")
52885295
.build());
52895296
if (dialect == Dialect.POSTGRESQL) {
52905297
// Add PG_OID value
@@ -5462,6 +5469,21 @@ private ListValue getRows(Dialect dialect) {
54625469
com.google.protobuf.Value.newBuilder()
54635470
.setStringValue("2023-01-12T11:55:18Z")
54645471
.build())
5472+
.build()))
5473+
.addValues(
5474+
com.google.protobuf.Value.newBuilder()
5475+
.setListValue(
5476+
ListValue.newBuilder()
5477+
.addValues(
5478+
com.google.protobuf.Value.newBuilder()
5479+
.setStringValue("P1Y2M3DT-1H-2M-3.456789S")
5480+
.build())
5481+
.addValues(
5482+
com.google.protobuf.Value.newBuilder()
5483+
.setNullValue(NullValue.NULL_VALUE)
5484+
.build())
5485+
.addValues(
5486+
com.google.protobuf.Value.newBuilder().setStringValue("P1Y").build())
54655487
.build()));
54665488

54675489
if (dialect == Dialect.GOOGLE_STANDARD_SQL) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/IntervalTest.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,28 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import static com.google.cloud.spanner.Interval.MICROS_PER_HOUR;
20+
import static com.google.cloud.spanner.Interval.MICROS_PER_MILLI;
21+
import static com.google.cloud.spanner.Interval.MICROS_PER_MINUTE;
22+
import static com.google.cloud.spanner.Interval.MICROS_PER_SECOND;
23+
import static com.google.cloud.spanner.Interval.MILLIS_PER_SECOND;
24+
import static com.google.cloud.spanner.Interval.MINUTES_PER_HOUR;
25+
import static com.google.cloud.spanner.Interval.NANOS_PER_HOUR;
26+
import static com.google.cloud.spanner.Interval.NANOS_PER_MICRO;
27+
import static com.google.cloud.spanner.Interval.NANOS_PER_MICROSECOND;
28+
import static com.google.cloud.spanner.Interval.NANOS_PER_MILLISECOND;
29+
import static com.google.cloud.spanner.Interval.NANOS_PER_MINUTE;
30+
import static com.google.cloud.spanner.Interval.NANOS_PER_SECOND;
31+
import static com.google.cloud.spanner.Interval.SECONDS_PER_HOUR;
32+
import static com.google.cloud.spanner.Interval.SECONDS_PER_MINUTE;
1933
import static org.junit.Assert.assertEquals;
2034
import static org.junit.Assert.assertNotEquals;
2135
import static org.junit.Assert.assertThrows;
2236

2337
import java.math.BigInteger;
38+
import java.time.Duration;
39+
import java.time.temporal.ChronoUnit;
40+
import java.util.concurrent.TimeUnit;
2441
import org.junit.Test;
2542
import org.junit.runner.RunWith;
2643
import org.junit.runners.JUnit4;
@@ -50,17 +67,15 @@ public void testOfSeconds() {
5067
Interval interval = Interval.ofSeconds(10);
5168
assertEquals(0, interval.getMonths());
5269
assertEquals(0, interval.getDays());
53-
assertEquals(
54-
BigInteger.valueOf(10).multiply(Interval.NANOS_PER_SECOND), interval.getNanoseconds());
70+
assertEquals(BigInteger.valueOf(Duration.ofSeconds(10).toNanos()), interval.getNanoseconds());
5571
}
5672

5773
@Test
5874
public void testOfMilliseconds() {
5975
Interval interval = Interval.ofMilliseconds(10);
6076
assertEquals(0, interval.getMonths());
6177
assertEquals(0, interval.getDays());
62-
assertEquals(
63-
BigInteger.valueOf(10).multiply(Interval.NANOS_PER_MILLISECOND), interval.getNanoseconds());
78+
assertEquals(BigInteger.valueOf(Duration.ofMillis(10).toNanos()), interval.getNanoseconds());
6479
}
6580

6681
@Test
@@ -69,7 +84,8 @@ public void testOfMicroseconds() {
6984
assertEquals(0, interval.getMonths());
7085
assertEquals(0, interval.getDays());
7186
assertEquals(
72-
BigInteger.valueOf(10).multiply(Interval.NANOS_PER_MICROSECOND), interval.getNanoseconds());
87+
BigInteger.valueOf(Duration.of(10, ChronoUnit.MICROS).toNanos()),
88+
interval.getNanoseconds());
7389
}
7490

7591
@Test
@@ -347,6 +363,32 @@ public void testHashCode() {
347363
assertNotEquals(interval10.hashCode(), interval11.hashCode());
348364
}
349365

366+
@Test
367+
public void testConstants() {
368+
assertEquals(Duration.of(1, ChronoUnit.HOURS).toMinutes(), MINUTES_PER_HOUR);
369+
assertEquals(Duration.of(1, ChronoUnit.MINUTES).getSeconds(), SECONDS_PER_MINUTE);
370+
assertEquals(Duration.of(1, ChronoUnit.HOURS).getSeconds(), SECONDS_PER_HOUR);
371+
assertEquals(Duration.of(1, ChronoUnit.SECONDS).toMillis(), MILLIS_PER_SECOND);
372+
373+
assertEquals(TimeUnit.MICROSECONDS.convert(1, TimeUnit.MILLISECONDS), MICROS_PER_MILLI);
374+
assertEquals(TimeUnit.NANOSECONDS.convert(1, TimeUnit.MICROSECONDS), NANOS_PER_MICRO);
375+
assertEquals(TimeUnit.MICROSECONDS.convert(1, TimeUnit.SECONDS), MICROS_PER_SECOND);
376+
assertEquals(TimeUnit.MICROSECONDS.convert(1, TimeUnit.MINUTES), MICROS_PER_MINUTE);
377+
assertEquals(TimeUnit.MICROSECONDS.convert(1, TimeUnit.HOURS), MICROS_PER_HOUR);
378+
379+
assertEquals(
380+
TimeUnit.NANOSECONDS.convert(1, TimeUnit.MICROSECONDS),
381+
NANOS_PER_MICROSECOND.longValueExact());
382+
assertEquals(
383+
TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS),
384+
NANOS_PER_MILLISECOND.longValueExact());
385+
assertEquals(
386+
TimeUnit.NANOSECONDS.convert(1, TimeUnit.SECONDS), NANOS_PER_SECOND.longValueExact());
387+
assertEquals(
388+
TimeUnit.NANOSECONDS.convert(1, TimeUnit.MINUTES), NANOS_PER_MINUTE.longValueExact());
389+
assertEquals(TimeUnit.NANOSECONDS.convert(1, TimeUnit.HOURS), NANOS_PER_HOUR.longValueExact());
390+
}
391+
350392
private static class TestCase {
351393
private final String intervalString;
352394
private final int months;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/MergedResultSetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public void testAllResultsAreReturned() {
150150
if (numPartitions == 0) {
151151
assertEquals(0, resultSet.getColumnCount());
152152
} else {
153-
assertEquals(24, resultSet.getColumnCount());
153+
assertEquals(26, resultSet.getColumnCount());
154154
assertEquals(Type.bool(), resultSet.getColumnType(0));
155155
assertEquals(Type.bool(), resultSet.getColumnType("COL0"));
156156
assertEquals(10, resultSet.getColumnIndex("COL10"));

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/PartitionedQueryMockServerTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ public void testRunEmptyPartitionedQuery() {
403403
statement, PartitionOptions.newBuilder().setMaxPartitions(maxPartitions).build())) {
404404
assertFalse(resultSet.next());
405405
assertNotNull(resultSet.getMetadata());
406-
assertEquals(24, resultSet.getMetadata().getRowType().getFieldsCount());
406+
assertEquals(26, resultSet.getMetadata().getRowType().getFieldsCount());
407407
assertNotNull(resultSet.getType());
408-
assertEquals(24, resultSet.getType().getStructFields().size());
408+
assertEquals(26, resultSet.getType().getStructFields().size());
409409
}
410410
if (isMultiplexedSessionsEnabled(connection.getSpanner())) {
411411
assertEquals(2, mockSpanner.countRequestsOfType(CreateSessionRequest.class));
@@ -435,15 +435,15 @@ public void testGetMetadataWithoutNextCall() {
435435
connection.runPartitionedQuery(
436436
statement, PartitionOptions.newBuilder().setMaxPartitions(maxPartitions).build())) {
437437
assertNotNull(resultSet.getMetadata());
438-
assertEquals(24, resultSet.getMetadata().getRowType().getFieldsCount());
438+
assertEquals(26, resultSet.getMetadata().getRowType().getFieldsCount());
439439
assertNotNull(resultSet.getType());
440-
assertEquals(24, resultSet.getType().getStructFields().size());
440+
assertEquals(26, resultSet.getType().getStructFields().size());
441441

442442
assertTrue(resultSet.next());
443443
assertNotNull(resultSet.getMetadata());
444-
assertEquals(24, resultSet.getMetadata().getRowType().getFieldsCount());
444+
assertEquals(26, resultSet.getMetadata().getRowType().getFieldsCount());
445445
assertNotNull(resultSet.getType());
446-
assertEquals(24, resultSet.getType().getStructFields().size());
446+
assertEquals(26, resultSet.getType().getStructFields().size());
447447

448448
assertFalse(resultSet.next());
449449
}
@@ -470,9 +470,9 @@ public void testGetMetadataWithoutNextCallOnEmptyResultSet() {
470470
connection.runPartitionedQuery(
471471
statement, PartitionOptions.newBuilder().setMaxPartitions(maxPartitions).build())) {
472472
assertNotNull(resultSet.getMetadata());
473-
assertEquals(24, resultSet.getMetadata().getRowType().getFieldsCount());
473+
assertEquals(26, resultSet.getMetadata().getRowType().getFieldsCount());
474474
assertNotNull(resultSet.getType());
475-
assertEquals(24, resultSet.getType().getStructFields().size());
475+
assertEquals(26, resultSet.getType().getStructFields().size());
476476

477477
assertFalse(resultSet.next());
478478
}

0 commit comments

Comments
 (0)