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

feat: support range sql #1807

Merged
merged 9 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
add unit tests
  • Loading branch information
Linchin committed Mar 5, 2024
commit 427eb9c35071235ca239f8936e79b09f5f669009
5 changes: 3 additions & 2 deletions google/cloud/bigquery/standard_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class StandardSqlDataType:
]
}
}
RANGE: {type_kind="RANGE", range_element_type="DATETIME"}

Args:
type_kind:
Expand All @@ -53,7 +54,7 @@ class StandardSqlDataType:
struct_type:
The fields of this struct, in order, if type_kind is STRUCT.
range_element_type:
The type of the range's elements, if type_kind = "RANGE".
The type of the range's elements, if type_kind is RANGE.
"""

def __init__(
Expand Down Expand Up @@ -144,7 +145,7 @@ def range_element_type(self) -> Optional["StandardSqlDataType"]:
result._properties = range_element_info # We do not use a copy on purpose.
return result

@struct_type.setter
@range_element_type.setter
def range_element_type(self, value: Optional["StandardSqlDataType"]):
range_element_type = None if value is None else value.to_api_repr()

Expand Down
49 changes: 38 additions & 11 deletions tests/unit/test_standard_sql_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ def test_to_api_repr_struct_type_w_field_types(self):
}
assert result == expected

def test_to_api_repr_range_type_element_type_missing(self):
instance = self._make_one(bq.StandardSqlTypeNames.RANGE, range_element_type=None)

result = instance.to_api_repr()

assert result == {"typeKind": "RANGE"}

def test_to_api_repr_range_type_w_element_type(self):
range_element_type = self._make_one(type_kind=bq.StandardSqlTypeNames.DATE)
instance = self._make_one(bq.StandardSqlTypeNames.RANGE, range_element_type=range_element_type)

result = instance.to_api_repr()

assert result == {
"typeKind": "RANGE",
'rangeElementType': {'typeKind': 'DATE'},
}

def test_from_api_repr_empty_resource(self):
klass = self._get_target_class()
result = klass.from_api_repr(resource={})
Expand Down Expand Up @@ -276,21 +294,30 @@ def test_from_api_repr_struct_type_incomplete_field_info(self):
)
assert result == expected

def test_from_api_repr_range_type():
pass
def test_from_api_repr_range_type_full(self):
klass = self._get_target_class()
resource = {"typeKind": "RANGE", "rangeElementType": {"typeKind": "DATE"}}

def test_from_api_repr_range_type_missing_element():
pass
result = klass.from_api_repr(resource=resource)

def test_from_api_repr_range_type_invalid_element():
pass
expected = klass(
type_kind=bq.StandardSqlTypeNames.RANGE,
range_element_type=klass(type_kind=bq.StandardSqlTypeNames.DATE),
)
assert result == expected

def test_to_api_repr_range_type_element_type_missing():
pass
def test_from_api_repr_range_type_missing_element_type(self):
klass = self._get_target_class()
resource = {"typeKind": "RANGE"}

def test_to_api_repr_range_type_w_element_type():
pass
result = klass.from_api_repr(resource=resource)

expected = klass(
type_kind=bq.StandardSqlTypeNames.RANGE,
range_element_type=None,
struct_type=None,
)
assert result == expected

def test__eq__another_type(self):
instance = self._make_one()
Expand Down Expand Up @@ -340,7 +367,7 @@ def test__eq__similar_instance(self):
(
"range_element_type",
bq.StandardSqlDataType(type_kind=bq.StandardSqlTypeNames.DATE),
bq.StandardSqlDataType(type_kind=bq.StandardSqlTypeNames.DATE),
bq.StandardSqlDataType(type_kind=bq.StandardSqlTypeNames.DATETIME),
),
),
)
Expand Down