-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(bigquery): add QueryResultsFormat and ArrowSerializationOptions configurations #13942
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
Changes from all commits
8caa072
b441a9d
13a7e67
afc9951
8f551c7
30f6835
e8e9de1
0f8156a
bfc1461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.common.base.MoreObjects; | ||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** <b>[Beta]</b> Options specific to the Apache Arrow output format. */ | ||
| @BetaApi | ||
| @NullMarked | ||
| public final class ArrowSerializationOptions implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final @Nullable String bufferCompression; | ||
| private final @Nullable String picosTimestampPrecision; | ||
|
|
||
| private ArrowSerializationOptions(Builder builder) { | ||
| this.bufferCompression = builder.bufferCompression; | ||
| this.picosTimestampPrecision = builder.picosTimestampPrecision; | ||
| } | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Returns the buffer compression algorithm (e.g., LZ4_FRAME, ZSTD, UNCOMPRESSED). | ||
| */ | ||
| @BetaApi | ||
| public @Nullable String getBufferCompression() { | ||
| return bufferCompression; | ||
| } | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Returns the timestamp precision for Arrow timestamp types. | ||
| * | ||
| * <p>Note: Only applies when {@link QueryResultsFormat#ARROW} is enabled. For Arrow result | ||
| * streams, this precision setting governs binary Arrow timestamp column types and takes | ||
| * precedence over {@link DataFormatOptions.TimestampFormatOptions}, which applies to default | ||
| * {@link QueryResultsFormat#STRUCT_ENCODING} JSON results. | ||
| */ | ||
| @BetaApi | ||
| public @Nullable String getPicosTimestampPrecision() { | ||
| return picosTimestampPrecision; | ||
| } | ||
|
|
||
| /** <b>[Beta]</b> Returns a new builder for {@link ArrowSerializationOptions}. */ | ||
| @BetaApi | ||
| public static Builder newBuilder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("bufferCompression", bufferCompression) | ||
| .add("picosTimestampPrecision", picosTimestampPrecision) | ||
| .toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| ArrowSerializationOptions that = (ArrowSerializationOptions) o; | ||
| return Objects.equals(bufferCompression, that.bufferCompression) | ||
| && Objects.equals(picosTimestampPrecision, that.picosTimestampPrecision); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(bufferCompression, picosTimestampPrecision); | ||
| } | ||
|
|
||
| com.google.api.services.bigquery.model.ArrowSerializationOptions toPb() { | ||
| return ArrowSerializationOptionsConverter.toPb(this); | ||
| } | ||
|
|
||
| static ArrowSerializationOptions fromPb( | ||
| com.google.api.services.bigquery.model.ArrowSerializationOptions optionsPb) { | ||
| return ArrowSerializationOptionsConverter.fromPb(optionsPb); | ||
| } | ||
|
|
||
| /** <b>[Beta]</b> Builder for {@link ArrowSerializationOptions}. */ | ||
| @BetaApi | ||
| public static final class Builder { | ||
| private @Nullable String bufferCompression; | ||
| private @Nullable String picosTimestampPrecision; | ||
|
Comment on lines
+108
to
+109
Member
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. Do you think it's possible we can set a default value for these so they they can be non-null and we can assert that they always will be non-null? especially in the setters? e.g. uncompressed and micro precision? |
||
|
|
||
| private Builder() {} | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Sets the buffer compression algorithm (e.g., LZ4_FRAME, ZSTD, UNCOMPRESSED). | ||
| */ | ||
| @BetaApi | ||
| public Builder setBufferCompression(String bufferCompression) { | ||
| this.bufferCompression = checkNotNull(bufferCompression, "bufferCompression cannot be null"); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Sets the timestamp precision for Arrow timestamp types. | ||
| * | ||
| * <p>Note: Only applies when {@link QueryResultsFormat#ARROW} is enabled. For Arrow result | ||
| * streams, this precision setting governs binary Arrow timestamp column types and takes | ||
| * precedence over {@link DataFormatOptions.TimestampFormatOptions}, which applies to default | ||
| * {@link QueryResultsFormat#STRUCT_ENCODING} JSON results. | ||
| */ | ||
| @BetaApi | ||
| public Builder setPicosTimestampPrecision(String picosTimestampPrecision) { | ||
|
Comment on lines
+117
to
+131
Member
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. For these setters, do you think we should create enum wrappers around the options? If there is a typo in the precision on compression codec, it may fail with the apiary model |
||
| this.picosTimestampPrecision = | ||
| checkNotNull(picosTimestampPrecision, "picosTimestampPrecision cannot be null"); | ||
| return this; | ||
| } | ||
|
|
||
| /** <b>[Beta]</b> Builds a new instance of {@link ArrowSerializationOptions}. */ | ||
| @BetaApi | ||
| public ArrowSerializationOptions build() { | ||
| return new ArrowSerializationOptions(this); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @NullMarked | ||
| final class ArrowSerializationOptionsConverter { | ||
|
|
||
| private ArrowSerializationOptionsConverter() {} | ||
|
|
||
| static com.google.api.services.bigquery.model.@Nullable ArrowSerializationOptions toPb( | ||
| @Nullable ArrowSerializationOptions options) { | ||
| if (options == null) { | ||
| return null; | ||
| } | ||
| com.google.api.services.bigquery.model.ArrowSerializationOptions optionsPb = | ||
| new com.google.api.services.bigquery.model.ArrowSerializationOptions(); | ||
| if (options.getBufferCompression() != null) { | ||
| optionsPb.setBufferCompression(options.getBufferCompression()); | ||
| } | ||
| if (options.getPicosTimestampPrecision() != null) { | ||
| optionsPb.setPicosTimestampPrecision(options.getPicosTimestampPrecision()); | ||
|
Comment on lines
+29
to
+38
Member
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. Follow the above, do you think we can just assert that the params are non-null? Also if we set a default value for ArrowSerializationOptions's params, then I don't think we need the null checks here |
||
| } | ||
| return optionsPb; | ||
| } | ||
|
|
||
| static @Nullable ArrowSerializationOptions fromPb(@Nullable Object optionsPbObj) { | ||
| if (optionsPbObj == null) { | ||
| return null; | ||
| } | ||
| com.google.api.services.bigquery.model.ArrowSerializationOptions optionsPb = | ||
| (com.google.api.services.bigquery.model.ArrowSerializationOptions) optionsPbObj; | ||
| return ArrowSerializationOptions.newBuilder() | ||
| .setBufferCompression(optionsPb.getBufferCompression()) | ||
| .setPicosTimestampPrecision(optionsPb.getPicosTimestampPrecision()) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import static com.google.common.base.Preconditions.checkNotNull; | ||
| import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.api.services.bigquery.model.JobConfigurationQuery; | ||
| import com.google.api.services.bigquery.model.QueryParameter; | ||
| import com.google.cloud.bigquery.JobInfo.CreateDisposition; | ||
|
|
@@ -75,6 +76,8 @@ public final class QueryJobConfiguration extends JobConfiguration { | |
| private final Long maxResults; | ||
| private final JobCreationMode jobCreationMode; | ||
| private final String reservation; | ||
| private final QueryResultsFormat queryResultsFormat; | ||
| private final ArrowSerializationOptions arrowSerializationOptions; | ||
|
|
||
| /** | ||
| * Priority levels for a query. If not specified the priority is assumed to be {@link | ||
|
|
@@ -144,6 +147,8 @@ public static final class Builder | |
| private Long maxResults; | ||
| private JobCreationMode jobCreationMode; | ||
| private String reservation; | ||
| private QueryResultsFormat queryResultsFormat; | ||
| private ArrowSerializationOptions arrowSerializationOptions; | ||
|
|
||
| private Builder() { | ||
| super(Type.QUERY); | ||
|
|
@@ -181,6 +186,8 @@ private Builder(QueryJobConfiguration jobConfiguration) { | |
| this.maxResults = jobConfiguration.maxResults; | ||
| this.jobCreationMode = jobConfiguration.jobCreationMode; | ||
| this.reservation = jobConfiguration.reservation; | ||
| this.queryResultsFormat = jobConfiguration.queryResultsFormat; | ||
| this.arrowSerializationOptions = jobConfiguration.arrowSerializationOptions; | ||
| } | ||
|
|
||
| private Builder(com.google.api.services.bigquery.model.JobConfiguration configurationPb) { | ||
|
|
@@ -701,6 +708,43 @@ public Builder setReservation(String reservation) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Sets the query results response format. Defaults to {@link | ||
| * QueryResultsFormat#STRUCT_ENCODING}. | ||
| * | ||
| * <p>When set to {@link QueryResultsFormat#ARROW}, query results are returned in binary Apache | ||
| * Arrow format, utilizing gRPC Storage Read streams for subsequent pages. | ||
| * | ||
| * <p><b>Prerequisite:</b> Requires the BigQuery Storage Read API ({@code | ||
|
jinseopkim0 marked this conversation as resolved.
|
||
| * bigquerystorage.googleapis.com}) to be enabled on your GCP project. See the <a | ||
| * href="https://docs.cloud.google.com/apis/docs/getting-started#enabling_apis">Google Cloud | ||
| * Enabling APIs Guide</a>. | ||
| * | ||
| * @param queryResultsFormat the format for query result payloads | ||
| * @return the Builder | ||
| */ | ||
| @BetaApi | ||
| public Builder setQueryResultsFormat(QueryResultsFormat queryResultsFormat) { | ||
| this.queryResultsFormat = queryResultsFormat; | ||
|
Member
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. nit: perhaps non-null checks here? We should be able to assert that users must pass valid values here |
||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * <b>[Beta]</b> Sets Arrow serialization options. Defaults to null. | ||
| * | ||
| * <p>Note: Only applied in the request payload when {@code queryResultsFormat} is {@code | ||
| * ARROW}. | ||
| * | ||
| * @param arrowSerializationOptions the Arrow serialization options to set | ||
| * @return the Builder | ||
| */ | ||
| @BetaApi | ||
| public Builder setArrowSerializationOptions( | ||
| ArrowSerializationOptions arrowSerializationOptions) { | ||
| this.arrowSerializationOptions = arrowSerializationOptions; | ||
| return this; | ||
| } | ||
|
|
||
| public QueryJobConfiguration build() { | ||
| return new QueryJobConfiguration(this); | ||
| } | ||
|
|
@@ -747,6 +791,8 @@ private QueryJobConfiguration(Builder builder) { | |
| this.maxResults = builder.maxResults; | ||
| this.jobCreationMode = builder.jobCreationMode; | ||
| this.reservation = builder.reservation; | ||
| this.queryResultsFormat = builder.queryResultsFormat; | ||
| this.arrowSerializationOptions = builder.arrowSerializationOptions; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -973,6 +1019,18 @@ public Builder toBuilder() { | |
| return new Builder(this); | ||
| } | ||
|
|
||
| /** <b>[Beta]</b> Returns the query results response format. */ | ||
| @BetaApi | ||
| public QueryResultsFormat getQueryResultsFormat() { | ||
| return queryResultsFormat; | ||
| } | ||
|
|
||
| /** <b>[Beta]</b> Returns Arrow serialization options, or null if unset. */ | ||
| @BetaApi | ||
| public ArrowSerializationOptions getArrowSerializationOptions() { | ||
| return arrowSerializationOptions; | ||
| } | ||
|
|
||
| @Override | ||
| ToStringHelper toStringHelper() { | ||
| return super.toStringHelper() | ||
|
|
@@ -1004,13 +1062,23 @@ ToStringHelper toStringHelper() { | |
| .add("rangePartitioning", rangePartitioning) | ||
| .add("connectionProperties", connectionProperties) | ||
| .add("jobCreationMode", jobCreationMode) | ||
| .add("reservation", reservation); | ||
| .add("reservation", reservation) | ||
| .add("queryResultsFormat", queryResultsFormat) | ||
| .add("arrowSerializationOptions", arrowSerializationOptions); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return obj == this | ||
| || obj instanceof QueryJobConfiguration && baseEquals((QueryJobConfiguration) obj); | ||
| if (obj == this) { | ||
| return true; | ||
| } | ||
| if (obj == null || !(obj instanceof QueryJobConfiguration)) { | ||
| return false; | ||
| } | ||
| QueryJobConfiguration other = (QueryJobConfiguration) obj; | ||
| return baseEquals(other) | ||
| && Objects.equals(queryResultsFormat, other.queryResultsFormat) | ||
| && Objects.equals(arrowSerializationOptions, other.arrowSerializationOptions); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -1043,7 +1111,9 @@ public int hashCode() { | |
| labels, | ||
| rangePartitioning, | ||
| connectionProperties, | ||
| reservation); | ||
| reservation, | ||
| queryResultsFormat, | ||
| arrowSerializationOptions); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.bigquery; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| /** <b>[Beta]</b> The format of the query results. */ | ||
| @BetaApi | ||
| @NullMarked | ||
| public enum QueryResultsFormat { | ||
| /** Serialized row data in Apache Arrow format. */ | ||
| ARROW, | ||
|
|
||
| /** Default encoding of results as JSON struct array. */ | ||
| STRUCT_ENCODING | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.