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

spannertest: error getting data with json #10949

Open
jfajardo opened this issue Oct 4, 2024 · 0 comments
Open

spannertest: error getting data with json #10949

jfajardo opened this issue Oct 4, 2024 · 0 comments
Assignees
Labels
api: spanner Issues related to the Spanner API. triage me I really want to be triaged.

Comments

@jfajardo
Copy link

jfajardo commented Oct 4, 2024

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.

@jfajardo jfajardo added the triage me I really want to be triaged. label Oct 4, 2024
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

2 participants