googleapis / googleapis/google-cloud-go
spannertest: error getting data with json
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.6k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 109
Description
### Issue Description:
When executing a SELECT query in Spanner using Go, I encounter the following error:
`spanner: code = "Unknown", desc = "rpc error: code = Unknown desc = unhandled base type 8"`
**Context:**
I am working with a table that contains a JSON type column. The error occurs when trying to read data from that column during a SELECT query.
**Steps to Reproduce:**
- Create a table with a JSON type column.
- Insert data into the JSON column.
- Execute a SELECT query to retrieve data from that column.
- The error is encountered when reading the JSON field.
**Relevant Code:**
```
package spanner
import (
"context"
"fmt"
"testing"
"cloud.google.com/go/spanner"
database "cloud.google.com/go/spanner/admin/database/apiv1"
"cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
"cloud.google.com/go/spanner/spannertest"
"github.com/stretchr/testify/assert"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func setupTableTest(t *testing.T) (context.Context, *spanner.Client, func()) {
ctx := context.Background()
server, err := spannertest.NewServer(":0")
assert.NoError(t, err)
conn, err := grpc.NewClient(server.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err)
dbClient, err := database.NewDatabaseAdminClient(ctx, option.WithGRPCConn(conn))
assert.NoError(t, err)
op, err := dbClient.UpdateDatabaseDdl(ctx, &databasepb.UpdateDatabaseDdlRequest{
Database: "test-db",
Statements: []string{
`CREATE TABLE test_table (
id STRING(30) NOT NULL,
other_id STRING(30) NOT NULL,
data JSON
) PRIMARY KEY (id, other_id)`,
},
})
assert.NoError(t, err)
assert.NoError(t, op.Wait(ctx))
spannerClient, err := spanner.NewClient(ctx, "projects/my-project/instances/my-instance/databases/test-db", option.WithGRPCConn(conn))
assert.NoError(t, err)
return ctx, spannerClient, func() {
spannerClient.Close()
server.Close()
}
}
func checkInsertedData(client *spanner.Client, ctx context.Context) error {
query := `SELECT id, other_id, data FROM test_table`
stmt := spanner.Statement{
SQL: query,
}
iter := client.Single().Query(ctx, stmt)
defer iter.Stop()
for {
row, err := iter.Next()
fmt.Println(err)
fmt.Println(row)
}
}
func TestTableStorage(t *testing.T) {
ctx, client, teardown := setupTableTest(t)
defer teardown()
cb := func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
data := `[{"a": "a"}, {"b": "b"}]`
stmt := spanner.Statement{
SQL: `INSERT INTO test_table (id, other_id, data) VALUES (@id, @other_id, @data)`,
Params: map[string]interface{}{
"id": "1",
"other_id": "111",
"data": data,
},
}
_, err := txn.Update(ctx, stmt)
if err != nil {
return fmt.Errorf("failed to perform insert, err: %w", err)
}
return nil
}
r, err := client.ReadWriteTransaction(ctx, cb)
fmt.Println(r)
fmt.Println(err)
checkInsertedData(client, ctx)
}
```
**Expected Behavior:**
The SELECT query should return the stored JSON data from the recommendations column without errors.
**Actual Behavior:**
The error rpc error: code = Unknown desc = unhandled base type 8 occurs when trying to read the recommendations column of type JSON.
**Environment:**
Go version: go1.21.4
Spanner client version: cloud.google.com/go/spanner v1.23.0
Spanner Emulator: spannertest
This focuses specifically on the issue occurring during the SELECT query on the JSON column.
Contributor guide
Assessment
This issue has not been assessed yet.