GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(bigquery-jdbc): support picosecond timestamp in `ResultSetMetaDa… · googleapis/google-cloud-java@43acdd3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • java-bigquery-jdbc/src
    • main/java/com/google/cloud/bigquery/jdbc
      • BigQueryDatabaseMetaData.java
      • BigQueryResultSetMetadata.java
      • BigQueryTemporalUtility.java
    • test/java/com/google/cloud/bigquery/jdbc
      • BigQueryDatabaseMetaDataTest.java
      • BigQueryResultSetMetadataTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java‎

Copy file name to clipboardExpand all lines: java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,12 @@ ColumnTypeInfo mapBigQueryTypeToJdbc(Field field) {
18131813
return new ColumnTypeInfo(Types.ARRAY, "ARRAY", null, null, null);
18141814
}
18151815

1816+
if (this.connection.isEnableTimestampPicos()
1817+
&& BigQueryTemporalUtility.isPicosecondTimestamp(field)) {
1818+
return new ColumnTypeInfo(
1819+
Types.VARCHAR, BigQueryTemporalUtility.TIMESTAMP_PICOSECONDS_TYPE_NAME, 32, 12, null);
1820+
}
1821+
18161822
StandardSQLTypeName bqType = null;
18171823
if (field.getType() != null && field.getType().getStandardType() != null) {
18181824
bqType = field.getType().getStandardType();
Collapse file

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryResultSetMetadata.java‎

Copy file name to clipboardExpand all lines: java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryResultSetMetadata.java
+35-7Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,50 @@
2121
import com.google.cloud.bigquery.FieldList;
2222
import com.google.cloud.bigquery.StandardSQLTypeName;
2323
import com.google.cloud.bigquery.exception.BigQueryJdbcException;
24+
import java.sql.Array;
2425
import java.sql.ResultSetMetaData;
2526
import java.sql.SQLException;
26-
import java.sql.Statement;
2727
import java.sql.Types;
2828

2929
/** This class returns ResultSetMetadata for the JSON and the Arrow ResultSets */
3030
class BigQueryResultSetMetadata implements ResultSetMetaData {
3131
private final BigQueryJdbcResultSetLogger LOG =
3232
BigQueryJdbcResultSetLogger.getLogger(this.getClass());
3333
private final FieldList schemaFieldList;
34-
private final Statement statement;
34+
private final BigQueryStatement statement;
3535
private final int columnCount;
36+
private final boolean enableTimestampPicos;
3637

3738
private static final int DEFAULT_DISPLAY_SIZE = 50;
3839

39-
private BigQueryResultSetMetadata(FieldList schemaFieldList, Statement statement) {
40+
private BigQueryResultSetMetadata(FieldList schemaFieldList, BigQueryStatement statement) {
4041
LOG.finestTrace("<init>");
4142
this.schemaFieldList = schemaFieldList;
4243
this.columnCount = schemaFieldList.size();
4344
this.statement = statement;
45+
this.enableTimestampPicos = statement != null && statement.isEnableTimestampPicos();
4446
}
4547

46-
static BigQueryResultSetMetadata of(FieldList schemaFieldList, Statement statement) {
48+
static BigQueryResultSetMetadata of(FieldList schemaFieldList, BigQueryStatement statement) {
4749
return new BigQueryResultSetMetadata(schemaFieldList, statement);
4850
}
4951

50-
Statement getStatement() {
52+
BigQueryStatement getStatement() {
5153
return this.statement;
5254
}
5355

5456
private Field getField(int sqlColumn) {
5557
return this.schemaFieldList.get(sqlColumn - 1);
5658
}
5759

60+
private boolean isPicosecondTimestampColumn(int sqlColumn) {
61+
if (!this.enableTimestampPicos) {
62+
return false;
63+
}
64+
Field field = getField(sqlColumn);
65+
return field.getMode() != Mode.REPEATED && BigQueryTemporalUtility.isPicosecondTimestamp(field);
66+
}
67+
5868
@Override
5969
public int getColumnCount() {
6070
return this.columnCount;
@@ -104,6 +114,9 @@ public boolean isSigned(int column) {
104114

105115
@Override
106116
public int getColumnDisplaySize(int column) {
117+
if (isPicosecondTimestampColumn(column)) {
118+
return 32;
119+
}
107120
int colType = getColumnType(column);
108121
switch (colType) {
109122
case Types.BOOLEAN:
@@ -116,7 +129,7 @@ public int getColumnDisplaySize(int column) {
116129
case Types.NUMERIC:
117130
return 14;
118131
case Types.TIMESTAMP:
119-
return 16;
132+
return 26;
120133
default:
121134
return DEFAULT_DISPLAY_SIZE;
122135
}
@@ -138,6 +151,9 @@ public int getPrecision(int column) {
138151
if (precision != null) {
139152
return precision.intValue();
140153
}
154+
if (isPicosecondTimestampColumn(column)) {
155+
return 32;
156+
}
141157
StandardSQLTypeName type = getStandardSQLTypeName(column);
142158
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(type);
143159
if (typeInfo != null && typeInfo.columnSize != null) {
@@ -152,6 +168,9 @@ public int getScale(int column) {
152168
if (scale != null) {
153169
return scale.intValue();
154170
}
171+
if (isPicosecondTimestampColumn(column)) {
172+
return 12;
173+
}
155174
StandardSQLTypeName type = getStandardSQLTypeName(column);
156175
BigQueryTypeRegistry.ColumnTypeInfo typeInfo = BigQueryTypeRegistry.getColumnTypeInfo(type);
157176
if (typeInfo != null && typeInfo.decimalDigits != null) {
@@ -187,11 +206,17 @@ private StandardSQLTypeName getStandardSQLTypeName(int column) {
187206

188207
@Override
189208
public int getColumnType(int column) {
209+
if (isPicosecondTimestampColumn(column)) {
210+
return Types.VARCHAR;
211+
}
190212
return BigQueryTypeRegistry.toJdbcType(getStandardSQLTypeName(column));
191213
}
192214

193215
@Override
194216
public String getColumnTypeName(int column) {
217+
if (isPicosecondTimestampColumn(column)) {
218+
return BigQueryTemporalUtility.TIMESTAMP_PICOSECONDS_TYPE_NAME;
219+
}
195220
return getStandardSQLTypeName(column).name();
196221
}
197222

@@ -212,9 +237,12 @@ public boolean isDefinitelyWritable(int column) {
212237

213238
@Override
214239
public String getColumnClassName(int column) throws SQLException {
240+
if (isPicosecondTimestampColumn(column)) {
241+
return String.class.getName();
242+
}
215243
Field field = getField(column);
216244
if (field.getMode() == Mode.REPEATED) {
217-
return java.sql.Array.class.getName();
245+
return Array.class.getName();
218246
}
219247
return BigQueryTypeRegistry.toJavaClass(getStandardSQLTypeName(column)).getName();
220248
}
Collapse file

‎java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTemporalUtility.java‎

Copy file name to clipboardExpand all lines: java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTemporalUtility.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ final class BigQueryTemporalUtility {
4949
private static final BigDecimal MICROS_PER_SECOND = new BigDecimal("1000000");
5050
private static final DateTimeFormatter BASE_FORMATTER =
5151
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.");
52+
static final String TIMESTAMP_PICOSECONDS_TYPE_NAME = "TIMESTAMP_PICOSECONDS";
5253

5354
private BigQueryTemporalUtility() {}
5455

Collapse file

‎java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaDataTest.java‎

Copy file name to clipboardExpand all lines: java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaDataTest.java
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,67 @@ public void testMapBigQueryTypeToJdbc_ArrayType() {
460460
assertNull(infoArray.numPrecRadix);
461461
}
462462

463+
@Test
464+
public void testMapBigQueryTypeToJdbc_timestampPicos_whenEnabled() {
465+
when(bigQueryConnection.isEnableTimestampPicos()).thenReturn(true);
466+
Field fieldTimestampPicos =
467+
Field.newBuilder("picos_ts", StandardSQLTypeName.TIMESTAMP)
468+
.setTimestampPrecision(12L)
469+
.build();
470+
ColumnTypeInfo info = dbMetadata.mapBigQueryTypeToJdbc(fieldTimestampPicos);
471+
assertEquals(Types.VARCHAR, info.jdbcType);
472+
assertEquals(BigQueryTemporalUtility.TIMESTAMP_PICOSECONDS_TYPE_NAME, info.typeName);
473+
assertEquals(Integer.valueOf(32), info.columnSize);
474+
assertEquals(Integer.valueOf(12), info.decimalDigits);
475+
assertNull(info.numPrecRadix);
476+
}
477+
478+
@Test
479+
public void testMapBigQueryTypeToJdbc_timestampPicos_whenDisabled() {
480+
when(bigQueryConnection.isEnableTimestampPicos()).thenReturn(false);
481+
Field fieldTimestampPicos =
482+
Field.newBuilder("picos_ts", StandardSQLTypeName.TIMESTAMP)
483+
.setTimestampPrecision(12L)
484+
.build();
485+
ColumnTypeInfo info = dbMetadata.mapBigQueryTypeToJdbc(fieldTimestampPicos);
486+
assertEquals(Types.TIMESTAMP, info.jdbcType);
487+
assertEquals("TIMESTAMP", info.typeName);
488+
assertEquals(Integer.valueOf(26), info.columnSize);
489+
assertEquals(Integer.valueOf(6), info.decimalDigits);
490+
assertNull(info.numPrecRadix);
491+
}
492+
493+
@Test
494+
public void testMapBigQueryTypeToJdbc_repeatedTimestampPicos_remainsArray() {
495+
when(bigQueryConnection.isEnableTimestampPicos()).thenReturn(true);
496+
Field fieldRepeatedTimestampPicos =
497+
Field.newBuilder("picos_array", StandardSQLTypeName.TIMESTAMP)
498+
.setMode(Field.Mode.REPEATED)
499+
.setTimestampPrecision(12L)
500+
.build();
501+
ColumnTypeInfo info = dbMetadata.mapBigQueryTypeToJdbc(fieldRepeatedTimestampPicos);
502+
assertEquals(Types.ARRAY, info.jdbcType);
503+
assertEquals("ARRAY", info.typeName);
504+
assertNull(info.columnSize);
505+
assertNull(info.decimalDigits);
506+
assertNull(info.numPrecRadix);
507+
}
508+
509+
@Test
510+
public void testMapBigQueryTypeToJdbc_standardTimestamp_whenPicosEnabled() {
511+
when(bigQueryConnection.isEnableTimestampPicos()).thenReturn(true);
512+
Field fieldStandardTimestamp =
513+
Field.newBuilder("standard_ts", StandardSQLTypeName.TIMESTAMP)
514+
.setTimestampPrecision(6L)
515+
.build();
516+
ColumnTypeInfo info = dbMetadata.mapBigQueryTypeToJdbc(fieldStandardTimestamp);
517+
assertEquals(Types.TIMESTAMP, info.jdbcType);
518+
assertEquals("TIMESTAMP", info.typeName);
519+
assertEquals(Integer.valueOf(26), info.columnSize);
520+
assertEquals(Integer.valueOf(6), info.decimalDigits);
521+
assertNull(info.numPrecRadix);
522+
}
523+
463524
@Test
464525
public void testCreateColumnRow() {
465526
Field realField =
Collapse file

‎java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryResultSetMetadataTest.java‎

Copy file name to clipboardExpand all lines: java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryResultSetMetadataTest.java
+82-1Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
2121
import static org.junit.jupiter.params.provider.Arguments.arguments;
2222
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.when;
2324

2425
import com.google.cloud.bigquery.Field;
2526
import com.google.cloud.bigquery.FieldList;
@@ -30,6 +31,7 @@
3031
import java.sql.Array;
3132
import java.sql.ResultSetMetaData;
3233
import java.sql.SQLException;
34+
import java.sql.Timestamp;
3335
import java.sql.Types;
3436
import java.util.concurrent.Future;
3537
import java.util.stream.Stream;
@@ -196,7 +198,7 @@ public void testColumnDisplaySize() throws SQLException {
196198
assertThat(resultSetMetaData.getColumnDisplaySize(2)).isEqualTo(10);
197199
assertThat(resultSetMetaData.getColumnDisplaySize(3)).isEqualTo(14);
198200
assertThat(resultSetMetaData.getColumnDisplaySize(12)).isEqualTo(50);
199-
assertThat(resultSetMetaData.getColumnDisplaySize(5)).isEqualTo(16);
201+
assertThat(resultSetMetaData.getColumnDisplaySize(5)).isEqualTo(26);
200202
}
201203

202204
// Nested Types
@@ -295,4 +297,83 @@ public void testIsSearchableForAllTypes(StandardSQLTypeName type) throws SQLExce
295297
ResultSetMetaData metaData = resultSet.getMetaData();
296298
assertThat(metaData.isSearchable(1)).isTrue();
297299
}
300+
301+
@Test
302+
public void testTimestampPicosecondsMetadata_whenEnabled() throws SQLException {
303+
Field picosTimestampField =
304+
Field.newBuilder("picosTs", StandardSQLTypeName.TIMESTAMP)
305+
.setTimestampPrecision(12L)
306+
.build();
307+
Schema schema = Schema.of(FieldList.of(picosTimestampField));
308+
when(statement.isEnableTimestampPicos()).thenReturn(true);
309+
BigQueryJsonResultSet jsonRs =
310+
BigQueryJsonResultSet.of(schema, 1L, null, statement, (Future<?>[]) null);
311+
ResultSetMetaData metadata = jsonRs.getMetaData();
312+
313+
assertThat(metadata.getColumnType(1)).isEqualTo(Types.VARCHAR);
314+
assertThat(metadata.getColumnTypeName(1))
315+
.isEqualTo(BigQueryTemporalUtility.TIMESTAMP_PICOSECONDS_TYPE_NAME);
316+
assertThat(metadata.getColumnClassName(1)).isEqualTo(String.class.getName());
317+
assertThat(metadata.getColumnDisplaySize(1)).isEqualTo(32);
318+
assertThat(metadata.getPrecision(1)).isEqualTo(32);
319+
assertThat(metadata.getScale(1)).isEqualTo(12);
320+
}
321+
322+
@Test
323+
public void testTimestampPicosecondsMetadata_whenDisabled() throws SQLException {
324+
Field picosTimestampField =
325+
Field.newBuilder("picosTs", StandardSQLTypeName.TIMESTAMP)
326+
.setTimestampPrecision(12L)
327+
.build();
328+
Schema schema = Schema.of(FieldList.of(picosTimestampField));
329+
when(statement.isEnableTimestampPicos()).thenReturn(false);
330+
BigQueryJsonResultSet jsonRs =
331+
BigQueryJsonResultSet.of(schema, 1L, null, statement, (Future<?>[]) null);
332+
ResultSetMetaData metadata = jsonRs.getMetaData();
333+
334+
assertThat(metadata.getColumnType(1)).isEqualTo(Types.TIMESTAMP);
335+
assertThat(metadata.getColumnTypeName(1)).isEqualTo("TIMESTAMP");
336+
assertThat(metadata.getColumnClassName(1)).isEqualTo(Timestamp.class.getName());
337+
assertThat(metadata.getColumnDisplaySize(1)).isEqualTo(26);
338+
assertThat(metadata.getPrecision(1)).isEqualTo(26);
339+
assertThat(metadata.getScale(1)).isEqualTo(6);
340+
}
341+
342+
@Test
343+
public void testRepeatedTimestampPicosecondsMetadata_remainsArray() throws SQLException {
344+
Field repeatedPicosTimestampField =
345+
Field.newBuilder("picosArray", StandardSQLTypeName.TIMESTAMP)
346+
.setMode(Field.Mode.REPEATED)
347+
.setTimestampPrecision(12L)
348+
.build();
349+
Schema schema = Schema.of(FieldList.of(repeatedPicosTimestampField));
350+
when(statement.isEnableTimestampPicos()).thenReturn(true);
351+
BigQueryJsonResultSet jsonRs =
352+
BigQueryJsonResultSet.of(schema, 1L, null, statement, (Future<?>[]) null);
353+
ResultSetMetaData metadata = jsonRs.getMetaData();
354+
355+
assertThat(metadata.getColumnType(1)).isEqualTo(Types.ARRAY);
356+
assertThat(metadata.getColumnTypeName(1)).isEqualTo("ARRAY");
357+
assertThat(metadata.getColumnClassName(1)).isEqualTo(Array.class.getName());
358+
}
359+
360+
@Test
361+
public void testStandardTimestampMetadata_whenPicosEnabled() throws SQLException {
362+
Field standardTimestampField =
363+
Field.newBuilder("standardTs", StandardSQLTypeName.TIMESTAMP)
364+
.setTimestampPrecision(6L)
365+
.build();
366+
Schema schema = Schema.of(FieldList.of(standardTimestampField));
367+
when(statement.isEnableTimestampPicos()).thenReturn(true);
368+
BigQueryJsonResultSet jsonRs =
369+
BigQueryJsonResultSet.of(schema, 1L, null, statement, (Future<?>[]) null);
370+
ResultSetMetaData metadata = jsonRs.getMetaData();
371+
372+
assertThat(metadata.getColumnType(1)).isEqualTo(Types.TIMESTAMP);
373+
assertThat(metadata.getColumnTypeName(1)).isEqualTo("TIMESTAMP");
374+
assertThat(metadata.getColumnClassName(1)).isEqualTo(Timestamp.class.getName());
375+
assertThat(metadata.getColumnDisplaySize(1)).isEqualTo(26);
376+
assertThat(metadata.getPrecision(1)).isEqualTo(26);
377+
assertThat(metadata.getScale(1)).isEqualTo(6);
378+
}
298379
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.