googleapis / googleapis/google-cloud-java

[java-bigquery] BigQuery: Unable to insert an empty array for an array of struct when using parameterized query

未关闭
#12,152 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
api: bigquery priority: p3 type: bug
主要语言
Java
星标
2.1k
派生
1.2k
平均合并
1 天 23 小时
30 天内合并 PR
154

描述

#### 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>`. E.g.
```sql
create table `.example.bug` (
people ARRAY>
);
```
2. Create a parameterized query, such as:
```
insert into `.example.bug` (people) values (@people);
```
3. Prepare the query job configuration (assume a `BigQuery` service is configured in the snippet below):
```kotlin
data class Person(val name: String)

val people = emptyList()
val sql = "insert into `.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> which cannot be inserted into column must_not_contain, which has type ARRAY> 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> which cannot be inserted into column people, which has type ARRAY> at [7:9]",
"reason": "invalidQuery"
}
],
"message": "Value has type ARRAY> which cannot be inserted into column people, which has type ARRAY> 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](https://dev.to/stack-labs/how-to-pass-an-array-of-structs-in-bigquerys-parameterized-queries-39nm#:~:text=Gotcha%20n%C2%B02%3A%20Provide%20the%20full%20structure%20type%20as%20second%20argument).

#### 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>` as a structure.

Thanks!

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。