Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit f46c116

Browse files
committed
chore: Refactor timestamp column names to constants
1 parent d131f81 commit f46c116

4 files changed

Lines changed: 64 additions & 51 deletions

File tree

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryStorageReadClientTest.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.bigquery.storage.v1.it;
1818

19+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_COLUMN_NAME;
20+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME;
1921
import static com.google.common.truth.Truth.assertThat;
2022
import static com.google.common.truth.Truth.assertWithMessage;
2123
import static org.junit.Assert.assertArrayEquals;
@@ -530,10 +532,10 @@ private static void setupTimestampTable()
530532
throws DescriptorValidationException, IOException, InterruptedException {
531533
com.google.cloud.bigquery.Schema timestampSchema =
532534
com.google.cloud.bigquery.Schema.of(
533-
Field.newBuilder("timestamp", StandardSQLTypeName.TIMESTAMP)
535+
Field.newBuilder(TIMESTAMP_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
534536
.setMode(Mode.NULLABLE)
535537
.build(),
536-
Field.newBuilder("timestamp_higher_precision", StandardSQLTypeName.TIMESTAMP)
538+
Field.newBuilder(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
537539
.setTimestampPrecision(12L)
538540
.setMode(Mode.NULLABLE)
539541
.build());
@@ -542,13 +544,13 @@ private static void setupTimestampTable()
542544
TableSchema.newBuilder()
543545
.addFields(
544546
TableFieldSchema.newBuilder()
545-
.setName("timestamp")
547+
.setName(TIMESTAMP_COLUMN_NAME)
546548
.setType(TableFieldSchema.Type.TIMESTAMP)
547549
.setMode(TableFieldSchema.Mode.NULLABLE)
548550
.build())
549551
.addFields(
550552
TableFieldSchema.newBuilder()
551-
.setName("timestamp_higher_precision")
553+
.setName(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME)
552554
.setTimestampPrecision(Int64Value.newBuilder().setValue(12).build())
553555
.setType(TableFieldSchema.Type.TIMESTAMP)
554556
.setMode(TableFieldSchema.Mode.NULLABLE)
@@ -575,8 +577,8 @@ private static void setupTimestampTable()
575577
JSONArray data = new JSONArray();
576578
for (Object[] timestampData : Helper.INPUT_TIMESTAMPS) {
577579
JSONObject row = new JSONObject();
578-
row.put("timestamp", timestampData[0]);
579-
row.put("timestamp_higher_precision", timestampData[1]);
580+
row.put(TIMESTAMP_COLUMN_NAME, timestampData[0]);
581+
row.put(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME, timestampData[1]);
580582
data.put(row);
581583
}
582584

@@ -926,7 +928,7 @@ public void timestamp_readArrow() throws IOException {
926928
new SimpleRowReaderArrow.ArrowTimestampBatchConsumer(Helper.INPUT_TIMESTAMPS));
927929
rowCount += response.getRowCount();
928930
}
929-
assertEquals(Helper.EXPECTED_TIMESTAMPS.length, rowCount);
931+
assertEquals(Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION.length, rowCount);
930932
}
931933
}
932934

@@ -936,16 +938,17 @@ public void timestamp_readAvro() throws IOException {
936938
BigQueryResource.formatTableResource(projectName, DATASET, BQSTORAGE_TIMESTAMP_READ_TABLE);
937939
List<GenericData.Record> rows = Helper.readAllRows(readClient, parentProjectId, table, null);
938940
List<Long> timestamps =
939-
rows.stream().map(x -> (Long) x.get("timestamp")).collect(Collectors.toList());
941+
rows.stream().map(x -> (Long) x.get(TIMESTAMP_COLUMN_NAME)).collect(Collectors.toList());
940942
List<String> timestampHigherPrecision =
941943
rows.stream()
942-
.map(x -> x.get("timestamp_higher_precision").toString())
944+
.map(x -> x.get(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME).toString())
943945
.collect(Collectors.toList());
944946
for (int i = 0; i < timestamps.size(); i++) {
945-
assertEquals(Helper.EXPECTED_TIMESTAMPS[i][0], timestamps.get(i));
947+
assertEquals(Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION[i][0], timestamps.get(i));
946948
}
947949
for (int i = 0; i < timestampHigherPrecision.size(); i++) {
948-
assertEquals(Helper.EXPECTED_TIMESTAMPS[i][1], timestampHigherPrecision.get(i));
950+
assertEquals(
951+
Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION[i][1], timestampHigherPrecision.get(i));
949952
}
950953
}
951954

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/ITBigQueryStorageWriteClientTest.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.bigquery.storage.v1.it;
1818

19+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_COLUMN_NAME;
20+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME;
1921
import static com.google.common.truth.Truth.assertThat;
2022
import static org.junit.Assert.assertEquals;
2123
import static org.junit.Assert.assertFalse;
@@ -123,19 +125,19 @@ public class ITBigQueryStorageWriteClientTest {
123125
// The data will be padded to fit into the higher precision columns.
124126
public static final Object[][] INPUT_ARROW_WRITE_TIMESTAMPS =
125127
new Object[][] {
126-
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, 1735734896123456789L},
127-
{1580646896123456L /* 2020-02-02T12:34:56.123456Z */, 1580646896123456789L},
128-
{636467696123456L /* 1990-03-03T12:34:56.123456Z */, 636467696123456789L},
129-
{165846896123456L /* 1975-04-04T12:34:56.123456Z */, 165846896123456789L}
128+
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, 1735734896123456789L},
129+
{1580646896123456L /* 2020-02-02T12:34:56.123456Z */, 1580646896123456789L},
130+
{636467696123456L /* 1990-03-03T12:34:56.123456Z */, 636467696123456789L},
131+
{165846896123456L /* 1975-04-04T12:34:56.123456Z */, 165846896123456789L}
130132
};
131133

