Skip to content

Pass the declared type of ARRAY and STRUCT query parameters - #525

Open
kfirco-torq wants to merge 2 commits into
goccy:mainfrom
kfirco-torq:typed-array-struct-params
Open

kfirco-torq wants to merge 2 commits into
goccy:mainfrom
kfirco-torq:typed-array-struct-params

Conversation

@kfirco-torq

@kfirco-torq kfirco-torq commented Oct 1, 2026 •

Copy link
Copy Markdown

Depends on goccy/googlesqlite#106, which adds googlesqlite.TypedValue. The first commit is a temporary replace that points go.mod at that change so CI can build. Once googlesqlite releases it, I'll swap that commit for a version bump.

Problem

ARRAY and STRUCT query parameters decode to Go slices and maps, and googlesqlite then has to infer their type. That loses information that only the request's parameterType carries:

  • the field order of a struct (structValues is a JSON object);
  • the type of a NULL field, and the element type of an empty array;
  • the type of scalar leaves, which the REST API sends as strings.

A nested NULL also decoded as "". QueryParameterValue.Value is a plain string, and the nil-pointer handling used for top-level NULLs can't reach into StructValues, which holds values rather than pointers.

Minimal repro (jobs.query):

{"query": "SELECT TO_JSON_STRING(@p)", "useLegacySql": false, "parameterMode": "NAMED",
 "queryParameters": [{"name": "p",
   "parameterType": {"type": "ARRAY", "arrayType": {"type": "STRUCT", "structTypes": [
     {"name": "id", "type": {"type": "INT64"}}, {"name": "name", "type": {"type": "STRING"}}]}},
   "parameterValue": {"arrayValues": [{"structValues": {"name": {"value": null}, "id": {"value": "2"}}}]}}]}
  • Expected: [{"id":2,"name":null}]
  • On main: "[{\"name\":\"\",\"id\":\"2\"}]". The value is a STRING, the fields are in random order, id is a string and name is "".

@p.id fails to analyze with Cannot access field id on a value with type STRING. The Go client shows the same behaviour.

Fix

  • Declared types. repository.Query converts the parameterType of an ARRAY or STRUCT parameter into a googlesqlite.Type and passes the value as a googlesqlite.TypedValue. googlesqlite then casts the value to that type and declares the parameter with it.
  • Nested NULLs. applyNullQueryParameters walks the raw JSON of ARRAY and STRUCT parameters. It marks every nested scalar whose value is JSON null or absent with NullFields: ["Value"], which is bigqueryv2's own way of saying a field is NULL, so no sentinel string is needed. queryParameterValueToGoValue reads a marked value as nil.

Tests

TestArrayAndStructQueryParameterTypes (server/server_test.go) covers:

  • an ARRAY<STRUCT<id INT64, name STRING, tags ARRAY<STRING>>> sent over REST, with unordered fields, a NULL field and an empty array;
  • STRUCT field access on the same type;
  • the same array sent through the Go client.

All three subtests fail on main and pass with this change. go test ./... passes, including test/e2e.

Notes

Points github.com/goccy/googlesqlite at the unreleased change that adds
googlesqlite.TypedValue. Replace this commit with a version bump once
that change is released.
An ARRAY or STRUCT query parameter decodes to Go slices and maps, from
which googlesqlite has to infer its type. That loses what only the
request's parameterType carries: struct field order (structValues is a
JSON object), the type of a NULL field, the element type of an empty
array, and the type of scalar leaves, which the REST API sends as
strings. The query saw the wrong type: a STRUCT as STRING ("Cannot
access field id on a value with type STRING"), fields in random order,
INT64 fields as strings.

Convert the parameterType into a googlesqlite.Type and pass the value as
a googlesqlite.TypedValue, which googlesqlite casts and declares with
that type.

A NULL scalar nested inside an ARRAY or STRUCT decoded as "", because
QueryParameterValue.Value is a plain string and a top-level parameter's
nil-pointer treatment does not apply to StructValues, which holds
values. applyNullQueryParameters now walks the raw JSON of ARRAY and
STRUCT parameters and marks every nested scalar whose value is JSON
null or absent with NullFields: ["Value"], bigqueryv2's own way of
saying a field is NULL, and queryParameterValueToGoValue reads it as
nil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant