From 76207a3672329359f13b8d626bc856c5706ee413 Mon Sep 17 00:00:00 2001 From: Neenu1995 Date: Thu, 19 Nov 2026 19:38:38 -0500 Subject: [PATCH] fix(bigquery-jdbc): unify Storage Write API and SQL parameter conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stack: PR 2 of 3 (base: `fix/swa-1-writer-lifecycle`, stacked on #15084) ### Summary - Fix locale-dependent / misquoted Storage Write API serialization (`b/587340667`) where `createJsonRow()` used default `Gson` formatting for non-string values. - Reuse `BigQueryTypeRegistry` (`isSupportedWriteApiField`, `isSupportedJavaValue`, `extractInstant`) and `BigQueryParameterHandler` (`toQueryParameterValue`, `createWriteApiJsonRow`) across both SQL parameter binding and Storage Write API row serialization (`Locale.ROOT` / UTC `DATE`, `TIME`, `DATETIME`, `TIMESTAMP(6)` / `TIMESTAMP(12)`, `byte[]` `JsonArray`, and plain `BigDecimal` strings). - Validate batch parameter compatibility (`areBatchParametersCompatibleWithWriteApi`)—including `REQUIRED` nullability, `NUMERIC(P, S)` / `BIGNUMERIC(P, S)` bounds, finite `FLOAT64`, and strict `0001..9999` calendar/timestamp ranges—falling back to standard SQL DML when SQL-side coercion is required. ### Testing - `make lint`, `mvn -B -ntp verify -DskipTests -DskipITs`, `mvn -q -Denforcer.skip=true -Dclirr.skip=true test-compile`, and `make unittest` (77 targeted tests passing; full suite 1305/1306 non-skipped passing, 1 pre-existing `BigQueryJdbcOAuthUtilityTest` P12 env failure; ITs require GCP credentials). --- .../jdbc/BigQueryParameterHandler.java | 324 ++++++++++++++++-- .../jdbc/BigQueryPreparedStatement.java | 56 +-- .../bigquery/jdbc/BigQueryTypeRegistry.java | 62 ++++ .../jdbc/BigQueryParameterHandlerTest.java | 133 +++++++ ...BigQueryPreparedStatementWriteApiTest.java | 277 +++++++++++++++ .../jdbc/BigQueryTypeRegistryTest.java | 49 +++ 6 files changed, 841 insertions(+), 60 deletions(-) diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java index 2fc89afbf173..d0764dbaae00 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java @@ -17,6 +17,8 @@ package com.google.cloud.bigquery.jdbc; import com.google.cloud.Tuple; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.FieldList; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; import com.google.cloud.bigquery.StandardSQLTypeName; @@ -24,6 +26,10 @@ import com.google.cloud.bigquery.exception.BigQueryJdbcSqlFeatureNotSupportedException; import com.google.cloud.bigquery.jdbc.telemetry.v1.DriverFeature; import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager; +import com.google.gson.JsonArray; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Date; import java.sql.SQLException; @@ -33,12 +39,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.time.format.ResolverStyle; import java.util.ArrayList; +import java.util.List; import java.util.Locale; +import java.util.Queue; class BigQueryParameterHandler { private static final BigQueryJdbcCustomLogger LOG = @@ -54,13 +62,25 @@ class BigQueryParameterHandler { DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSXXX", Locale.ROOT); private static final DateTimeFormatter TIMESTAMP_NANOS_UTC = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSSSSXXX", Locale.ROOT); + private static final DateTimeFormatter TIMESTAMP_PICOS_UTC = + DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSSSS'000'XXX", Locale.ROOT); // DATETIME is a zone-less wall clock; the validator requires six fraction digits and no offset. private static final DateTimeFormatter DATETIME_MICROS = - DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS", Locale.ROOT); + DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSS", Locale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter DATE_DIGITS = - DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ROOT); + DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.ROOT) + .withResolverStyle(ResolverStyle.STRICT); private static final DateTimeFormatter TIME_MICROS = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS", Locale.ROOT); + private static final BigDecimal NUMERIC_MAX = + new BigDecimal("99999999999999999999999999999.999999999"); + private static final BigDecimal BIGNUMERIC_MAX = + new BigDecimal( + "578960446186580977117854925043439539266.34992332820282019728792003956564819967"); + private static final Instant MIN_WRITE_API_INSTANT = Instant.parse("0001-01-01T00:00:00Z"); + private static final Instant MAX_WRITE_API_INSTANT = + Instant.parse("9999-12-31T23:59:59.999999999Z"); /** Wall-clock digits for DATETIME (read as UTC if sent to TIMESTAMP). Nanos are truncated. */ static String formatDateTimeDigits(LocalDateTime value) { @@ -83,14 +103,9 @@ static Tuple> bindingForJavaTime(Object value) { if (value instanceof LocalDateTime) { return Tuple.of(formatDateTimeDigits((LocalDateTime) value), String.class); } - if (value instanceof OffsetDateTime) { - return Tuple.of(Timestamp.from(((OffsetDateTime) value).toInstant()), Timestamp.class); - } - if (value instanceof ZonedDateTime) { - return Tuple.of(Timestamp.from(((ZonedDateTime) value).toInstant()), Timestamp.class); - } - if (value instanceof Instant) { - return Tuple.of(Timestamp.from((Instant) value), Timestamp.class); + Instant instant = BigQueryTypeRegistry.extractInstant(value); + if (instant != null && !(value instanceof Timestamp)) { + return Tuple.of(Timestamp.from(instant), Timestamp.class); } return null; } @@ -126,11 +141,10 @@ QueryJobConfiguration.Builder configureParameters( QueryJobConfiguration.Builder jobConfigurationBuilder) throws SQLException { LOG.finest("++enter++"); for (int i = 1; i <= this.parametersArraySize; i++) { - int arrayIndex = i - 1; - if (this.parametersList.size() <= arrayIndex - || this.parametersList.get(arrayIndex) == null - || !this.parametersList.get(arrayIndex).isBound()) { + BigQueryJdbcParameter parameter = + arrayIndex < this.parametersList.size() ? this.parametersList.get(arrayIndex) : null; + if (parameter == null || !parameter.isBound()) { throw new BigQueryJdbcException( String.format( "One or more parameters missing in Prepared statement. No value was set for" @@ -138,19 +152,24 @@ QueryJobConfiguration.Builder configureParameters( i, this.parametersArraySize)); } - Object parameterValue = getParameter(i); - StandardSQLTypeName sqlType = getSqlType(i); - parameterValue = - formatValueForQueryParameter(parameterValue, sqlType, this.enableTimestampPicos); + QueryParameterValue queryParameter = + toQueryParameterValue(parameter, this.enableTimestampPicos); LOG.finest( "Parameter %s of type %s at index %s added to QueryJobConfiguration", - parameterValue, sqlType, i); - jobConfigurationBuilder.addPositionalParameter( - QueryParameterValue.of(parameterValue, sqlType)); + queryParameter.getValue(), queryParameter.getType(), i); + jobConfigurationBuilder.addPositionalParameter(queryParameter); } return jobConfigurationBuilder; } + static QueryParameterValue toQueryParameterValue( + BigQueryJdbcParameter parameter, boolean enableTimestampPicos) { + StandardSQLTypeName sqlType = parameter.getSqlType(); + Object parameterValue = + formatValueForQueryParameter(parameter.getValue(), sqlType, enableTimestampPicos); + return QueryParameterValue.of(parameterValue, sqlType); + } + static Object formatValueForQueryParameter( Object parameterValue, StandardSQLTypeName sqlType, boolean enableTimestampPicos) { if (parameterValue == null) { @@ -167,18 +186,9 @@ static Object formatValueForQueryParameter( } if (sqlType == StandardSQLTypeName.TIMESTAMP) { // Reduce to an Instant and render in UTC so digits and 'Z' can never disagree. - if (parameterValue instanceof Timestamp) { - return formatInstantUtc(((Timestamp) parameterValue).toInstant(), enableTimestampPicos); - } - if (parameterValue instanceof OffsetDateTime) { - return formatInstantUtc( - ((OffsetDateTime) parameterValue).toInstant(), enableTimestampPicos); - } - if (parameterValue instanceof ZonedDateTime) { - return formatInstantUtc(((ZonedDateTime) parameterValue).toInstant(), enableTimestampPicos); - } - if (parameterValue instanceof Instant) { - return formatInstantUtc((Instant) parameterValue, enableTimestampPicos); + Instant instant = BigQueryTypeRegistry.extractInstant(parameterValue); + if (instant != null) { + return formatInstantUtc(instant, enableTimestampPicos); } if (parameterValue instanceof String) { String str = ((String) parameterValue).trim(); @@ -487,4 +497,248 @@ StandardSQLTypeName getSqlType(String name) { int getParametersArraySize() { return this.parametersArraySize; } + + private static Object formatParameterValueForField( + Object value, + Field field, + StandardSQLTypeName fallbackSqlType, + boolean enableTimestampPicos) { + if (value == null) { + return null; + } + StandardSQLTypeName targetType = + field != null && field.getType() != null && field.getType().getStandardType() != null + ? field.getType().getStandardType() + : fallbackSqlType; + if (targetType == StandardSQLTypeName.TIMESTAMP) { + if (field != null + && enableTimestampPicos + && BigQueryTemporalUtility.isPicosecondTimestamp(field)) { + Instant instant = BigQueryTypeRegistry.extractInstant(value); + return instant != null + ? TIMESTAMP_PICOS_UTC.format(instant.atOffset(ZoneOffset.UTC)) + : formatValueForQueryParameter(value, targetType, true); + } + return formatValueForQueryParameter(value, targetType, field == null && enableTimestampPicos); + } + return formatValueForQueryParameter(value, targetType, enableTimestampPicos); + } + + static JsonObject createWriteApiJsonRow( + FieldList fieldLists, + List parameterList, + boolean enableTimestampPicos) { + JsonObject rowObject = new JsonObject(); + for (int j = 0; j < parameterList.size(); j++) { + Field field = fieldLists.get(j); + String fieldName = field.getName(); + BigQueryJdbcParameter parameter = parameterList.get(j); + Object formatted = + formatParameterValueForField( + parameter.getValue(), field, parameter.getSqlType(), enableTimestampPicos); + if (formatted == null) { + rowObject.add(fieldName, JsonNull.INSTANCE); + } else if (formatted instanceof byte[]) { + // JsonStreamWriter's Gson path parses via org.json.JSONArray; JsonToProtoMessage only + // accepts a JSON array of signed byte integers here (Base64 strings are rejected). + JsonArray byteArray = new JsonArray(); + for (byte b : (byte[]) formatted) { + byteArray.add(b); + } + rowObject.add(fieldName, byteArray); + } else if (formatted instanceof Boolean) { + rowObject.addProperty(fieldName, (Boolean) formatted); + } else if (formatted instanceof BigDecimal) { + BigDecimal bd = (BigDecimal) formatted; + rowObject.addProperty( + fieldName, bd.signum() == 0 ? "0" : bd.stripTrailingZeros().toPlainString()); + } else { + rowObject.addProperty(fieldName, formatted.toString()); + } + } + return rowObject; + } + + static boolean areBatchParametersCompatibleWithWriteApi( + FieldList fields, Queue> batchParameters) { + for (ArrayList parameterList : batchParameters) { + if (parameterList == null || parameterList.size() != fields.size()) { + return false; + } + for (int i = 0; i < fields.size(); i++) { + Field field = fields.get(i); + BigQueryJdbcParameter parameter = parameterList.get(i); + if (parameter == null || !parameter.isBound()) { + return false; + } + StandardSQLTypeName columnType = field.getType().getStandardType(); + Object value = parameter.getValue(); + StandardSQLTypeName paramSqlType = parameter.getSqlType(); + if (value == null) { + if (field.getMode() == Field.Mode.REQUIRED + || !isSqlTypeAssignableForWriteApi(paramSqlType, columnType)) { + return false; + } + } else if (!isValueAssignableForWriteApi(value, paramSqlType, field, columnType)) { + return false; + } + } + } + return true; + } + + private static boolean isSqlTypeAssignableForWriteApi( + StandardSQLTypeName paramType, StandardSQLTypeName columnType) { + if (paramType == null || columnType == null) { + return false; + } + if (paramType == columnType) { + return true; + } + switch (columnType) { + case NUMERIC: + return paramType == StandardSQLTypeName.INT64; + case BIGNUMERIC: + return paramType == StandardSQLTypeName.INT64 || paramType == StandardSQLTypeName.NUMERIC; + case FLOAT64: + return paramType == StandardSQLTypeName.INT64 + || paramType == StandardSQLTypeName.NUMERIC + || paramType == StandardSQLTypeName.BIGNUMERIC; + case DATETIME: + return paramType == StandardSQLTypeName.STRING; + default: + return false; + } + } + + private static boolean isValueAssignableForWriteApi( + Object value, StandardSQLTypeName paramSqlType, Field field, StandardSQLTypeName columnType) { + if (!isSqlTypeAssignableForWriteApi(paramSqlType, columnType)) { + return false; + } + switch (columnType) { + case STRING: + case BYTES: + case BOOL: + case INT64: + case TIME: + return BigQueryTypeRegistry.isSupportedJavaValue(columnType, value); + case NUMERIC: + case BIGNUMERIC: + StandardSQLTypeName effectiveType = + paramSqlType == StandardSQLTypeName.NUMERIC + || (paramSqlType == StandardSQLTypeName.INT64 + && columnType == StandardSQLTypeName.NUMERIC) + ? StandardSQLTypeName.NUMERIC + : StandardSQLTypeName.BIGNUMERIC; + BigDecimal maxMag = + effectiveType == StandardSQLTypeName.NUMERIC ? NUMERIC_MAX : BIGNUMERIC_MAX; + int maxScale = BigQueryTypeRegistry.getColumnTypeInfo(effectiveType).decimalDigits; + if (paramSqlType == StandardSQLTypeName.INT64) { + return BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, value) + && isValidWriteApiBigDecimal( + BigDecimal.valueOf(((Number) value).longValue()), + maxScale, + maxMag, + field.getPrecision(), + field.getScale()); + } + return BigQueryTypeRegistry.isSupportedJavaValue(columnType, value) + && isValidWriteApiBigDecimal( + (BigDecimal) value, maxScale, maxMag, field.getPrecision(), field.getScale()); + case FLOAT64: + if (paramSqlType == StandardSQLTypeName.FLOAT64) { + return BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.FLOAT64, value) + && Double.isFinite(((Number) value).doubleValue()); + } + if (paramSqlType == StandardSQLTypeName.INT64) { + return BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, value); + } + StandardSQLTypeName decType = + paramSqlType == StandardSQLTypeName.NUMERIC + ? StandardSQLTypeName.NUMERIC + : StandardSQLTypeName.BIGNUMERIC; + return BigQueryTypeRegistry.isSupportedJavaValue(paramSqlType, value) + && isValidWriteApiBigDecimal( + (BigDecimal) value, + BigQueryTypeRegistry.getColumnTypeInfo(decType).decimalDigits, + decType == StandardSQLTypeName.NUMERIC ? NUMERIC_MAX : BIGNUMERIC_MAX, + null, + null); + case DATE: + if (!BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.DATE, value)) { + return false; + } + if (value instanceof LocalDate) { + int year = ((LocalDate) value).getYear(); + return year >= 1 && year <= 9999; + } + String formattedDate = + formatValueForQueryParameter(value, StandardSQLTypeName.DATE, false).toString(); + if (formattedDate.length() != 10) { + return false; + } + try { + int year = LocalDate.parse(formattedDate, DATE_DIGITS).getYear(); + return year >= 1 && year <= 9999; + } catch (DateTimeParseException ex) { + return false; + } + case DATETIME: + if (paramSqlType == StandardSQLTypeName.DATETIME) { + return value instanceof LocalDateTime + && ((LocalDateTime) value).getYear() >= 1 + && ((LocalDateTime) value).getYear() <= 9999; + } + if (!(value instanceof String) || ((String) value).length() != 26) { + return false; + } + try { + int year = LocalDateTime.parse((String) value, DATETIME_MICROS).getYear(); + return year >= 1 && year <= 9999; + } catch (DateTimeParseException ex) { + return false; + } + case TIMESTAMP: + if (!BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.TIMESTAMP, value)) { + return false; + } + Instant instant = BigQueryTypeRegistry.extractInstant(value); + return instant != null + && !instant.isBefore(MIN_WRITE_API_INSTANT) + && !instant.isAfter(MAX_WRITE_API_INSTANT); + default: + return false; + } + } + + private static boolean isValidWriteApiBigDecimal( + BigDecimal value, + int maxScale, + BigDecimal maxMagnitude, + Long columnPrecision, + Long columnScale) { + // Check integer-digit count in long arithmetic before stripTrailingZeros() / compareTo() so + // extreme negative scales (e.g. BigDecimal.valueOf(10, Integer.MIN_VALUE)) cannot underflow or + // trigger huge scale-alignment allocations. + long intDigits = + value.signum() == 0 ? 0L : Math.max(0L, (long) value.precision() - (long) value.scale()); + if (intDigits > maxMagnitude.precision() - maxMagnitude.scale()) { + return false; + } + BigDecimal normalized = value.signum() == 0 ? BigDecimal.ZERO : value.stripTrailingZeros(); + if (normalized.scale() > maxScale) { + return false; + } + if (columnScale != null && normalized.scale() > columnScale) { + return false; + } + if (columnPrecision != null) { + long effectiveScale = columnScale != null ? columnScale : 0L; + if (normalized.scale() > effectiveScale || intDigits > columnPrecision - effectiveScale) { + return false; + } + } + return normalized.abs().compareTo(maxMagnitude) <= 0; + } } diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java index 8d079ba3ce2b..92bf77c3e333 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java @@ -19,6 +19,7 @@ import com.google.api.gax.retrying.RetrySettings; import com.google.api.services.bigquery.model.QueryParameter; import com.google.cloud.Tuple; +import com.google.cloud.bigquery.Field; import com.google.cloud.bigquery.FieldList; import com.google.cloud.bigquery.JobStatistics.QueryStatistics; import com.google.cloud.bigquery.QueryJobConfiguration; @@ -40,7 +41,6 @@ import com.google.cloud.bigquery.storage.v1.TableName; import com.google.gson.Gson; import com.google.gson.JsonArray; -import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.protobuf.Descriptors.DescriptorValidationException; import java.io.IOException; @@ -164,13 +164,20 @@ private static StandardSQLTypeName toStandardSqlType(String typeName) { // Remembers the table and schema of single-table INSERT targets, for the Storage Write API. // Failure is delegated to executeBatch instead of the constructor. private void captureInsertMetadata(QueryStatistics statistics) { + Schema schema = statistics.getSchema(); if (!QueryStatistics.StatementType.INSERT.equals(statistics.getStatementType()) - || statistics.getSchema() == null + || schema == null + || schema.getFields() == null || statistics.getReferencedTables() == null || statistics.getReferencedTables().stream().distinct().count() != 1) { return; } - this.insertSchema = statistics.getSchema(); + for (Field field : schema.getFields()) { + if (!BigQueryTypeRegistry.isSupportedWriteApiField(field)) { + return; + } + } + this.insertSchema = schema; TableId tableId = statistics.getReferencedTables().get(0); this.insertTableName = TableName.of(tableId.getProject(), tableId.getDataset(), tableId.getTable()); @@ -546,16 +553,16 @@ private long bulkInsertWithWriteAPI(BigQueryWriteClient bigQueryWriteClient) long offset = 0; JsonArray jsonArray = new JsonArray(); - Gson gson = new Gson(); FieldList fieldLists = this.insertSchema.getFields(); int count = this.batchParameters.size(); + boolean timestampPicos = this.isEnableTimestampPicos(); for (int i = 0; i < count; i++) { ArrayList parameterList = this.batchParameters.poll(); if (fieldLists.size() != parameterList.size()) { throw new BigQueryJdbcException("Mismatch between field count and parameter count."); } - JsonObject rowObject = createJsonRow(fieldLists, parameterList, gson); + JsonObject rowObject = createJsonRow(fieldLists, parameterList, timestampPicos); jsonArray.add(rowObject); if (jsonArray.size() == this.querySettings.getWriteAPIAppendRowCount() @@ -591,18 +598,15 @@ private long bulkInsertWithWriteAPI(BigQueryWriteClient bigQueryWriteClient) static JsonObject createJsonRow( FieldList fieldLists, List parameterList, Gson gson) { - JsonObject rowObject = new JsonObject(); - for (int j = 0; j < parameterList.size(); j++) { - BigQueryJdbcParameter parameter = parameterList.get(j); - if (parameter.getValue() == null) { - rowObject.add(fieldLists.get(j).getName(), JsonNull.INSTANCE); - } else if (parameter.getSqlType() == StandardSQLTypeName.STRING) { - rowObject.addProperty(fieldLists.get(j).getName(), parameter.getValue().toString()); - } else { - rowObject.addProperty(fieldLists.get(j).getName(), gson.toJson(parameter.getValue())); - } - } - return rowObject; + return createJsonRow(fieldLists, parameterList, false); + } + + static JsonObject createJsonRow( + FieldList fieldLists, + List parameterList, + boolean enableTimestampPicos) { + return BigQueryParameterHandler.createWriteApiJsonRow( + fieldLists, parameterList, enableTimestampPicos); } QueryJobConfiguration getWriteBatchJobConfiguration( @@ -622,19 +626,19 @@ QueryJobConfiguration getStandardBatchJobConfiguration(String query) throws SQLE QueryJobConfiguration.Builder jobConfiguration = getJobConfig(query); jobConfiguration.setParameterMode("POSITIONAL"); jobConfiguration.setPriority(QueryJobConfiguration.Priority.BATCH); + boolean enableTimestampPicos = this.isEnableTimestampPicos(); int index = 0; while (!this.batchParameters.isEmpty()) { ArrayList parameterList = this.batchParameters.poll(); - for (BigQueryJdbcParameter parameter : parameterList) { - Object parameterValue = - BigQueryParameterHandler.formatValueForQueryParameter( - parameter.getValue(), parameter.getSqlType(), this.isEnableTimestampPicos()); - StandardSQLTypeName sqlType = parameter.getSqlType(); + for (int i = 0; i < this.parameterCount; i++) { + BigQueryJdbcParameter parameter = parameterList.get(i); + QueryParameterValue queryParameter = + BigQueryParameterHandler.toQueryParameterValue(parameter, enableTimestampPicos); LOG.finer( "Parameter %s of type %s at index %s added to QueryJobConfiguration", - parameterValue, sqlType, index++); - jobConfiguration.addPositionalParameter(QueryParameterValue.of(parameterValue, sqlType)); + queryParameter.getValue(), queryParameter.getType(), index++); + jobConfiguration.addPositionalParameter(queryParameter); } } return jobConfiguration.build(); @@ -645,7 +649,9 @@ Boolean useWriteAPI() { return this.querySettings.isUseWriteAPI() // the constructor dry run already confirmed a single-table INSERT && this.insertSchema != null - && this.batchParameters.size() >= this.querySettings.getWriteAPIActivationRowCount(); + && this.batchParameters.size() >= this.querySettings.getWriteAPIActivationRowCount() + && BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + this.insertSchema.getFields(), this.batchParameters); } @Override diff --git a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistry.java b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistry.java index 45cb14f1d83f..f596dc5f2819 100644 --- a/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistry.java +++ b/java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistry.java @@ -16,6 +16,7 @@ package com.google.cloud.bigquery.jdbc; +import com.google.cloud.bigquery.Field; import com.google.cloud.bigquery.FieldValue; import com.google.cloud.bigquery.StandardSQLTypeName; import com.google.cloud.bigquery.exception.BigQueryJdbcException; @@ -41,7 +42,10 @@ import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Base64; +import java.util.Collections; +import java.util.EnumSet; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.apache.arrow.vector.PeriodDuration; @@ -54,6 +58,23 @@ final class BigQueryTypeRegistry { private static final TypeDescriptor[] DESCRIPTORS_BY_ORDINAL; private static final Map, TypeDescriptor> DESCRIPTORS_BY_CLASS; private static final Map> DESCRIPTORS_BY_JDBC_TYPE; + private static final Set SUPPORTED_WRITE_API_TYPES = + Collections.unmodifiableSet( + EnumSet.of( + StandardSQLTypeName.STRING, + StandardSQLTypeName.BYTES, + StandardSQLTypeName.INT64, + StandardSQLTypeName.FLOAT64, + StandardSQLTypeName.NUMERIC, + StandardSQLTypeName.BIGNUMERIC, + StandardSQLTypeName.BOOL, + StandardSQLTypeName.DATE, + StandardSQLTypeName.TIME, + StandardSQLTypeName.DATETIME, + StandardSQLTypeName.TIMESTAMP, + StandardSQLTypeName.JSON, + StandardSQLTypeName.GEOGRAPHY, + StandardSQLTypeName.INTERVAL)); private static final DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"); @@ -998,4 +1019,45 @@ StandardSQLTypeName.ARRAY, new ColumnTypeInfo(Types.ARRAY, "ARRAY", null, null, public static ColumnTypeInfo getColumnTypeInfo(StandardSQLTypeName bqType) { return STANDARD_TYPE_INFO.get(bqType); } + + static boolean isSupportedWriteApiField(Field field) { + return field != null + && field.getMode() != Field.Mode.REPEATED + && (field.getSubFields() == null || field.getSubFields().isEmpty()) + && field.getType() != null + && field.getType().getStandardType() != null + && SUPPORTED_WRITE_API_TYPES.contains(field.getType().getStandardType()); + } + + static boolean isSupportedJavaValue(StandardSQLTypeName bqType, Object value) { + if (bqType == null || value == null) { + return false; + } + int ordinal = bqType.ordinal(); + if (ordinal >= DESCRIPTORS_BY_ORDINAL.length || DESCRIPTORS_BY_ORDINAL[ordinal] == null) { + return false; + } + for (Class supported : DESCRIPTORS_BY_ORDINAL[ordinal].getSupportedJavaTypes()) { + if (supported.isInstance(value)) { + return true; + } + } + return false; + } + + static Instant extractInstant(Object value) { + if (value instanceof Instant) { + return (Instant) value; + } + if (value instanceof OffsetDateTime) { + return ((OffsetDateTime) value).toInstant(); + } + if (value instanceof ZonedDateTime) { + return ((ZonedDateTime) value).toInstant(); + } + if (value instanceof Timestamp) { + return ((Timestamp) value).toInstant(); + } + return null; + } } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandlerTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandlerTest.java index 496982f42683..7eb3d2d09c29 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandlerTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandlerTest.java @@ -17,20 +17,35 @@ package com.google.cloud.bigquery.jdbc; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import com.google.cloud.bigquery.Field; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; +import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.exception.BigQueryJdbcException; import com.google.cloud.bigquery.jdbc.BigQueryParameterHandler.BigQueryStatementParameterType; +import com.google.gson.JsonObject; +import java.math.BigDecimal; +import java.sql.Date; +import java.sql.Time; import java.sql.Timestamp; import java.time.Instant; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; import java.util.TimeZone; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -373,4 +388,122 @@ public void testFormatDateTimeDigits_isZoneFreeAndFixedWidth(String zone) { BigQueryParameterHandler.formatDateTimeDigits( LocalDateTime.of(2025, 1, 1, 12, 0, 0, 123_456_789))); } + + @Test + public void testDoor1AndDoor2ShareConversionAndStrictCalendarValidation() { + Schema schema = + Schema.of( + Field.of("s_int", StandardSQLTypeName.INT64), + Field.of("b_int", StandardSQLTypeName.INT64), + Field.of("f_col", StandardSQLTypeName.FLOAT64), + Field.of("d_col", StandardSQLTypeName.DATE), + Field.of("t_col", StandardSQLTypeName.TIME), + Field.of("dt_col", StandardSQLTypeName.DATETIME), + Field.of("ts6_col", StandardSQLTypeName.TIMESTAMP), + Field.newBuilder("ts12_col", StandardSQLTypeName.TIMESTAMP) + .setTimestampPrecision(12L) + .build()); + + BigQueryParameterHandler handler = new BigQueryParameterHandler(8, true); + handler.setParameter(1, (short) 7, Short.class); + handler.setParameter(2, (byte) 9, Byte.class); + handler.setParameter(3, 0.25f, Float.class); + handler.setParameter(4, LocalDate.of(2026, 11, 18), Date.class); + handler.setParameter(5, LocalTime.of(9, 8, 7, 123_456_000), Time.class); + handler.setParameter( + 6, + BigQueryParameterHandler.formatDateTimeDigits(LocalDateTime.of(2026, 11, 18, 9, 8, 7)), + String.class); + Instant tsInstant = Instant.parse("2026-11-18T19:39:36.123456789Z"); + handler.setParameter(7, Timestamp.from(tsInstant), Timestamp.class); + handler.setParameter(8, Timestamp.from(tsInstant), Timestamp.class); + + Queue> batches = new LinkedList<>(); + batches.add(handler.parametersList); + assertTrue( + BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + schema.getFields(), batches)); + + JsonObject jsonRow = + BigQueryParameterHandler.createWriteApiJsonRow( + schema.getFields(), handler.parametersList, true); + for (int i = 0; i < 6; i++) { + String fieldName = schema.getFields().get(i).getName(); + QueryParameterValue qpv = + BigQueryParameterHandler.toQueryParameterValue(handler.parametersList.get(i), true); + assertEquals(qpv.getValue(), jsonRow.get(fieldName).getAsString()); + } + assertEquals("2026-11-18 19:39:36.123456Z", jsonRow.get("ts6_col").getAsString()); + assertEquals("2026-11-18 19:39:36.123456789000Z", jsonRow.get("ts12_col").getAsString()); + + // ResolverStyle.STRICT rejects non-existent calendar dates like 1500-02-29 or 2026-02-31. + Schema dtSchema = Schema.of(Field.of("dt_col", StandardSQLTypeName.DATETIME)); + for (String badDateTime : + new String[] {"1500-02-29 10:20:30.000000", "2026-02-31 10:20:30.000000"}) { + BigQueryParameterHandler invalidDtHandler = new BigQueryParameterHandler(1); + invalidDtHandler.setParameter(1, badDateTime, String.class); + Queue> invalidBatch = new LinkedList<>(); + invalidBatch.add(invalidDtHandler.parametersList); + assertFalse( + BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + dtSchema.getFields(), invalidBatch)); + } + + // ResolverStyle.STRICT on DATE_DIGITS rejects Julian leap date 1500-02-29 for DATE columns. + Schema dateSchema = Schema.of(Field.of("d_col", StandardSQLTypeName.DATE)); + BigQueryParameterHandler invalidDateHandler = new BigQueryParameterHandler(1); + invalidDateHandler.setParameter(1, Date.valueOf("1500-02-29"), Date.class); + Queue> invalidDateBatch = new LinkedList<>(); + invalidDateBatch.add(invalidDateHandler.parametersList); + assertFalse( + BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + dateSchema.getFields(), invalidDateBatch)); + + // Extreme negative BigDecimal scale on unparameterized NUMERIC & precision-only NUMERIC(5). + Schema numSchema = + Schema.of( + Field.of("n_any", StandardSQLTypeName.NUMERIC), + Field.newBuilder("n5", StandardSQLTypeName.NUMERIC).setPrecision(5L).build()); + for (BigDecimal[] row : + new BigDecimal[][] { + {BigDecimal.valueOf(1, Integer.MIN_VALUE), BigDecimal.ONE}, + {BigDecimal.valueOf(10, Integer.MIN_VALUE), BigDecimal.ONE}, + {BigDecimal.ONE, new BigDecimal("12.5")} + }) { + BigQueryParameterHandler badNumHandler = new BigQueryParameterHandler(2); + badNumHandler.setParameter(1, row[0], BigDecimal.class); + badNumHandler.setParameter(2, row[1], BigDecimal.class); + Queue> badNumBatch = new LinkedList<>(); + badNumBatch.add(badNumHandler.parametersList); + assertFalse( + BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + numSchema.getFields(), badNumBatch)); + } + BigQueryParameterHandler okNumHandler = new BigQueryParameterHandler(2); + okNumHandler.setParameter(1, new BigDecimal("999.123456789"), BigDecimal.class); + okNumHandler.setParameter(2, new BigDecimal("12300"), BigDecimal.class); + Queue> okNumBatch = new LinkedList<>(); + okNumBatch.add(okNumHandler.parametersList); + assertTrue( + BigQueryParameterHandler.areBatchParametersCompatibleWithWriteApi( + numSchema.getFields(), okNumBatch)); + } + + @Test + public void testConfigureParametersThrowsBigQueryJdbcExceptionWhenParametersUnset() { + BigQueryParameterHandler unsetAllHandler = new BigQueryParameterHandler(2); + QueryJobConfiguration.Builder builder1 = QueryJobConfiguration.newBuilder("SELECT ?, ?"); + BigQueryJdbcException missingFirst = + assertThrows( + BigQueryJdbcException.class, () -> unsetAllHandler.configureParameters(builder1)); + assertTrue(missingFirst.getMessage().contains("parameter 1 of 2")); + + BigQueryParameterHandler partialHandler = new BigQueryParameterHandler(2); + partialHandler.setParameter(1, "only-first", String.class); + QueryJobConfiguration.Builder builder2 = QueryJobConfiguration.newBuilder("SELECT ?, ?"); + BigQueryJdbcException missingSecond = + assertThrows( + BigQueryJdbcException.class, () -> partialHandler.configureParameters(builder2)); + assertTrue(missingSecond.getMessage().contains("parameter 2 of 2")); + } } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatementWriteApiTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatementWriteApiTest.java index 6ee46e027880..77bb1ee1726d 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatementWriteApiTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatementWriteApiTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; @@ -41,10 +42,12 @@ import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.FieldElementType; import com.google.cloud.bigquery.Job; import com.google.cloud.bigquery.JobInfo; import com.google.cloud.bigquery.JobStatistics.QueryStatistics; import com.google.cloud.bigquery.JobStatistics.QueryStatistics.StatementType; +import com.google.cloud.bigquery.QueryParameterValue; import com.google.cloud.bigquery.Schema; import com.google.cloud.bigquery.StandardSQLTypeName; import com.google.cloud.bigquery.TableId; @@ -53,23 +56,47 @@ import com.google.cloud.bigquery.jdbc.telemetry.v1.StatementExecution; import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager; import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; +import com.google.cloud.bigquery.storage.v1.BQTableSchemaToProtoDescriptor; import com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsRequest; import com.google.cloud.bigquery.storage.v1.BatchCommitWriteStreamsResponse; import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient; import com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest; import com.google.cloud.bigquery.storage.v1.FinalizeWriteStreamResponse; import com.google.cloud.bigquery.storage.v1.JsonStreamWriter; +import com.google.cloud.bigquery.storage.v1.JsonToProtoMessage; +import com.google.cloud.bigquery.storage.v1.TableFieldSchema; import com.google.cloud.bigquery.storage.v1.TableSchema; import com.google.cloud.bigquery.storage.v1.WriteStream; import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.protobuf.Descriptors.Descriptor; +import com.google.protobuf.DynamicMessage; +import com.google.protobuf.Int64Value; import io.grpc.Status; import io.grpc.StatusRuntimeException; import java.io.IOException; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Date; import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; +import java.util.TimeZone; +import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -95,10 +122,38 @@ public void setUp() { doReturn(1000).when(connection).getWriteAPIAppendRowCount(); } + @FunctionalInterface + private interface BatchRowBinder { + void bindRow(int rowIndex) throws SQLException; + } + + private static void populateBatchAndAssertWriteApi( + BigQueryPreparedStatement ps, int rows, BatchRowBinder binder, boolean expectedUseWriteApi) + throws SQLException { + ps.clearBatch(); + for (int i = 0; i < rows; i++) { + binder.bindRow(i); + ps.addBatch(); + } + assertEquals(expectedUseWriteApi, ps.useWriteAPI()); + } + private static QueryParameter queryParam(String typeName) { return new QueryParameter().setParameterType(new QueryParameterType().setType(typeName)); } + private static TableFieldSchema protoField(String name, TableFieldSchema.Type type, long picos) { + TableFieldSchema.Builder b = + TableFieldSchema.newBuilder() + .setName(name) + .setType(type) + .setMode(TableFieldSchema.Mode.NULLABLE); + if (picos > 0) { + b.setTimestampPrecision(Int64Value.of(picos)); + } + return b.build(); + } + private Job mockInsertDryRunJob(TableId tableId, Schema schema, List parameters) { Job dryRunJob = mock(Job.class); QueryStatistics stats = mock(QueryStatistics.class); @@ -319,4 +374,226 @@ public void testExecuteBatchWriteApiFailurePathsPreserveClientCloseWriterAndReco }); verify(stalledWriter, times(1)).close(); } + + @Test + public void testCreateJsonRowAndProtoRoundTripAcrossLocalesZonesAndTypes() throws Exception { + Schema schema = + Schema.of( + Field.of("b_col", StandardSQLTypeName.BYTES), + Field.of("f_col", StandardSQLTypeName.FLOAT64), + Field.of("n_zero", StandardSQLTypeName.NUMERIC), + Field.of("bool_col", StandardSQLTypeName.BOOL), + Field.of("d_sql", StandardSQLTypeName.DATE), + Field.of("d_local", StandardSQLTypeName.DATE), + Field.of("t_sql", StandardSQLTypeName.TIME), + Field.of("t_local", StandardSQLTypeName.TIME), + Field.of("dt_col", StandardSQLTypeName.DATETIME), + Field.of("ts_sql", StandardSQLTypeName.TIMESTAMP), + Field.of("ts_odt", StandardSQLTypeName.TIMESTAMP), + Field.newBuilder("ts_picos", StandardSQLTypeName.TIMESTAMP) + .setTimestampPrecision(12L) + .build()); + doReturn( + mockInsertDryRunJob( + TableId.of("proj", "ds", "TAB"), + schema, + Arrays.asList( + queryParam("BYTES"), + queryParam("FLOAT64"), + queryParam("NUMERIC"), + queryParam("BOOL"), + queryParam("DATE"), + queryParam("DATE"), + queryParam("TIME"), + queryParam("TIME"), + queryParam("STRING"), + queryParam("TIMESTAMP"), + queryParam("TIMESTAMP"), + queryParam("TIMESTAMP")))) + .when(bigQuery) + .create(any(JobInfo.class)); + + TableSchema protoTableSchema = + TableSchema.newBuilder() + .addFields(protoField("b_col", TableFieldSchema.Type.BYTES, 0)) + .addFields(protoField("f_col", TableFieldSchema.Type.DOUBLE, 0)) + .addFields(protoField("n_zero", TableFieldSchema.Type.NUMERIC, 0)) + .addFields(protoField("bool_col", TableFieldSchema.Type.BOOL, 0)) + .addFields(protoField("d_sql", TableFieldSchema.Type.DATE, 0)) + .addFields(protoField("d_local", TableFieldSchema.Type.DATE, 0)) + .addFields(protoField("t_sql", TableFieldSchema.Type.TIME, 0)) + .addFields(protoField("t_local", TableFieldSchema.Type.TIME, 0)) + .addFields(protoField("dt_col", TableFieldSchema.Type.DATETIME, 0)) + .addFields(protoField("ts_sql", TableFieldSchema.Type.TIMESTAMP, 0)) + .addFields(protoField("ts_odt", TableFieldSchema.Type.TIMESTAMP, 0)) + .addFields(protoField("ts_picos", TableFieldSchema.Type.TIMESTAMP, 12)) + .build(); + Descriptor descriptor = + BQTableSchemaToProtoDescriptor.convertBQTableSchemaToProtoDescriptor(protoTableSchema); + + byte[] rawBytes = new byte[] {-128, 0, 127}; + Locale origLocale = Locale.getDefault(); + TimeZone origZone = TimeZone.getDefault(); + try { + for (Locale locale : Arrays.asList(Locale.US, Locale.FRANCE, Locale.JAPAN)) { + for (String zoneId : Arrays.asList("UTC", "America/New_York", "Asia/Tokyo")) { + Locale.setDefault(locale); + TimeZone.setDefault(TimeZone.getTimeZone(zoneId)); + for (boolean picos : new boolean[] {false, true}) { + doReturn(picos).when(connection).isEnableTimestampPicos(); + BigQueryPreparedStatement ps = + new BigQueryPreparedStatement( + connection, "INSERT INTO TAB VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + for (int r = 0; r < 3; r++) { + ps.setBytes(1, rawBytes); + ps.setFloat(2, 0.6f); + ps.setBigDecimal(3, BigDecimal.valueOf(0, -100_000_000)); + ps.setBoolean(4, true); + ps.setDate(5, Date.valueOf("2026-11-18")); + ps.setObject(6, LocalDate.of(1969, 7, 20)); + ps.setTime(7, Time.valueOf("14:39:36")); + ps.setObject(8, LocalTime.of(12, 34, 56, 789_123_000)); + ps.setObject(9, LocalDateTime.of(2025, 12, 3, 12, 30, 0, 123_456_000)); + ps.setTimestamp(10, Timestamp.from(Instant.parse("2026-11-18T19:39:36.123456789Z"))); + ps.setObject( + 11, + ZonedDateTime.of( + 2026, 11, 18, 14, 39, 36, 123_456_000, ZoneId.of("America/New_York"))); + ps.setObject(12, OffsetDateTime.parse("2026-11-18T14:39:36.123456789-05:00")); + ps.addBatch(); + } + assertTrue(ps.useWriteAPI()); + + JsonObject row = + BigQueryPreparedStatement.createJsonRow( + schema.getFields(), ps.parameterHandler.parametersList, picos); + assertEquals("0", row.get("n_zero").getAsString()); + assertEquals( + picos ? "2026-11-18 19:39:36.123456789000Z" : "2026-11-18 19:39:36.123456Z", + row.get("ts_picos").getAsString()); + assertEquals( + row.get("ts_picos").getAsString(), + QueryParameterValue.timestamp(row.get("ts_picos").getAsString()).getValue()); + + DynamicMessage protoMsg = + JsonToProtoMessage.INSTANCE.convertToProtoMessage( + descriptor, protoTableSchema, new JSONObject(row.toString())); + assertArrayEquals( + rawBytes, (byte[]) protoMsg.getField(descriptor.findFieldByName("b_col"))); + assertEquals( + (int) LocalDate.of(2026, 11, 18).toEpochDay(), + protoMsg.getField(descriptor.findFieldByName("d_sql"))); + assertEquals( + ChronoUnit.MICROS.between( + Instant.EPOCH, Instant.parse("2026-11-18T19:39:36.123456Z")), + protoMsg.getField(descriptor.findFieldByName("ts_sql"))); + assertEquals( + picos ? "2026-11-18 19:39:36.123456789000Z" : "2026-11-18T19:39:36.123456+00:00", + protoMsg.getField(descriptor.findFieldByName("ts_picos"))); + } + } + } + } finally { + doReturn(false).when(connection).isEnableTimestampPicos(); + Locale.setDefault(origLocale); + TimeZone.setDefault(origZone); + } + } + + @Test + public void testWriteApiCompatibilityAndParameterizedNumericBounds() throws Exception { + // Unsupported RANGE field leaves insertSchema null. + Schema rangeSchema = + Schema.of( + Field.newBuilder("rng", StandardSQLTypeName.RANGE) + .setRangeElementType(FieldElementType.newBuilder().setType("DATE").build()) + .build()); + doReturn( + mockInsertDryRunJob( + TableId.of("proj", "ds", "TAB"), + rangeSchema, + Collections.singletonList(queryParam("STRING")))) + .when(bigQuery) + .create(any(JobInfo.class)); + assertNull( + new BigQueryPreparedStatement(connection, "INSERT INTO TAB VALUES (?)").insertSchema); + + Schema schema = + Schema.of( + Field.newBuilder("req_str", StandardSQLTypeName.STRING) + .setMode(Field.Mode.REQUIRED) + .build(), + Field.newBuilder("n5_2", StandardSQLTypeName.NUMERIC) + .setPrecision(5L) + .setScale(2L) + .build(), + Field.of("flt_col", StandardSQLTypeName.FLOAT64), + Field.of("d_col", StandardSQLTypeName.DATE), + Field.of("dt_col", StandardSQLTypeName.DATETIME), + Field.of("ts_col", StandardSQLTypeName.TIMESTAMP), + Field.of("json_col", StandardSQLTypeName.JSON)); + doReturn( + mockInsertDryRunJob( + TableId.of("proj", "ds", "TAB"), + schema, + Arrays.asList( + queryParam("STRING"), + queryParam("NUMERIC"), + queryParam("FLOAT64"), + queryParam("DATE"), + queryParam("STRING"), + queryParam("TIMESTAMP"), + queryParam("JSON")))) + .when(bigQuery) + .create(any(JobInfo.class)); + + BigQueryPreparedStatement ps = + new BigQueryPreparedStatement(connection, "INSERT INTO TAB VALUES (?, ?, ?, ?, ?, ?, ?)"); + + // Valid values (including trailing zeros "12.3400", INT64 999 on NUMERIC(5,2), null JSON). + BatchRowBinder validRow = + i -> { + ps.setString(1, "v" + i); + ps.setBigDecimal(2, new BigDecimal("12.3400")); + ps.setDouble(3, 1.5d); + ps.setObject(4, LocalDate.of(2026, 11, 18)); + ps.setObject(5, LocalDateTime.of(2026, 11, 18, 10, 20, 30)); + ps.setTimestamp(6, Timestamp.from(Instant.parse("2026-11-18T10:20:30Z"))); + ps.setNull(7, Types.OTHER); + }; + populateBatchAndAssertWriteApi(ps, 3, validRow, true); + populateBatchAndAssertWriteApi( + ps, + 3, + i -> { + validRow.bindRow(i); + ps.setInt(2, 999); + }, + true); + + // Incompatible / out-of-range bindings fall back cleanly to standard SQL DML. + for (BatchRowBinder invalidOverride : + Arrays.asList( + i -> ps.setNull(1, Types.VARCHAR), // NULL on REQUIRED + i -> ps.setInt(1, 99), // INT64 into STRING + i -> ps.setBigDecimal(2, new BigDecimal("1234.50")), // 4 int digits > 5 - 2 + i -> ps.setBigDecimal(2, new BigDecimal("12.345")), // scale 3 > 2 + i -> ps.setInt(2, 1000), // INT64 4 int digits > 5 - 2 + i -> ps.setObject(2, BigInteger.TEN, Types.BIGINT), + i -> ps.setDouble(3, Double.NaN), + i -> ps.setObject(4, LocalDate.of(10000, 1, 1)), + i -> ps.setString(5, "2026-11-18 10:20:30"), + i -> ps.setObject(6, LocalDateTime.of(2026, 11, 18, 10, 20, 30)), + i -> ps.setTimestamp(6, Timestamp.from(Instant.parse("+10000-01-01T00:00:00Z"))), + i -> ps.setString(7, "{\"k\":1}"))) { + populateBatchAndAssertWriteApi( + ps, + 3, + i -> { + validRow.bindRow(i); + invalidOverride.bindRow(i); + }, + false); + } + } } diff --git a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistryTest.java b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistryTest.java index b3d4a384481d..0b29ba34700b 100644 --- a/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistryTest.java +++ b/java-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryTypeRegistryTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import com.google.cloud.bigquery.Field; import com.google.cloud.bigquery.StandardSQLTypeName; import com.google.cloud.bigquery.exception.BigQueryJdbcException; import java.sql.Date; @@ -325,4 +326,52 @@ public void testDatetimeReadCoercionsPreserveCivilDigits(String zone) throws Exc .isEqualTo(Duration.ofHours(2).plusMinutes(30).toMillis()); } } + + @Test + public void testWriteApiHelpersReuseRegistryDescriptorsAndExtractInstant() { + assertThat(BigQueryTypeRegistry.isSupportedWriteApiField(null)).isFalse(); + assertThat( + BigQueryTypeRegistry.isSupportedWriteApiField( + Field.of("s", StandardSQLTypeName.STRING))) + .isTrue(); + assertThat( + BigQueryTypeRegistry.isSupportedWriteApiField( + Field.newBuilder("rep", StandardSQLTypeName.STRING) + .setMode(Field.Mode.REPEATED) + .build())) + .isFalse(); + assertThat( + BigQueryTypeRegistry.isSupportedWriteApiField( + Field.of( + "st", StandardSQLTypeName.STRUCT, Field.of("a", StandardSQLTypeName.STRING)))) + .isFalse(); + + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(null, "x")).isFalse(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.STRING, null)) + .isFalse(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, (byte) 1)) + .isTrue(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, (short) 2)) + .isTrue(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, 3)).isTrue(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, 4L)).isTrue(); + assertThat(BigQueryTypeRegistry.isSupportedJavaValue(StandardSQLTypeName.INT64, "4")).isFalse(); + assertThat( + BigQueryTypeRegistry.isSupportedJavaValue( + StandardSQLTypeName.DATE, LocalDate.of(2026, 11, 18))) + .isTrue(); + assertThat( + BigQueryTypeRegistry.isSupportedJavaValue( + StandardSQLTypeName.TIME, Time.valueOf("10:00:00"))) + .isTrue(); + + Instant expected = Instant.parse("2026-11-18T19:39:36.123456789Z"); + assertThat(BigQueryTypeRegistry.extractInstant(expected)).isEqualTo(expected); + assertThat(BigQueryTypeRegistry.extractInstant(expected.atOffset(ZoneOffset.ofHours(-5)))) + .isEqualTo(expected); + assertThat(BigQueryTypeRegistry.extractInstant(expected.atZone(ZoneId.of("Asia/Tokyo")))) + .isEqualTo(expected); + assertThat(BigQueryTypeRegistry.extractInstant(Timestamp.from(expected))).isEqualTo(expected); + assertThat(BigQueryTypeRegistry.extractInstant(Date.valueOf("2026-11-18"))).isNull(); + } }