Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changes to support data, timestamp and arrays in IT tests #1840

Merged
merged 8 commits into from Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -355,7 +355,6 @@ public void bindTimestampNull() {

@Test
public void bindDate() {
assumeFalse("date type is not supported on POSTGRESQL", dialect.dialect == Dialect.POSTGRESQL);
Date d = Date.parseDate("2016-09-18");
Struct row = execute(Statement.newBuilder(selectValueQuery).bind("p1").to(d), Type.date());
assertThat(row.isNull(0)).isFalse();
Expand All @@ -364,7 +363,6 @@ public void bindDate() {

@Test
public void bindDateNull() {
assumeFalse("date type is not supported on POSTGRESQL", dialect.dialect == Dialect.POSTGRESQL);
Struct row =
execute(Statement.newBuilder(selectValueQuery).bind("p1").to((Date) null), Type.date());
assertThat(row.isNull(0)).isTrue();
Expand Down Expand Up @@ -682,35 +680,33 @@ public void bindTimestampArrayNull() {

@Test
public void bindDateArray() {
assumeFalse("date type is not supported on POSTGRESQL", dialect.dialect == Dialect.POSTGRESQL);
Date d1 = Date.parseDate("2016-09-18");
Date d2 = Date.parseDate("2016-09-19");

Struct row =
execute(
Statement.newBuilder("SELECT @v").bind("v").toDateArray(asList(d1, d2, null)),
Statement.newBuilder(selectValueQuery).bind("p1").toDateArray(asList(d1, d2, null)),
Type.array(Type.date()));
assertThat(row.isNull(0)).isFalse();
assertThat(row.getDateList(0)).containsExactly(d1, d2, null).inOrder();
}

@Test
public void bindDateArrayEmpty() {
assumeFalse("date type is not supported on POSTGRESQL", dialect.dialect == Dialect.POSTGRESQL);
Struct row =
execute(
Statement.newBuilder("SELECT @v").bind("v").toDateArray(Collections.emptyList()),
Statement.newBuilder(selectValueQuery).bind("p1").toDateArray(Collections.emptyList()),
Type.array(Type.date()));
assertThat(row.isNull(0)).isFalse();
assertThat(row.getDateList(0)).containsExactly();
}

@Test
public void bindDateArrayNull() {
assumeFalse("date type is not supported on POSTGRESQL", dialect.dialect == Dialect.POSTGRESQL);
Struct row =
execute(
Statement.newBuilder("SELECT @v").bind("v").toDateArray(null), Type.array(Type.date()));
Statement.newBuilder(selectValueQuery).bind("p1").toDateArray(null),
Type.array(Type.date()));
assertThat(row.isNull(0)).isTrue();
}

Expand Down Expand Up @@ -771,7 +767,7 @@ public void bindNumericArray_doesNotPreservePrecision() {

Struct row =
execute(
Statement.newBuilder("SELECT @v").bind("v").toNumericArray(asList(b1, b2, null)),
Statement.newBuilder(selectValueQuery).bind("p1").toNumericArray(asList(b1, b2, null)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case seem to be disabled for PostgreSQL still. Is that intentional?
The same also seems to be the case for other numeric array tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed those, adding it

Type.array(Type.numeric()));
assertThat(row.isNull(0)).isFalse();
assertThat(row.getBigDecimalList(0))
Expand Down
Expand Up @@ -21,7 +21,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

import com.google.cloud.ByteArray;
import com.google.cloud.Date;
Expand Down Expand Up @@ -128,7 +127,17 @@ public static void beforeClass()
+ "float64 DOUBLE PRECISION,"
+ "numeric NUMERIC,"
+ "string VARCHAR,"
+ "bytes BYTEA"
+ "bytes BYTEA,"
+ "timestamp TIMESTAMPTZ,"
+ "date DATE,"
+ "boolArray BOOL[],"
+ "int64Array BIGINT[],"
+ "float64Array DOUBLE PRECISION[],"
+ "numericArray NUMERIC[],"
+ "stringArray VARCHAR[],"
+ "bytesArray BYTEA[],"
+ "dateArray DATE[],"
+ "timestampArray TIMESTAMPTZ[]"
+ ")"));
postgreSQLClient = env.getTestHelper().getDatabaseClient(postgreSQLDatabase);
}
Expand Down Expand Up @@ -264,6 +273,32 @@ public void testReadNonNullValuesPostgreSQL() {
.to("stringValue")
.set("bytes")
.to(ByteArray.copyFrom("bytesValue"))
.set("date")
.to(Date.fromYearMonthDay(2021, 1, 2))
.set("timestamp")
.to(Timestamp.ofTimeSecondsAndNanos(1, 0))
.set("boolArray")
.toBoolArray(new boolean[] {false, true})
.set("int64Array")
.toInt64Array(new long[] {100L, 200L})
.set("float64Array")
.toFloat64Array(new double[] {1000D, 2000D})
.set("numericArray")
.toNumericArray(Arrays.asList(new BigDecimal("10000"), new BigDecimal("20000")))
.set("stringArray")
.toStringArray(Arrays.asList("string1", "string2"))
.set("bytesArray")
.toBytesArray(
Arrays.asList(ByteArray.copyFrom("bytes1"), ByteArray.copyFrom("bytes2")))
.set("timestampArray")
.toTimestampArray(
Arrays.asList(
Timestamp.ofTimeSecondsAndNanos(10, 0),
Timestamp.ofTimeSecondsAndNanos(20, 0)))
.set("dateArray")
.toDateArray(
Arrays.asList(
Date.fromYearMonthDay(2021, 2, 3), Date.fromYearMonthDay(2021, 3, 4)))
.build()));

try (ResultSet resultSet =
Expand All @@ -278,6 +313,34 @@ public void testReadNonNullValuesPostgreSQL() {
assertEquals(Value.pgNumeric("30"), resultSet.getValue("numeric"));
assertEquals(Value.string("stringValue"), resultSet.getValue("string"));
assertEquals(Value.bytes(ByteArray.copyFrom("bytesValue")), resultSet.getValue("bytes"));
assertEquals(
Value.timestamp(Timestamp.ofTimeSecondsAndNanos(1, 0)), resultSet.getValue("timestamp"));
assertEquals(Value.date(Date.fromYearMonthDay(2021, 1, 2)), resultSet.getValue("date"));
assertEquals(Value.boolArray(new boolean[] {false, true}), resultSet.getValue("boolarray"));
assertEquals(Value.int64Array(new long[] {100L, 200L}), resultSet.getValue("int64array"));
assertArrayEquals(
new double[] {1000D, 2000D},
Doubles.toArray(resultSet.getValue("float64array").getFloat64Array()),
1e-15);
assertEquals(
Value.pgNumericArray(Arrays.asList("10000", "20000")),
resultSet.getValue("numericarray"));
assertEquals(
Value.stringArray(Arrays.asList("string1", "string2")),
resultSet.getValue("stringarray"));
assertEquals(
Value.bytesArray(
Arrays.asList(ByteArray.copyFrom("bytes1"), ByteArray.copyFrom("bytes2"))),
resultSet.getValue("bytesarray"));
assertEquals(
Value.timestampArray(
Arrays.asList(
Timestamp.ofTimeSecondsAndNanos(10, 0), Timestamp.ofTimeSecondsAndNanos(20, 0))),
resultSet.getValue("timestamparray"));
assertEquals(
Value.dateArray(
Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), Date.fromYearMonthDay(2021, 3, 4))),
resultSet.getValue("datearray"));
}
}

