-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(bigquery-jdbc): support picosecond in REST JSON path and nested types #14334
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,14 +33,20 @@ | |
| class BigQueryJsonArray extends BigQueryBaseArray { | ||
|
|
||
| private List<FieldValue> values; | ||
| private final boolean enableTimestampPicos; | ||
|
|
||
| BigQueryJsonArray(Field schema, FieldValue values) { | ||
| this(schema, values, BigQueryJdbcResultSetLogger.getLogger(BigQueryJsonArray.class)); | ||
| this(schema, values, BigQueryJdbcResultSetLogger.getLogger(BigQueryJsonArray.class), false); | ||
| } | ||
|
|
||
| BigQueryJsonArray(Field schema, FieldValue values, BigQueryJdbcResultSetLogger log) { | ||
| BigQueryJsonArray( | ||
| Field schema, | ||
| FieldValue values, | ||
| BigQueryJdbcResultSetLogger log, | ||
| boolean enableTimestampPicos) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep logger as last parameter |
||
| super(schema, log); | ||
| this.values = (values == null || values.isNull()) ? null : values.getRepeatedValue(); | ||
| this.enableTimestampPicos = enableTimestampPicos; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -74,7 +80,11 @@ public ResultSet getResultSet() { | |
| BigQueryFieldValueListWrapper bigQueryFieldValueListWrapper = | ||
| getNestedFieldValueListWrapper(FieldList.of(singleElementSchema()), this.values); | ||
| return BigQueryJsonResultSet.getNestedResultSet( | ||
| Schema.of(this.schema), bigQueryFieldValueListWrapper, 0, this.values.size()); | ||
| Schema.of(this.schema), | ||
| bigQueryFieldValueListWrapper, | ||
| 0, | ||
| this.values.size(), | ||
| this.enableTimestampPicos); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -88,7 +98,11 @@ public ResultSet getResultSet(long index, int count) { | |
| BigQueryFieldValueListWrapper bigQueryFieldValueListWrapper = | ||
| getNestedFieldValueListWrapper(FieldList.of(singleElementSchema()), this.values); | ||
| return BigQueryJsonResultSet.getNestedResultSet( | ||
| Schema.of(this.schema), bigQueryFieldValueListWrapper, range.x(), range.y()); | ||
| Schema.of(this.schema), | ||
| bigQueryFieldValueListWrapper, | ||
| range.x(), | ||
| range.y(), | ||
| this.enableTimestampPicos); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -97,12 +111,35 @@ public void free() { | |
| markInvalid(); | ||
| } | ||
|
|
||
| @Override | ||
| protected Class<?> getTargetClass() { | ||
| LOG.finestTrace("getTargetClass"); | ||
| if (this.enableTimestampPicos && BigQueryTemporalUtility.isPicosecondTimestamp(this.schema)) { | ||
| return String.class; | ||
| } | ||
| return super.getTargetClass(); | ||
| } | ||
|
|
||
| @Override | ||
| Object getCoercedValue(int index) throws SQLException { | ||
| LOG.finestTrace("getCoercedValue"); | ||
| FieldValue fieldValue = this.values.get(index); | ||
| return this.arrayOfStruct | ||
| ? new BigQueryJsonStruct( | ||
| this.schema.getSubFields(), fieldValue, this.LOG.getJsonStructLogger()) | ||
| : BigQueryTypeRegistry.convert(fieldValue, this.schema.getType().getStandardType(), null); | ||
| if (fieldValue == null || fieldValue.isNull()) { | ||
| return null; | ||
| } | ||
| if (this.arrayOfStruct) { | ||
| return new BigQueryJsonStruct( | ||
| this.schema.getSubFields(), | ||
| fieldValue, | ||
| this.LOG.getJsonStructLogger(), | ||
| this.enableTimestampPicos); | ||
| } | ||
| if (this.enableTimestampPicos && BigQueryTemporalUtility.isPicosecondTimestamp(this.schema)) { | ||
| return BigQueryTemporalUtility.formatTimestampValue(fieldValue.getStringValue(), true); | ||
| } | ||
| if (this.enableTimestampPicos && BigQueryJsonResultSet.isRangeTimestamp(this.schema)) { | ||
| return BigQueryJsonResultSet.formatRangeTimestamp(fieldValue); | ||
| } | ||
| return BigQueryTypeRegistry.convert(fieldValue, this.schema.getType().getStandardType(), null); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,9 @@ | |
| import com.google.cloud.bigquery.FieldValue; | ||
| import com.google.cloud.bigquery.FieldValue.Attribute; | ||
| import com.google.cloud.bigquery.Job; | ||
| import com.google.cloud.bigquery.Range; | ||
| import com.google.cloud.bigquery.Schema; | ||
| import com.google.cloud.bigquery.StandardSQLTypeName; | ||
| import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
@@ -45,6 +47,7 @@ class BigQueryJsonResultSet extends BigQueryBaseResultSet { | |
| private final int fromIndex; | ||
| private final int toIndexExclusive; | ||
| private final Future<?>[] ownedTasks; | ||
| private final boolean enableTimestampPicos; | ||
|
|
||
| private BigQueryJsonResultSet( | ||
| Schema schema, | ||
|
|
@@ -57,7 +60,8 @@ private BigQueryJsonResultSet( | |
| int toIndexExclusive, | ||
| Future<?>[] ownedTasks, | ||
| BigQuery bigQuery, | ||
| Job job) { | ||
| Job job, | ||
| boolean enableTimestampPicos) { | ||
| super(bigQuery, statement, schema, isNested, job); | ||
| this.totalRows = totalRows; | ||
| this.buffer = buffer; | ||
|
|
@@ -66,6 +70,8 @@ private BigQueryJsonResultSet( | |
| this.toIndexExclusive = toIndexExclusive; | ||
| this.nestedRowIndex = fromIndex - 1; | ||
| this.ownedTasks = ownedTasks; | ||
| this.enableTimestampPicos = | ||
| statement != null ? statement.isEnableTimestampPicos() : enableTimestampPicos; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -95,7 +101,18 @@ static BigQueryJsonResultSet of( | |
| Job job) { | ||
|
|
||
| return new BigQueryJsonResultSet( | ||
| schema, totalRows, buffer, statement, false, null, -1, -1, ownedTasks, bigQuery, job); | ||
| schema, | ||
| totalRows, | ||
| buffer, | ||
| statement, | ||
| false, | ||
| null, | ||
| -1, | ||
| -1, | ||
| ownedTasks, | ||
| bigQuery, | ||
| job, | ||
| false); | ||
| } | ||
|
|
||
| static BigQueryJsonResultSet of( | ||
|
|
@@ -106,7 +123,7 @@ static BigQueryJsonResultSet of( | |
| Future<?>[] ownedTasks) { | ||
|
|
||
| return new BigQueryJsonResultSet( | ||
| schema, totalRows, buffer, statement, false, null, -1, -1, ownedTasks, null, null); | ||
| schema, totalRows, buffer, statement, false, null, -1, -1, ownedTasks, null, null, false); | ||
| } | ||
|
|
||
| static BigQueryJsonResultSet of( | ||
|
|
@@ -133,6 +150,7 @@ static BigQueryJsonResultSet of( | |
| fromIndex = 0; | ||
| ownedTasks = new Future<?>[0]; | ||
| toIndexExclusive = 0; | ||
| this.enableTimestampPicos = false; | ||
| } | ||
|
|
||
| // | ||
|
|
@@ -145,10 +163,15 @@ static BigQueryJsonResultSet of( | |
| * @param cursor Points to the current record | ||
| * @param fromIndex starting index under consideration | ||
| * @param toIndexExclusive last index under consideration | ||
| * @param enableTimestampPicos whether picosecond timestamp precision is enabled | ||
| * @return The BigQueryJsonResultSet | ||
| */ | ||
| static BigQueryJsonResultSet getNestedResultSet( | ||
| Schema schema, BigQueryFieldValueListWrapper cursor, int fromIndex, int toIndexExclusive) { | ||
| Schema schema, | ||
| BigQueryFieldValueListWrapper cursor, | ||
| int fromIndex, | ||
| int toIndexExclusive, | ||
| boolean enableTimestampPicos) { | ||
| return new BigQueryJsonResultSet( | ||
| schema, | ||
| -1, | ||
|
|
@@ -160,7 +183,8 @@ static BigQueryJsonResultSet getNestedResultSet( | |
| toIndexExclusive, | ||
| null, | ||
| null, | ||
| null); | ||
| null, | ||
| enableTimestampPicos); | ||
| } | ||
|
|
||
| /* Advances the result set to the next row, returning false if no such row exists. Potentially blocking operation */ | ||
|
|
@@ -232,21 +256,60 @@ public Object getObject(int columnIndex) throws SQLException { | |
| Field arrayField = this.schema.getFields().get(0); | ||
| if (isStruct(arrayField)) { | ||
| return new BigQueryJsonStruct( | ||
| arrayField.getSubFields(), value, this.LOG.getJsonStructLogger()); | ||
| arrayField.getSubFields(), | ||
| value, | ||
| this.LOG.getJsonStructLogger(), | ||
| this.enableTimestampPicos); | ||
| } | ||
| if (this.enableTimestampPicos && BigQueryTemporalUtility.isPicosecondTimestamp(arrayField)) { | ||
| return BigQueryTemporalUtility.formatTimestampValue(value.getStringValue(), true); | ||
| } | ||
| if (this.enableTimestampPicos && isRangeTimestamp(arrayField)) { | ||
| return formatRangeTimestamp(value); | ||
| } | ||
| return BigQueryTypeRegistry.convert(value, arrayField.getType().getStandardType(), null); | ||
| } | ||
|
|
||
| int extraIndex = this.isNested ? 2 : 1; | ||
| Field fieldSchema = this.schemaFieldList.get(columnIndex - extraIndex); | ||
| Field fieldSchema = this.schemaFieldList.get(columnIndex - 1); | ||
| if (isArray(fieldSchema)) { | ||
| return new BigQueryJsonArray(fieldSchema, value, this.LOG.getJsonArrayLogger()); | ||
| } else if (isStruct(fieldSchema)) { | ||
| return new BigQueryJsonArray( | ||
| fieldSchema, value, this.LOG.getJsonArrayLogger(), this.enableTimestampPicos); | ||
| } | ||
| if (isStruct(fieldSchema)) { | ||
| return new BigQueryJsonStruct( | ||
| fieldSchema.getSubFields(), value, this.LOG.getJsonStructLogger()); | ||
| } else { | ||
| return BigQueryTypeRegistry.convert(value, fieldSchema.getType().getStandardType(), null); | ||
| fieldSchema.getSubFields(), | ||
| value, | ||
| this.LOG.getJsonStructLogger(), | ||
| this.enableTimestampPicos); | ||
| } | ||
| if (this.enableTimestampPicos && isRangeTimestamp(fieldSchema)) { | ||
| return formatRangeTimestamp(value); | ||
| } | ||
| if (this.enableTimestampPicos && BigQueryTemporalUtility.isPicosecondTimestamp(fieldSchema)) { | ||
| return BigQueryTemporalUtility.formatTimestampValue(value.getStringValue(), true); | ||
| } | ||
| return BigQueryTypeRegistry.convert(value, fieldSchema.getType().getStandardType(), null); | ||
| } | ||
|
|
||
| static String formatRangeTimestamp(FieldValue value) throws SQLException { | ||
| Range range = value.getRangeValue(); | ||
| String start = | ||
| range.getStart().isNull() | ||
| ? "UNBOUNDED" | ||
| : BigQueryTemporalUtility.formatTimestampValue(range.getStart().getStringValue(), true); | ||
| String end = | ||
| range.getEnd().isNull() | ||
| ? "UNBOUNDED" | ||
| : BigQueryTemporalUtility.formatTimestampValue(range.getEnd().getStringValue(), true); | ||
| return String.format("[%s, %s)", start, end); | ||
| } | ||
|
|
||
| static boolean isRangeTimestamp(Field field) { | ||
| return field != null | ||
| && field.getRangeElementType() != null | ||
| && StandardSQLTypeName.TIMESTAMP | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd reverse the order; Rather than compare string to |
||
| .name() | ||
| .equalsIgnoreCase(field.getRangeElementType().getType()); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.