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

Spanner should have helpful aliases for param types #3364

Closed
theacodes opened this issue May 3, 2017 · 10 comments
Closed

Spanner should have helpful aliases for param types #3364

theacodes opened this issue May 3, 2017 · 10 comments
Assignees
Labels
api: spanner Issues related to the Spanner API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@theacodes
Copy link
Contributor

Having to do:

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

instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

params = {
    'start': start,
    'end': end
}
param_types = {
    'start': type_pb2.Type(code=type_pb2.STRING),
    'end': type_pb2.Type(code=type_pb2.STRING)
}
results = database.execute_sql(
    "SELECT AlbumId, AlbumTitle, MarketingBudget "
    "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle} "
    "WHERE AlbumTitle >= @start AND AlbumTitle < @end",
    params=params, param_types=param_types)

Is rather awkward, it would be much more friendly to have:

from google.cloud import spanner

instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

params = {
    'start': start,
    'end': end
}
param_types = {
    'start': spanner.ParamTypes.STRING,
    'end': spanner.ParamTypes.STRING
}
results = database.execute_sql(
    "SELECT AlbumId, AlbumTitle, MarketingBudget "
    "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle} "
    "WHERE AlbumTitle >= @start AND AlbumTitle < @end",
    params=params, param_types=param_types)

or similar

@theacodes theacodes added the api: spanner Issues related to the Spanner API. label May 3, 2017
@tseaver tseaver added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label May 16, 2017
@tseaver
Copy link
Contributor

tseaver commented Jul 25, 2017

@jonparrott Aliasing the "scalar" types is easy (PR #3670). What do you envision for array / struct?

@theacodes
Copy link
Contributor Author

@tseaver would list and dict work, e.g.:

param_types = {
     tags: list,
     metadata: dict
}

@theacodes
Copy link
Contributor Author

Oh nevermind, ignore my last comment, I'm being thick.

What does it look like today to if you need to specify an array / struct?

@tseaver
Copy link
Contributor

tseaver commented Jul 27, 2017

@jonparrott For arrays, you have to pass the type of the elements:

array_type = type_pb2.Type(
    code=type_pb2.ARRAY, array_element_type=type_pb2.Type(code=type_pb2.INT64))

For structs, you have to construct / pass a StructType describing the members:

struct_type_pb = type_pb2.StructType(fields=[
    type_pb2.StructType.Field(name='name', type=type_pb2.Type(code=type_pb2.STRING)),
    type_pb2.StructType.Field(name='age', type=type_pb2.Type(code=type_pb2.INT64)),
])
my_struct_type = type_pb2.Type(code=type_pb2.STRUCT, struct_type=struct_type_pb)

@theacodes
Copy link
Contributor Author

theacodes commented Jul 28, 2017

@tseaver that's pretty rough. Is it possible to make it into something like:

from google.cloud.spanner import types

array_type = types.Array(types.INT64)
struct_type = types.Struct(
    types.StructField('name', types.STRING),
    types.StructField('age', types.INT64)),
)

@tseaver
Copy link
Contributor

tseaver commented Jul 28, 2017

@jonparrott Assuming you meant to have the open square bracket after types.Struct (the order matters) that should be straightforward.

@theacodes
Copy link
Contributor Author

Actually I didn't, I meant for types.Struct to just use *args. But yeah, let's do it. :)

@tseaver
Copy link
Contributor

tseaver commented Jul 28, 2017

@jonparrott Should we name those factories ArrayParamType / StructParamType (to be equivalent to the FOO_PARAM_TYPE aliases?

@theacodes
Copy link
Contributor Author

Sure, if that's all they're used for.

@tseaver
Copy link
Contributor

tseaver commented Jul 28, 2017

Conceptually they describe types of returned cell values, too, but we don't expose that.

landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 21, 2017
landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 21, 2017
landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 22, 2017
landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 22, 2017
landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 22, 2017
landrito pushed a commit to landrito/google-cloud-python that referenced this issue Aug 22, 2017
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. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

2 participants