Expand Down Expand Up @@ -367,12 +430,42 @@ public void testReadNullValuesPostgreSQL() {
assertThrows(IllegalStateException.class, () -> resultSet.getValue("string").getString());
assertTrue(resultSet.getValue("bytes").isNull());
assertThrows(IllegalStateException.class, () -> resultSet.getValue("bytes").getBytes());
assertTrue(resultSet.getValue("timestamp").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("timestamp").getTimestamp());
assertTrue(resultSet.getValue("date").isNull());
assertThrows(IllegalStateException.class, () -> resultSet.getValue("date").getDate());
assertTrue(resultSet.getValue("boolarray").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("boolarray").getBoolArray());
assertTrue(resultSet.getValue("int64array").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("int64array").getInt64Array());
assertTrue(resultSet.getValue("float64array").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("float64array").getFloat64Array());
assertTrue(resultSet.getValue("numericarray").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("numericarray").getNumericArray());
assertTrue(resultSet.getValue("stringarray").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("stringarray").getStringArray());
assertTrue(resultSet.getValue("bytesarray").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("bytesarray").getBytesArray());
assertTrue(resultSet.getValue("timestamparray").isNull());
assertThrows(
IllegalStateException.class,
() -> resultSet.getValue("timestamparray").getTimestampArray());
assertTrue(resultSet.getValue("datearray").isNull());
assertThrows(
IllegalStateException.class, () -> resultSet.getValue("datearray").getDateArray());
}
}

