1

当使用 spanner python 客户端(但问题可能更普遍)时,似乎不可能将结构作为参数传递给查询。

考虑以下设置:

from google.cloud.proto.spanner.v1 import type_pb2
from google.cloud import spanner

spanner_client = spanner.Client()
instance = spanner_client.instance('myinstance')
database = instance.database('mydb')

如果我想将以下查询转换为使用参数

select * from UNNEST(ARRAY[STRUCT<foo INT64, bar INT64>(1, 2)])

通过做

STRUCT_TYPE = type_pb2.StructType()
FOO = type_pb2.StructType.Field(name='foo', type=type_pb2.Type(code=type_pb2.INT64))
BAR = type_pb2.StructType.Field(name='bar', type=type_pb2.Type(code=type_pb2.INT64))

STRUCT_TYPE.fields.extend([FOO, BAR])

database.execute_sql('select * from UNNEST(ARRAY[@struct])', 
    params={'struct': [1,2]}, 
    param_types={'struct': STRUCT_TYPE})

会给我

TypeError: Parameter to MergeFrom() must be instance of same class: expected Type got StructType.

有什么方法可以传递结构吗?

4

1 回答 1

0

你现在不能传递结构。这是我们正在解决的问题,但没有确定的时间表。

于 2017-06-08T17:13:47.477 回答