Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions google/cloud/bigquery/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import enum
import six

from google.cloud.bigquery_v2.gapic import enums as gapic_enums
from google.cloud.bigquery_v2 import types as gapic_types


_SQL_SCALAR_TYPES = frozenset(
Expand Down Expand Up @@ -46,13 +46,13 @@ def _make_sql_scalars_enum():
"StandardSqlDataTypes",
(
(member.name, member.value)
for member in gapic_enums.StandardSqlDataType.TypeKind
for member in gapic_types.StandardSqlDataType.TypeKind
if member.name in _SQL_SCALAR_TYPES
),
)

# make sure the docstring for the new enum is also correct
orig_doc = gapic_enums.StandardSqlDataType.TypeKind.__doc__
orig_doc = gapic_types.StandardSqlDataType.TypeKind.__doc__
skip_pattern = re.compile(
"|".join(_SQL_NONSCALAR_TYPES)
+ "|because a JSON object" # the second description line of STRUCT member
Expand Down
9 changes: 5 additions & 4 deletions google/cloud/bigquery/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Model(object):
def __init__(self, model_ref):
# Use _proto on read-only properties to use it's built-in type
# conversion.
self._proto = types.Model()
self._proto = types.Model()._pb

# Use _properties on read-write properties to match the REST API
# semantics. The BigQuery API makes a distinction between an unset
Expand Down Expand Up @@ -306,7 +306,7 @@ def from_api_repr(cls, resource):
training_run["startTime"] = datetime_helpers.to_rfc3339(start_time)

this._proto = json_format.ParseDict(
resource, types.Model(), ignore_unknown_fields=True
resource, types.Model()._pb, ignore_unknown_fields=True
)
return this

Expand All @@ -326,7 +326,7 @@ class ModelReference(object):
"""

def __init__(self):
self._proto = types.ModelReference()
self._proto = types.ModelReference()._pb
self._properties = {}

@property
Expand Down Expand Up @@ -370,8 +370,9 @@ def from_api_repr(cls, resource):
# field values.
ref._properties = resource
ref._proto = json_format.ParseDict(
resource, types.ModelReference(), ignore_unknown_fields=True
resource, types.ModelReference()._pb, ignore_unknown_fields=True
)

return ref

@classmethod
Expand Down
18 changes: 12 additions & 6 deletions google/cloud/bigquery/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,17 @@ def return_type(self):
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["return_type"])
if not resource:
return resource

output = google.cloud.bigquery_v2.types.StandardSqlDataType()
output = json_format.ParseDict(resource, output, ignore_unknown_fields=True)
return output
raw_protobuf = json_format.ParseDict(
resource, output._pb, ignore_unknown_fields=True
)
return type(output).wrap(raw_protobuf)

@return_type.setter
def return_type(self, value):
if value:
resource = json_format.MessageToDict(value)
resource = json_format.MessageToDict(value._pb)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["return_type"]] = resource
Expand Down Expand Up @@ -357,14 +360,17 @@ def data_type(self):
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["data_type"])
if not resource:
return resource

output = google.cloud.bigquery_v2.types.StandardSqlDataType()
output = json_format.ParseDict(resource, output, ignore_unknown_fields=True)
return output
raw_protobuf = json_format.ParseDict(
resource, output._pb, ignore_unknown_fields=True
)
return type(output).wrap(raw_protobuf)

@data_type.setter
def data_type(self, value):
if value:
resource = json_format.MessageToDict(value)
resource = json_format.MessageToDict(value._pb)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["data_type"]] = resource
Expand Down
52 changes: 30 additions & 22 deletions google/cloud/bigquery/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@
# https://cloud.google.com/bigquery/data-types#legacy_sql_data_types
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types
LEGACY_TO_STANDARD_TYPES = {
"STRING": types.StandardSqlDataType.STRING,
"BYTES": types.StandardSqlDataType.BYTES,
"INTEGER": types.StandardSqlDataType.INT64,
"INT64": types.StandardSqlDataType.INT64,
"FLOAT": types.StandardSqlDataType.FLOAT64,
"FLOAT64": types.StandardSqlDataType.FLOAT64,
"NUMERIC": types.StandardSqlDataType.NUMERIC,
"BOOLEAN": types.StandardSqlDataType.BOOL,
"BOOL": types.StandardSqlDataType.BOOL,
"GEOGRAPHY": types.StandardSqlDataType.GEOGRAPHY,
"RECORD": types.StandardSqlDataType.STRUCT,
"STRUCT": types.StandardSqlDataType.STRUCT,
"TIMESTAMP": types.StandardSqlDataType.TIMESTAMP,
"DATE": types.StandardSqlDataType.DATE,
"TIME": types.StandardSqlDataType.TIME,
"DATETIME": types.StandardSqlDataType.DATETIME,
"STRING": types.StandardSqlDataType.TypeKind.STRING,
"BYTES": types.StandardSqlDataType.TypeKind.BYTES,
"INTEGER": types.StandardSqlDataType.TypeKind.INT64,
"INT64": types.StandardSqlDataType.TypeKind.INT64,
"FLOAT": types.StandardSqlDataType.TypeKind.FLOAT64,
"FLOAT64": types.StandardSqlDataType.TypeKind.FLOAT64,
"NUMERIC": types.StandardSqlDataType.TypeKind.NUMERIC,
"BOOLEAN": types.StandardSqlDataType.TypeKind.BOOL,
"BOOL": types.StandardSqlDataType.TypeKind.BOOL,
"GEOGRAPHY": types.StandardSqlDataType.TypeKind.GEOGRAPHY,
"RECORD": types.StandardSqlDataType.TypeKind.STRUCT,
"STRUCT": types.StandardSqlDataType.TypeKind.STRUCT,
"TIMESTAMP": types.StandardSqlDataType.TypeKind.TIMESTAMP,
"DATE": types.StandardSqlDataType.TypeKind.DATE,
"TIME": types.StandardSqlDataType.TypeKind.TIME,
"DATETIME": types.StandardSqlDataType.TypeKind.DATETIME,
# no direct conversion from ARRAY, the latter is represented by mode="REPEATED"
}
"""String names of the legacy SQL types to integer codes of Standard SQL types."""
Expand Down Expand Up @@ -209,26 +209,34 @@ def to_standard_sql(self):
sql_type = types.StandardSqlDataType()

if self.mode == "REPEATED":
sql_type.type_kind = types.StandardSqlDataType.ARRAY
sql_type.type_kind = types.StandardSqlDataType.TypeKind.ARRAY
else:
sql_type.type_kind = LEGACY_TO_STANDARD_TYPES.get(
self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED
self.field_type,
types.StandardSqlDataType.TypeKind.TYPE_KIND_UNSPECIFIED,
)

if sql_type.type_kind == types.StandardSqlDataType.ARRAY: # noqa: E721
if sql_type.type_kind == types.StandardSqlDataType.TypeKind.ARRAY: # noqa: E721
array_element_type = LEGACY_TO_STANDARD_TYPES.get(
self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED
self.field_type,
types.StandardSqlDataType.TypeKind.TYPE_KIND_UNSPECIFIED,
)
sql_type.array_element_type.type_kind = array_element_type

# ARRAY cannot directly contain other arrays, only scalar types and STRUCTs
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#array-type
if array_element_type == types.StandardSqlDataType.STRUCT: # noqa: E721
if (
array_element_type
== types.StandardSqlDataType.TypeKind.STRUCT # noqa: E721
):
sql_type.array_element_type.struct_type.fields.extend(
field.to_standard_sql() for field in self.fields
)

elif sql_type.type_kind == types.StandardSqlDataType.STRUCT: # noqa: E721
elif (
sql_type.type_kind
== types.StandardSqlDataType.TypeKind.STRUCT # noqa: E721
):
sql_type.struct_type.fields.extend(
field.to_standard_sql() for field in self.fields
)
Expand Down
14 changes: 1 addition & 13 deletions tests/unit/enums/test_standard_sql_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def enum_under_test():
@pytest.fixture
def gapic_enum():
"""The referential autogenerated enum the enum under test is based on."""
from google.cloud.bigquery_v2.gapic.enums import StandardSqlDataType
from google.cloud.bigquery_v2.types import StandardSqlDataType

return StandardSqlDataType.TypeKind

Expand All @@ -59,15 +59,3 @@ def test_standard_sql_types_enum_members(enum_under_test, gapic_enum):
for name in ("STRUCT", "ARRAY"):
assert name in gapic_enum.__members__
assert name not in enum_under_test.__members__


def test_standard_sql_types_enum_docstring(enum_under_test, gapic_enum):
assert "STRUCT (int):" not in enum_under_test.__doc__
assert "BOOL (int):" in enum_under_test.__doc__
assert "TIME (int):" in enum_under_test.__doc__

# All lines in the docstring should actually come from the original docstring,
# except for the header.
assert "An Enum of scalar SQL types." in enum_under_test.__doc__
doc_lines = enum_under_test.__doc__.splitlines()
assert set(doc_lines[1:]) <= set(gapic_enum.__doc__.splitlines())
6 changes: 3 additions & 3 deletions tests/unit/model/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pytest

import google.cloud._helpers
from google.cloud.bigquery_v2.gapic import enums
from google.cloud.bigquery_v2 import types

KMS_KEY_NAME = "projects/1/locations/us/keyRings/1/cryptoKeys/1"

Expand Down Expand Up @@ -117,7 +117,7 @@ def test_from_api_repr(target_class):
assert got.expires == expiration_time
assert got.description == u"A friendly description."
assert got.friendly_name == u"A friendly name."
assert got.model_type == enums.Model.ModelType.LOGISTIC_REGRESSION
assert got.model_type == types.Model.ModelType.LOGISTIC_REGRESSION
assert got.labels == {"greeting": u"こんにちは"}
assert got.encryption_configuration.kms_key_name == KMS_KEY_NAME
assert got.training_runs[0].training_options.initial_learn_rate == 1.0
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_from_api_repr_w_minimal_resource(target_class):
assert got.expires is None
assert got.description is None
assert got.friendly_name is None
assert got.model_type == enums.Model.ModelType.MODEL_TYPE_UNSPECIFIED
assert got.model_type == types.Model.ModelType.MODEL_TYPE_UNSPECIFIED
assert got.labels == {}
assert got.encryption_configuration is None
assert len(got.training_runs) == 0
Expand Down
9 changes: 4 additions & 5 deletions tests/unit/routine/test_routine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Google LLC
#
Expand Down Expand Up @@ -63,14 +62,14 @@ def test_ctor_w_properties(target_class):
RoutineArgument(
name="x",
data_type=bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
),
)
]
body = "x * 3"
language = "SQL"
return_type = bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
)
type_ = "SCALAR_FUNCTION"
description = "A routine description."
Expand Down Expand Up @@ -141,14 +140,14 @@ def test_from_api_repr(target_class):
RoutineArgument(
name="x",
data_type=bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
),
)
]
assert actual_routine.body == "42"
assert actual_routine.language == "SQL"
assert actual_routine.return_type == bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
)
assert actual_routine.type_ == "SCALAR_FUNCTION"
assert actual_routine._properties["someNewField"] == "someValue"
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/routine/test_routine_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def target_class():

def test_ctor(target_class):
data_type = bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
)
actual_arg = target_class(
name="field_name", kind="FIXED_TYPE", mode="IN", data_type=data_type
Expand All @@ -51,7 +51,7 @@ def test_from_api_repr(target_class):
assert actual_arg.kind == "FIXED_TYPE"
assert actual_arg.mode == "IN"
assert actual_arg.data_type == bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
)


Expand All @@ -72,7 +72,7 @@ def test_from_api_repr_w_unknown_fields(target_class):

def test_eq(target_class):
data_type = bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
)
arg = target_class(
name="field_name", kind="FIXED_TYPE", mode="IN", data_type=data_type
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ def test_update_routine(self):
RoutineArgument(
name="x",
data_type=bigquery_v2.types.StandardSqlDataType(
type_kind=bigquery_v2.enums.StandardSqlDataType.TypeKind.INT64
type_kind=bigquery_v2.types.StandardSqlDataType.TypeKind.INT64
),
)
]
Expand Down
Loading