@Test
public void testReadNullValuesInArrays() {
assumeFalse("PostgreSQL does not yet support Arrays", dialect.dialect == Dialect.POSTGRESQL);
public void testReadNullValuesInArraysGoogleStandardSQL() {
Assume.assumeTrue(dialect.dialect == Dialect.GOOGLE_STANDARD_SQL);
databaseClient.write(
Collections.singletonList(
Mutation.newInsertBuilder(TABLE_NAME)
Expand Down Expand Up @@ -429,6 +522,59 @@ public void testReadNullValuesInArrays() {
}
}

@Test
public void testReadNullValuesInArraysPostgreSQL() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need separate tests for GoogleSQL and PostgreSQL in this case? They seem to be equal. Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

structs and json are not supported and column names are different as PG requires explicit quotes for case

Assume.assumeTrue(dialect.dialect == Dialect.POSTGRESQL);
databaseClient.write(
Collections.singletonList(
Mutation.newInsertBuilder(TABLE_NAME)
.set("Id")
.to(3L)
.set("boolArray")
.toBoolArray(Arrays.asList(true, null))
.set("int64Array")
.toInt64Array(Arrays.asList(null, 2L))
.set("float64Array")
.toFloat64Array(Arrays.asList(null, 10D))
.set("numericArray")
.toNumericArray(Arrays.asList(new BigDecimal("10000"), null))
.set("stringArray")
.toStringArray(Arrays.asList(null, "string2"))
.set("bytesArray")
.toBytesArray(Arrays.asList(ByteArray.copyFrom("bytes1"), null))
.set("timestampArray")
.toTimestampArray(Arrays.asList(null, Timestamp.ofTimeSecondsAndNanos(20, 0)))
.set("dateArray")
.toDateArray(Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), null))
.build()));

try (ResultSet resultSet =
databaseClient
.singleUse()
.executeQuery(Statement.of("SELECT * FROM " + TABLE_NAME + " WHERE Id = 3"))) {
resultSet.next();

assertEquals(Value.int64(3L), resultSet.getValue("id"));
assertEquals(Value.boolArray(Arrays.asList(true, null)), resultSet.getValue("boolarray"));
assertEquals(Value.int64Array(Arrays.asList(null, 2L)), resultSet.getValue("int64array"));
assertNull(resultSet.getValue("float64array").getFloat64Array().get(0));
assertEquals(10D, resultSet.getValue("float64array").getFloat64Array().get(1), DELTA);
assertEquals(
Value.pgNumericArray(Arrays.asList("10000", null)), resultSet.getValue("numericarray"));
assertEquals(
Value.stringArray(Arrays.asList(null, "string2")), resultSet.getValue("stringarray"));
assertEquals(
Value.bytesArray(Arrays.asList(ByteArray.copyFrom("bytes1"), null)),
resultSet.getValue("bytesarray"));
assertEquals(
Value.timestampArray(Arrays.asList(null, Timestamp.ofTimeSecondsAndNanos(20, 0))),
resultSet.getValue("timestamparray"));
assertEquals(
Value.dateArray(Arrays.asList(Date.fromYearMonthDay(2021, 2, 3), null)),
resultSet.getValue("datearray"));
}
}

