Skip to content

Commit 1e5f593

Browse files
author
Sagar Agarwal
committed
Added integration test for typed query parameter and fixed Interval parsing regex
1 parent f16ae42 commit 1e5f593

File tree

5 files changed

+89
-39
lines changed

5 files changed

+89
-39
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public abstract class Interval implements Serializable {
5151

5252
private static final Pattern INTERVAL_PATTERN =
5353
Pattern.compile(
54-
"^P(?!$)(-?\\d+Y)?(-?\\d+M)?(-?\\d+D)?(T(?=-?\\d)(-?\\d+H)?(-?\\d+M)?(-?\\d+(\\.\\d{1,9})?S)?)?$");
54+
"^P(?!$)(-?\\d+Y)?(-?\\d+M)?(-?\\d+D)?(T(?=-?.?\\d)(-?\\d+H)?(-?\\d+M)?(-?((\\d+(\\.\\d{1,9})?)|(\\.\\d{1,9}))S)?)?$");
5555

5656
/** Returns the months component of the interval. */
5757
public abstract int months();
@@ -154,8 +154,6 @@ public static Interval fromMonthsDaysNanos(int months, int days, BigInteger nano
154154
(nanos.subtract(BigInteger.valueOf(micros).multiply(BigInteger.valueOf(NANOS_PER_MICRO))))
155155
.shortValue();
156156

157-
System.out.println("Micros: " + micros + " Nanos: " + nanoFractions);
158-
159157
return builder()
160158
.setMonths(months)
161159
.setDays(days)

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/DirectExecuteResultSet.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
import com.google.cloud.ByteArray;
2020
import com.google.cloud.Date;
2121
import com.google.cloud.Timestamp;
22-
import com.google.cloud.spanner.*;
22+
import com.google.cloud.spanner.Interval;
23+
import com.google.cloud.spanner.ProtobufResultSet;
24+
import com.google.cloud.spanner.ResultSet;
25+
import com.google.cloud.spanner.SpannerException;
26+
import com.google.cloud.spanner.Struct;
27+
import com.google.cloud.spanner.Type;
28+
import com.google.cloud.spanner.Value;
2329
import com.google.common.base.Preconditions;
2430
import com.google.protobuf.AbstractMessage;
2531
import com.google.protobuf.ProtocolMessageEnum;

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReplaceableForwardingResultSet.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919
import com.google.cloud.ByteArray;
2020
import com.google.cloud.Date;
2121
import com.google.cloud.Timestamp;
22-
import com.google.cloud.spanner.*;
22+
import com.google.cloud.spanner.ErrorCode;
23+
import com.google.cloud.spanner.Interval;
24+
import com.google.cloud.spanner.ProtobufResultSet;
25+
import com.google.cloud.spanner.ResultSet;
26+
import com.google.cloud.spanner.SpannerException;
27+
import com.google.cloud.spanner.SpannerExceptionFactory;
28+
import com.google.cloud.spanner.Struct;
29+
import com.google.cloud.spanner.Type;
30+
import com.google.cloud.spanner.Value;
2331
import com.google.common.base.Preconditions;
2432
import com.google.protobuf.AbstractMessage;
2533
import com.google.protobuf.ProtocolMessageEnum;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,7 @@ public void testValueToProto() {
18551855
.addAllValues(
18561856
Arrays.asList(
18571857
com.google.protobuf.Value.newBuilder()
1858-
.setStringValue("P1Y2M3DT5H6M2.456787800S")
1858+
.setStringValue("P1Y2M3DT5H6M2.4567878S")
18591859
.build(),
18601860
com.google.protobuf.Value.newBuilder()
18611861
.setNullValue(NullValue.NULL_VALUE)

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITIntervalTest.java

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package com.google.cloud.spanner.it;
1818

1919
import static com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator;
20-
import static com.google.common.base.Strings.isNullOrEmpty;
2120
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertFalse;
2321
import static org.junit.Assert.assertTrue;
2422
import static org.junit.Assume.assumeFalse;
2523
import static org.junit.Assume.assumeTrue;
@@ -30,6 +28,7 @@
3028
import java.util.Arrays;
3129
import java.util.Collections;
3230
import java.util.List;
31+
import java.util.Objects;
3332
import java.util.concurrent.ExecutionException;
3433
import java.util.concurrent.TimeoutException;
3534
import org.junit.AfterClass;
@@ -63,32 +62,30 @@ public static List<DialectTestParameter> data() {
6362
new String[] {
6463
"CREATE TABLE IntervalTable (\n"
6564
+ " key STRING(MAX),\n"
66-
+ " slo_days INT64,\n"
67-
+ " update_time TIMESTAMP,\n"
68-
+ " expiry_days INT64 AS (EXTRACT(DAY FROM make_interval(day => GREATEST(LEAST(slo_days, 365), 1)))),\n"
69-
+ " interval_array_len bigint AS (ARRAY_LENGTH(ARRAY<INTERVAL>[INTERVAL '1-2 3 4:5:6' YEAR TO SECOND]))\n"
70-
+ ") PRIMARY KEY (key);"
65+
+ " create_time TIMESTAMP,\n"
66+
+ " expiry_time TIMESTAMP,\n"
67+
+ " expiry_within_month bool AS (expiry_time - create_time < INTERVAL 30 DAY),\n"
68+
+ " interval_array_len INT64 AS (ARRAY_LENGTH(ARRAY<INTERVAL>[INTERVAL '1-2 3 4:5:6' YEAR TO SECOND]))\n"
69+
+ ") PRIMARY KEY (key)"
7170
};
7271

7372
private static final String[] POSTGRESQL_SCHEMA =
7473
new String[] {
7574
"CREATE TABLE IntervalTable (\n"
7675
+ " key text primary key,\n"
77-
+ " slo_days bigint,\n"
78-
+ " update_time timestamptz,\n"
79-
+ " expiry_days bigint GENERATED ALWAYS AS (EXTRACT(DAY FROM make_interval(days =>GREATEST(LEAST(slo_days, 365), 1)))) STORED,\n"
76+
+ " create_time timestamptz,\n"
77+
+ " expiry_time timestamptz,\n"
78+
+ " expiry_within_month bool GENERATED ALWAYS AS (INTERVAL '1' DAY < INTERVAL '30' DAY) STORED,\n"
8079
+ " interval_array_len bigint GENERATED ALWAYS AS (ARRAY_LENGTH(ARRAY[INTERVAL '1-2 3 4:5:6'], 1)) STORED\n"
81-
+ ");"
80+
+ ")"
8281
};
8382

8483
private static DatabaseClient client;
8584

8685
private static boolean isUsingCloudDevel() {
87-
String jobType = System.getenv("JOB_TYPE");
88-
89-
// Assumes that the jobType contains the string "cloud-devel" to signal that
90-
// the environment is cloud-devel.
91-
return !isNullOrEmpty(jobType) && jobType.contains("cloud-devel");
86+
return Objects.equals(
87+
System.getProperty("spanner.gce.config.server_url"),
88+
"https://staging-wrenchworks.sandbox.googleapis.com");
9289
}
9390

9491
@BeforeClass
@@ -131,7 +128,9 @@ private Timestamp write(Mutation m) {
131128
}
132129

133130
private Mutation.WriteBuilder baseInsert() {
134-
return Mutation.newInsertOrUpdateBuilder("T").set("Key").to(lastKey = uniqueString());
131+
return Mutation.newInsertOrUpdateBuilder("IntervalTable")
132+
.set("Key")
133+
.to(lastKey = uniqueString());
135134
}
136135

137136
private Struct readRow(String table, String key, String... columns) {
@@ -141,23 +140,30 @@ private Struct readRow(String table, String key, String... columns) {
141140
}
142141

143142
private Struct readLastRow(String... columns) {
144-
return readRow("T", lastKey, columns);
143+
return readRow("IntervalTable", lastKey, columns);
145144
}
146145

147146
@Test
148147
public void writeToTableWithIntervalExpressions() {
149148
write(
150149
baseInsert()
151-
.set("slo_days")
152-
.to(5)
153-
.set("update_time")
154-
.to(Timestamp.ofTimeMicroseconds(12345678L))
150+
.set("create_time")
151+
.to(Timestamp.parseTimestamp("2004-11-30T04:53:54Z"))
152+
.set("expiry_time")
153+
.to(Timestamp.parseTimestamp("2004-12-15T04:53:54Z"))
155154
.build());
156-
Struct row = readLastRow("expiryDays", "interval_array_len");
157-
assertFalse(row.isNull(0));
158-
assertEquals(5, row.getLong(0));
159-
assertFalse(row.isNull(1));
160-
assertEquals(1, row.getLong(1));
155+
try (ResultSet resultSet =
156+
client
157+
.singleUse()
158+
.executeQuery(
159+
Statement.of(
160+
"SELECT expiry_within_month, interval_array_len FROM IntervalTable WHERE key='"
161+
+ lastKey
162+
+ "'"))) {
163+
assertTrue(resultSet.next());
164+
assertTrue(resultSet.getBoolean(0));
165+
assertEquals(1, resultSet.getLong(1));
166+
}
161167
}
162168

163169
@Test
@@ -167,7 +173,39 @@ public void queryInterval() {
167173
.singleUse()
168174
.executeQuery(Statement.of("SELECT INTERVAL '1' DAY + INTERVAL '1' MONTH AS Col1"))) {
169175
assertTrue(resultSet.next());
170-
assertTrue(resultSet.getInterval(0).equals(Interval.fromMonthsDaysMicros(1, 1, 0)));
176+
assertEquals(resultSet.getInterval(0), Interval.fromMonthsDaysMicros(1, 1, 0));
177+
}
178+
}
179+
180+
@Test
181+
public void queryWithIntervalParam() {
182+
write(
183+
baseInsert()
184+
.set("create_time")
185+
.to(Timestamp.parseTimestamp("2004-08-30T04:53:54Z"))
186+
.set("expiry_time")
187+
.to(Timestamp.parseTimestamp("2004-12-15T04:53:54Z"))
188+
.build());
189+
190+
String query;
191+
if (dialect.dialect == Dialect.POSTGRESQL) {
192+
query =
193+
"SELECT COUNT(*) FROM IntervalTable WHERE create_time < TIMESTAMPTZ '2004-11-30T10:23:54+0530' - $1";
194+
} else {
195+
query =
196+
"SELECT COUNT(*) FROM IntervalTable WHERE create_time < TIMESTAMP('2004-11-30T10:23:54+0530') - @p1";
197+
}
198+
199+
try (ResultSet resultSet =
200+
client
201+
.singleUse()
202+
.executeQuery(
203+
Statement.newBuilder(query)
204+
.bind("p1")
205+
.to(Value.interval(Interval.ofDays(30)))
206+
.build())) {
207+
assertTrue(resultSet.next());
208+
assertEquals(resultSet.getLong(0), 1L);
171209
}
172210
}
173211

@@ -203,12 +241,12 @@ public void queryIntervalArray() {
203241
"SELECT ARRAY[CAST('P1Y2M3DT4H5M6.789123S' AS INTERVAL), null, CAST('P-1Y-2M-3DT-4H-5M-6.789123S' AS INTERVAL)] AS Col1";
204242
try (ResultSet resultSet = client.singleUse().executeQuery(Statement.of(query))) {
205243
assertTrue(resultSet.next());
206-
assertTrue(
244+
assertEquals(
207245
Arrays.asList(
208-
Interval.parseFromString("P1Y2M3DT4H5M6.789123S"),
209-
null,
210-
Interval.parseFromString("P-1Y-2M-3DT-4H-5M-6.789123S"))
211-
.equals(resultSet.getIntervalList(0)));
246+
Interval.parseFromString("P1Y2M3DT4H5M6.789123S"),
247+
null,
248+
Interval.parseFromString("P-1Y-2M-3DT-4H-5M-6.789123S")),
249+
resultSet.getIntervalList(0));
212250
}
213251
}
214252
}

0 commit comments

Comments
 (0)