Skip to content

Commit

Permalink
Added integration test for typed query parameter and fixed Interval p…
Browse files Browse the repository at this point in the history
…arsing regex
  • Loading branch information
Sagar Agarwal committed Oct 24, 2024
1 parent f16ae42 commit 2b50235
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class Interval implements Serializable {

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

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

System.out.println("Micros: " + micros + " Nanos: " + nanoFractions);

return builder()
.setMonths(months)
.setDays(days)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
import com.google.cloud.ByteArray;
import com.google.cloud.Date;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.*;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Interval;
import com.google.cloud.spanner.ProtobufResultSet;
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.Type;
import com.google.cloud.spanner.Value;
import com.google.common.base.Preconditions;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.ProtocolMessageEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ public void testValueToProto() {
.addAllValues(
Arrays.asList(
com.google.protobuf.Value.newBuilder()
.setStringValue("P1Y2M3DT5H6M2.456787800S")
.setStringValue("P1Y2M3DT5H6M2.4567878S")
.build(),
com.google.protobuf.Value.newBuilder()
.setNullValue(NullValue.NULL_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static boolean isUsingCloudDevel() {
public static void setUpDatabase()
throws ExecutionException, InterruptedException, TimeoutException {
assumeTrue("Interval is supported only in Cloud-Devel for now", isUsingCloudDevel());
System.out.println("---Running Interval Integration Tests.---");
assumeFalse("Emulator does not support Interval yet", isUsingEmulator());

Database googleStandardSQLDatabase =
Expand Down Expand Up @@ -171,6 +172,39 @@ public void queryInterval() {
}
}

@Test
public void queryWithIntervalParam() {

write(
baseInsert()
.set("slo_days")
.to(5)
.set("update_time")
.to(Timestamp.parseTimestamp("2004-10-19 10:23:54+0530"))
.build());

String query;
if (dialect.dialect == Dialect.POSTGRESQL) {
query =
"SELECT COUNT(*) FROM IntervalTable WHERE update_time < TIMESTAMP '2004-11-30 10:23:54+0530' - $1";
} else {
query =
"SELECT SELECT COUNT(*) FROM IntervalTable WHERE update_time < TIMESTAMP('2004-11-30 10:23:54+0530') - @p1";
}

try (ResultSet resultSet =
client
.singleUse()
.executeQuery(
Statement.newBuilder(query)
.bind("p1")
.to(Value.interval(Interval.ofDays(30)))
.build())) {
assertTrue(resultSet.next());
assertEquals(resultSet.getLong(0), 1L);
}
}

@Test
public void queryWithUntypedIntervalParam() {
String query;
Expand Down

0 comments on commit 2b50235

Please sign in to comment.