@Test
public void testReadNonFloat64LiteralsGoogleStandardSQL() {
Assume.assumeTrue(dialect.dialect == Dialect.GOOGLE_STANDARD_SQL);
Expand Down Expand Up @@ -570,14 +716,46 @@ public void testReadNonFloat64LiteralsPostgreSQL() {
+ "1 AS int64,"
+ "CAST('100' AS numeric) AS numeric,"
+ "'stringValue' AS string,"
+ "CAST('bytesValue' AS BYTEA) AS bytes"))) {
+ "CAST('bytesValue' AS BYTEA) AS bytes,"
+ "CAST('1970-01-01T00:00:01 UTC' AS TIMESTAMPTZ) AS timestamp,"
+ "CAST('2021-02-03' AS DATE) AS date,"
+ "ARRAY[false, true] AS boolArray,"
+ "ARRAY[1, 2] AS int64Array,"
+ "ARRAY[CAST('100' AS NUMERIC), CAST('200' AS NUMERIC)] AS numericArray,"
+ "ARRAY['string1', 'string2'] AS stringArray,"
+ "ARRAY[CAST('bytes1' AS BYTEA), CAST('bytes2' AS BYTEA)] AS bytesArray,"
+ "ARRAY[CAST('1970-01-01T00:00:01 UTC' AS TIMESTAMPTZ), CAST('1970-01-01T00:00:02 UTC' AS TIMESTAMPTZ)] AS timestampArray,"
+ "ARRAY[CAST('2020-01-02' AS DATE), CAST('2021-02-03' AS DATE)] AS dateArray"))) {
resultSet.next();

assertEquals(Value.bool(true), resultSet.getValue("bool"));
assertEquals(Value.int64(1L), resultSet.getValue("int64"));
assertEquals(Value.pgNumeric("100"), resultSet.getValue("numeric"));
assertEquals(Value.string("stringValue"), resultSet.getValue("string"));
assertEquals(Value.bytes(ByteArray.copyFrom("bytesValue")), resultSet.getValue("bytes"));
assertEquals(
Value.timestamp(Timestamp.ofTimeSecondsAndNanos(1, 0)), resultSet.getValue("timestamp"));
assertEquals(Value.date(Date.fromYearMonthDay(2021, 2, 3)), resultSet.getValue("date"));
assertEquals(Value.boolArray(new boolean[] {false, true}), resultSet.getValue("boolarray"));
assertEquals(Value.int64Array(new long[] {1L, 2L}), resultSet.getValue("int64array"));
assertEquals(
Value.pgNumericArray(Arrays.asList("100", "200")), resultSet.getValue("numericarray"));
assertEquals(
Value.stringArray(Arrays.asList("string1", "string2")),
resultSet.getValue("stringarray"));
assertEquals(
Value.bytesArray(
Arrays.asList(ByteArray.copyFrom("bytes1"), ByteArray.copyFrom("bytes2"))),
resultSet.getValue("bytesarray"));
assertEquals(
Value.timestampArray(
Arrays.asList(
Timestamp.ofTimeSecondsAndNanos(1, 0), Timestamp.ofTimeSecondsAndNanos(2, 0))),
resultSet.getValue("timestamparray"));
assertEquals(
Value.dateArray(
Arrays.asList(Date.fromYearMonthDay(2020, 1, 2), Date.fromYearMonthDay(2021, 2, 3))),
resultSet.getValue("datearray"));
}
}

Expand Down Expand Up @@ -615,9 +793,16 @@ public void testReadFloat64LiteralsGoogleStandardSQL() {
public void testReadFloat64LiteralsPostgreSQL() {
Assume.assumeTrue(dialect.dialect == Dialect.POSTGRESQL);
try (ResultSet resultSet =
databaseClient.singleUse().executeQuery(Statement.of("SELECT 10.0 AS float64"))) {
databaseClient
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need a separate test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

.singleUse()
.executeQuery(
Statement.of("SELECT 10.0 AS float64, " + "ARRAY[20.0, 30.0] AS float64Array"))) {
resultSet.next();
assertEquals(10D, resultSet.getValue("float64").getFloat64(), DELTA);
assertArrayEquals(
new double[] {20D, 30D},
Doubles.toArray(resultSet.getValue("float64array").getFloat64Array()),
DELTA);
}
}
}