From 2b50235fb61a77064a646ae80cd9ccdaa1fec85e Mon Sep 17 00:00:00 2001 From: Sagar Agarwal Date: Thu, 24 Oct 2024 20:57:21 +0530 Subject: [PATCH] Added integration test for typed query parameter and fixed Interval parsing regex --- .../com/google/cloud/spanner/Interval.java | 4 +-- .../ReplaceableForwardingResultSet.java | 10 +++++- .../com/google/cloud/spanner/ValueTest.java | 2 +- .../cloud/spanner/it/ITIntervalTest.java | 34 +++++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Interval.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Interval.java index 68b81490dc..c53524b3eb 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Interval.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Interval.java @@ -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(); @@ -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) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReplaceableForwardingResultSet.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReplaceableForwardingResultSet.java index 59229037e1..a6198b7d63 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReplaceableForwardingResultSet.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReplaceableForwardingResultSet.java @@ -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; diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ValueTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ValueTest.java index 6b2544c191..cef76b2998 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ValueTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ValueTest.java @@ -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) diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITIntervalTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITIntervalTest.java index 710e9ed483..2964bfb815 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITIntervalTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITIntervalTest.java @@ -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 = @@ -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;