From c20118010b416a0f70adf430bd36b3c7c6219331 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 | 2 +- .../ReplaceableForwardingResultSet.java | 10 ++++++++- .../com/google/cloud/spanner/ValueTest.java | 2 +- .../cloud/spanner/it/ITIntervalTest.java | 22 +++++++++++++++++++ 4 files changed, 33 insertions(+), 3 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..0591f8519a 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(); 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..fdfdbf90ed 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 @@ -171,6 +171,28 @@ public void queryInterval() { } } + @Test + public void queryWithIntervalParam() { + String query; + if (dialect.dialect == Dialect.POSTGRESQL) { + query = "SELECT TIMESTAMP '2004-10-19 10:23:54+0530' + $1 AS Col1"; + } else { + query = "SELECT TIMESTAMP('2004-10-19 10:23:54+0530') + @p1 AS Col1"; + } + + try (ResultSet resultSet = + client + .singleUse() + .executeQuery( + Statement.newBuilder(query) + .bind("p1") + .to(Value.interval(Interval.ofDays(10))) + .build())) { + assertTrue(resultSet.next()); + assertEquals(resultSet.getTimestamp(0), Timestamp.parseTimestamp("2004-10-29 10:23:54+0530")); + } + } + @Test public void queryWithUntypedIntervalParam() { String query;