132134
// Arrow's higher precision column is padded with extra 0's.
133135
public static final Object[][] EXPECTED_ARROW_WRITE_TIMESTAMPS =
134136
new Object[][] {
135-
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, "2025-01-01T12:34:56.123456789000Z"},
136-
{1580646896123456L /* 2020-02-02T12:34:56.123456Z */, "2020-02-02T12:34:56.123456789000Z"},
137-
{636467696123456L /* 1990-03-03T12:34:56.123456Z */, "1990-03-03T12:34:56.123456789000Z"},
138-
{165846896123456L /* 1975-04-04T12:34:56.123456Z */, "1975-04-04T12:34:56.123456789000Z"}
137+
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, "2025-01-01T12:34:56.123456789000Z"},
138+
{1580646896123456L /* 2020-02-02T12:34:56.123456Z */, "2020-02-02T12:34:56.123456789000Z"},
139+
{636467696123456L /* 1990-03-03T12:34:56.123456Z */, "1990-03-03T12:34:56.123456789000Z"},
140+
{165846896123456L /* 1975-04-04T12:34:56.123456Z */, "1975-04-04T12:34:56.123456789000Z"}
139141
};
140142

141143
public static class StringWithSecondsNanos {
@@ -2296,14 +2298,12 @@ public void testLargeRequest() throws IOException, InterruptedException, Executi
22962298

22972299
@Test
22982300
public void timestamp_arrowWrite() throws IOException {
2299-
String timestampFieldName = "timestamp";
2300-
String timestampHigherPrecisionFieldName = "timestampHigherPrecision";
23012301
com.google.cloud.bigquery.Schema bigqueryTableSchema =
23022302
com.google.cloud.bigquery.Schema.of(
2303-
Field.newBuilder(timestampFieldName, StandardSQLTypeName.TIMESTAMP)
2303+
Field.newBuilder(TIMESTAMP_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
23042304
.setMode(Mode.NULLABLE)
23052305
.build(),
2306-
Field.newBuilder(timestampHigherPrecisionFieldName, StandardSQLTypeName.TIMESTAMP)
2306+
Field.newBuilder(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
23072307
.setMode(Mode.NULLABLE)
23082308
.setTimestampPrecision(12L)
23092309
.build());
@@ -2318,13 +2318,13 @@ public void timestamp_arrowWrite() throws IOException {
23182318
List<org.apache.arrow.vector.types.pojo.Field> fields =
23192319
ImmutableList.of(
23202320
new org.apache.arrow.vector.types.pojo.Field(
2321-
timestampFieldName,
2321+
TIMESTAMP_COLUMN_NAME,
23222322
FieldType.nullable(
23232323
new ArrowType.Timestamp(
23242324
org.apache.arrow.vector.types.TimeUnit.MICROSECOND, "UTC")),
23252325
null),
23262326
new org.apache.arrow.vector.types.pojo.Field(
2327-
timestampHigherPrecisionFieldName,
2327+
TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME,
23282328
FieldType.nullable(
23292329
new ArrowType.Timestamp(
23302330
org.apache.arrow.vector.types.TimeUnit.NANOSECOND, "UTC")),
@@ -2340,9 +2340,9 @@ public void timestamp_arrowWrite() throws IOException {
23402340
.build()) {
23412341
try (VectorSchemaRoot root = VectorSchemaRoot.create(arrowSchema, allocator)) {
23422342
TimeStampMicroTZVector timestampVector =
2343-
(TimeStampMicroTZVector) root.getVector(timestampFieldName);
2343+
(TimeStampMicroTZVector) root.getVector(TIMESTAMP_COLUMN_NAME);
23442344
TimeStampNanoTZVector timestampHigherPrecisionVector =
2345-
(TimeStampNanoTZVector) root.getVector(timestampHigherPrecisionFieldName);
2345+
(TimeStampNanoTZVector) root.getVector(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME);
23462346
timestampVector.allocateNew(numRows);
23472347
timestampHigherPrecisionVector.allocateNew(numRows);
23482348

@@ -2370,30 +2370,29 @@ public void timestamp_arrowWrite() throws IOException {
23702370
ServiceOptions.getDefaultProjectId(), DATASET, tableName);
23712371
List<GenericData.Record> rows = Helper.readAllRows(readClient, parentProjectId, table, null);
23722372
List<Long> timestamps =
2373-
rows.stream().map(x -> (Long) x.get(timestampFieldName)).collect(Collectors.toList());
2373+
rows.stream().map(x -> (Long) x.get(TIMESTAMP_COLUMN_NAME)).collect(Collectors.toList());
23742374
List<String> timestampHigherPrecision =
23752375
rows.stream()
2376-
.map(x -> x.get(timestampHigherPrecisionFieldName).toString())
2376+
.map(x -> x.get(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME).toString())
23772377
.collect(Collectors.toList());
2378-
assertEquals(timestamps.size(), Helper.EXPECTED_TIMESTAMPS.length);
2379-
assertEquals(timestampHigherPrecision.size(), Helper.EXPECTED_TIMESTAMPS.length);
2378+
assertEquals(timestamps.size(), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION.length);
2379+
assertEquals(
2380+
timestampHigherPrecision.size(), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION.length);
23802381
for (int i = 0; i < timestampHigherPrecision.size(); i++) {
2381-
assertEquals(timestamps.get(i), Helper.EXPECTED_TIMESTAMPS[i][0]);
2382+
assertEquals(timestamps.get(i), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION[i][0]);
23822383
assertEquals(timestampHigherPrecision.get(i), EXPECTED_ARROW_WRITE_TIMESTAMPS[i][1]);
23832384
}
23842385
}
23852386

23862387
@Test
23872388
public void timestamp_protobufWrite()
23882389
throws IOException, DescriptorValidationException, InterruptedException {
2389-
String timestampFieldName = "timestamp";
2390-
String timestampHigherPrecisionFieldName = "timestampHigherPrecision";
23912390
com.google.cloud.bigquery.Schema bqTableSchema =
23922391
com.google.cloud.bigquery.Schema.of(
2393-
Field.newBuilder(timestampFieldName, StandardSQLTypeName.TIMESTAMP)
2392+
Field.newBuilder(TIMESTAMP_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
23942393
.setMode(Mode.NULLABLE)
23952394
.build(),
2396-
Field.newBuilder(timestampHigherPrecisionFieldName, StandardSQLTypeName.TIMESTAMP)
2395+
Field.newBuilder(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME, StandardSQLTypeName.TIMESTAMP)
23972396
.setMode(Mode.NULLABLE)
23982397
.setTimestampPrecision(12L)
23992398
.build());
@@ -2406,13 +2405,13 @@ public void timestamp_protobufWrite()
24062405

24072406
TableFieldSchema testTimestamp =
24082407
TableFieldSchema.newBuilder()
2409-
.setName(timestampFieldName)
2408+
.setName(TIMESTAMP_COLUMN_NAME)
24102409
.setType(TableFieldSchema.Type.TIMESTAMP)
24112410
.setMode(TableFieldSchema.Mode.NULLABLE)
24122411
.build();
24132412
TableFieldSchema testTimestampHighPrecision =
24142413
TableFieldSchema.newBuilder()
2415-
.setName(timestampHigherPrecisionFieldName)
2414+
.setName(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME)
24162415
.setTimestampPrecision(Int64Value.newBuilder().setValue(12).build())
24172416
.setType(TableFieldSchema.Type.TIMESTAMP)
24182417
.setMode(TableFieldSchema.Mode.NULLABLE)
@@ -2429,8 +2428,8 @@ public void timestamp_protobufWrite()
24292428
JSONArray jsonArray = new JSONArray();
24302429
for (Object[] timestampData : Helper.INPUT_TIMESTAMPS) {
24312430
JSONObject row = new JSONObject();
2432-
row.put(timestampFieldName, timestampData[0]);
2433-
row.put(timestampHigherPrecisionFieldName, timestampData[1]);
2431+
row.put(TIMESTAMP_COLUMN_NAME, timestampData[0]);
2432+
row.put(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME, timestampData[1]);
24342433
jsonArray.put(row);
24352434
}
24362435
ApiFuture<AppendRowsResponse> future = jsonStreamWriter.append(jsonArray);
@@ -2443,16 +2442,18 @@ public void timestamp_protobufWrite()
24432442
ServiceOptions.getDefaultProjectId(), DATASET, tableName);
24442443
List<GenericData.Record> rows = Helper.readAllRows(readClient, parentProjectId, table, null);
24452444
List<Long> timestamps =
2446-
rows.stream().map(x -> (Long) x.get(timestampFieldName)).collect(Collectors.toList());
2445+
rows.stream().map(x -> (Long) x.get(TIMESTAMP_COLUMN_NAME)).collect(Collectors.toList());
24472446
List<String> timestampHigherPrecision =
24482447
rows.stream()
2449-
.map(x -> x.get(timestampHigherPrecisionFieldName).toString())
2448+
.map(x -> x.get(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME).toString())
24502449
.collect(Collectors.toList());
2451-
assertEquals(timestamps.size(), Helper.EXPECTED_TIMESTAMPS.length);
2452-
assertEquals(timestampHigherPrecision.size(), Helper.EXPECTED_TIMESTAMPS.length);
2450+
assertEquals(timestamps.size(), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION.length);
2451+
assertEquals(
2452+
timestampHigherPrecision.size(), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION.length);
24532453
for (int i = 0; i < timestamps.size(); i++) {
2454-
assertEquals(timestamps.get(i), Helper.EXPECTED_TIMESTAMPS[i][0]);
2455-
assertEquals(timestampHigherPrecision.get(i), Helper.EXPECTED_TIMESTAMPS[i][1]);
2454+
assertEquals(timestamps.get(i), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION[i][0]);
2455+
assertEquals(
2456+
timestampHigherPrecision.get(i), Helper.EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION[i][1]);
24562457
}
24572458
}
24582459
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/util/Helper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343

4444
public class Helper {
4545

46+
public static final String TIMESTAMP_COLUMN_NAME = "timestamp";
47+
public static final String TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME = "timestampHigherPrecision";
48+
49+
// Sample test cases for timestamps. First element is micros from epcoh and the second element
50+
// is the ISO format in with picosecond precision
4651
public static final Object[][] INPUT_TIMESTAMPS =
4752
new Object[][] {
4853
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, "2025-01-01T12:34:56.123456789123Z"},
@@ -51,7 +56,9 @@ public class Helper {
5156
{165846896123456L /* 1975-04-04T12:34:56.123456Z */, "1975-04-04T12:34:56.123456789123Z"}
5257
};
5358

54-
public static final Object[][] EXPECTED_TIMESTAMPS =
59+
// Expected response for timestamps from the input. The output is configured to return ISO8601
60+
// format for any picosecond enabled column.
61+
public static final Object[][] EXPECTED_TIMESTAMPS_ISO_HIGHER_PRECISION =
5562
new Object[][] {
5663
{1735734896123456L /* 2025-01-01T12:34:56.123456Z */, "2025-01-01T12:34:56.123456789123Z"},
5764
{1580646896123456L /* 2020-02-02T12:34:56.123456Z */, "2020-02-02T12:34:56.123456789123Z"},

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/it/util/SimpleRowReaderArrow.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.bigquery.storage.v1.it.util;
1818

19+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_COLUMN_NAME;
20+
import static com.google.cloud.bigquery.storage.v1.it.util.Helper.TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME;
1921
import static com.google.common.truth.Truth.assertThat;
2022

2123
import com.google.cloud.bigquery.FieldElementType;
@@ -58,18 +60,18 @@ public ArrowTimestampBatchConsumer(Object[][] expectedTimestampValues) {
5860

5961
@Override
6062
public void accept(VectorSchemaRoot root) {
61-
FieldVector timestampFieldVector = root.getVector("timestamp");
63+
FieldVector timestampFieldVector = root.getVector(TIMESTAMP_COLUMN_NAME);
6264
FieldVector timestampHigherPrecisionFieldVector =
63-
root.getVector("timestamp_higher_precision");
65+
root.getVector(TIMESTAMP_HIGHER_PRECISION_COLUMN_NAME);
6466
assertThat(timestampFieldVector.getValueCount())
6567
.isEqualTo(timestampHigherPrecisionFieldVector.getValueCount());
6668
int count = timestampFieldVector.getValueCount();
6769
for (int i = 0; i < count; i++) {
6870
long timestampMicros = (Long) timestampFieldVector.getObject(i);
6971
assertThat(timestampMicros).isEqualTo(expectedTimestampValues[i][0]);
7072

71-
// The Object comes back as `Text` which cannot be casted to String (use `toString()`
72-
// instead)
73+
// The Object comes back as `Text` which cannot be cast to String
74+
// (use `toString()` instead)
7375
String timestampHigherPrecisionISO =
7476
timestampHigherPrecisionFieldVector.getObject(i).toString();
7577
assertThat(timestampHigherPrecisionISO).isEqualTo(expectedTimestampValues[i][1]);

0 commit comments

Comments
 (0)