Skip to content
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

BigQuery: Unable to insert an empty array for an array of struct when using parameterized query #3606

Open
trietsch opened this issue Dec 12, 2024 · 0 comments
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.

Comments

@trietsch
Copy link

Environment details

  1. Specify the API at the beginning of the title. For example, "BigQuery: ...").
    General, Core, and Other are also allowed as types
  2. OS type and version: macOS 14.7.1
  3. Java version: JDK Zulu 21.0.3
  4. version(s): 2.44.0

Steps to reproduce

  1. Create a BigQuery table that contains a field, which is an ARRAY<STRUCT<subfield STRING>>. E.g.
create table `<project_id>.example.bug` (
  people ARRAY<STRUCT<name STRING>>
);
  1. Create a parameterized query, such as:
insert into `<project_id>.example.bug` (people) values (@people);
  1. Prepare the query job configuration (assume a BigQuery service is configured in the snippet below):
data class Person(val name: String)

val people = emptyList<Person>()
val sql = "insert into `<project_id>.example.bug` (people) values (@people);"
val params = mapOf("people" to QueryParameterValue.array(people, StandardSQLTypeName.STRUCT))
val jobConfig = QueryJobConfiguration.newBuilder(sql).setNamedParameters(params).build()

val tableResult = bigQuery.query(jobConfig)

The code above will fail with ``.

Stack trace

com.google.cloud.bigquery.BigQueryException: Value has type ARRAY<STRUCT<>> which cannot be inserted into column must_not_contain, which has type ARRAY<STRUCT<term_regex STRING>> at [7:9]
...
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
POST https://bigquery.googleapis.com/bigquery/v2/projects/bolcom-stg-nostradamus-916/queries
{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "location": "q",
      "locationType": "parameter",
      "message": "Value has type ARRAY<STRUCT<>> which cannot be inserted into column people, which has type ARRAY<STRUCT<name STRING>> at [7:9]",
      "reason": "invalidQuery"
    }
  ],
  "message": "Value has type ARRAY<STRUCT<>> which cannot be inserted into column people, which has type ARRAY<STRUCT<name STRING>> at [7:9]",
  "status": "INVALID_ARGUMENT"
}
	at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
	at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$3.interceptResponse(AbstractGoogleClientRequest.java:479)
	at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:565)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:506)
	at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:616)
	at com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc.queryRpc(HttpBigQueryRpc.java:771)
	... 19 more

Any additional information below

This is caused by the fact that setting the value and the schema are intertwined in the Java SDK. In the Python SDK, this is not the case, an empty array and the corresponding schema can be set individually. See here as a reference.

Proposed resolution

There are two options here:

  1. QueryParameterValue should allow for a separation of schema definition and value. Currently it's not possible to provide the structure of a field without specifying a value.
  2. BigQuery itself should not require the full schema when an empty array is provided to a field that has an ARRAY<STRUCT<subfield STRING>> as a structure.

Thanks!

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/java-bigquery API. label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/java-bigquery API.
Projects
None yet
Development

No branches or pull requests

1 participant