jerryshao commented on code in PR #9870: URL: https://github.com/apache/gravitino/pull/9870#discussion_r2964581630
########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,952 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import builtins +import typing +from abc import ABC, abstractmethod +from dataclasses import dataclass, field + +from dataclasses_json import config, dataclass_json + +from gravitino.api.rel.expressions.expression import Expression +from gravitino.api.rel.indexes.index import Index +from gravitino.api.rel.indexes.indexes import Indexes +from gravitino.api.rel.table_change import ( + DeleteColumn, + RenameColumn, + TableChange, + UpdateColumnAutoIncrement, + UpdateColumnComment, + UpdateColumnDefaultValue, + UpdateColumnNullability, + UpdateColumnPosition, + UpdateColumnType, +) +from gravitino.api.rel.types.json_serdes import TypeSerdes +from gravitino.api.rel.types.type import Type +from gravitino.dto.rel.expressions.json_serdes.column_default_value_serdes import ( + ColumnDefaultValueSerdes, +) +from gravitino.dto.rel.indexes.json_serdes.index_serdes import IndexSerdes +from gravitino.dto.rel.json_serdes.column_position_serdes import ColumnPositionSerdes +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils import StringUtils +from gravitino.utils.precondition import Precondition + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest, ABC): + """Base class for all table update requests.""" + + _type: str = field(init=False, metadata=config(field_name="@type")) + + @abstractmethod + def table_change(self) -> TableChange: + """Convert to table change operation""" + pass + + +class TableUpdateRequest: + """Namespace for all table update request types.""" + + @dataclass_json + @dataclass + class RenameTableRequest(TableUpdateRequestBase): + """ + Update request to rename a table + """ + + _new_name: str = field(metadata=config(field_name="newName")) + _new_schema_name: typing.Optional[str] = field( + default=None, + metadata=config( + field_name="newSchemaName", + exclude=lambda value: value is None, + ), + ) + + def __post_init__(self) -> None: Review Comment: Every subclass defines both `__post_init__` and a manual `__init__` that calls `self.__post_init__()`. The manual `__init__` overrides the dataclass-generated one, making the `@dataclass` decorator's `init` generation redundant across all subclasses. The idiomatic pattern is to remove the manual `__init__` and let dataclass generate it, using only `__post_init__` to set `_type`:\n\n\n\nThis applies to all subclasses and removes ~5 lines of boilerplate per class. ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,952 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import builtins +import typing +from abc import ABC, abstractmethod +from dataclasses import dataclass, field + +from dataclasses_json import config, dataclass_json + +from gravitino.api.rel.expressions.expression import Expression +from gravitino.api.rel.indexes.index import Index +from gravitino.api.rel.indexes.indexes import Indexes +from gravitino.api.rel.table_change import ( + DeleteColumn, + RenameColumn, + TableChange, + UpdateColumnAutoIncrement, + UpdateColumnComment, + UpdateColumnDefaultValue, + UpdateColumnNullability, + UpdateColumnPosition, + UpdateColumnType, +) +from gravitino.api.rel.types.json_serdes import TypeSerdes +from gravitino.api.rel.types.type import Type +from gravitino.dto.rel.expressions.json_serdes.column_default_value_serdes import ( + ColumnDefaultValueSerdes, +) +from gravitino.dto.rel.indexes.json_serdes.index_serdes import IndexSerdes +from gravitino.dto.rel.json_serdes.column_position_serdes import ColumnPositionSerdes +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils import StringUtils +from gravitino.utils.precondition import Precondition + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest, ABC): + """Base class for all table update requests.""" + + _type: str = field(init=False, metadata=config(field_name="@type")) + + @abstractmethod + def table_change(self) -> TableChange: + """Convert to table change operation""" + pass + + +class TableUpdateRequest: + """Namespace for all table update request types.""" + + @dataclass_json + @dataclass + class RenameTableRequest(TableUpdateRequestBase): + """ + Update request to rename a table + """ + + _new_name: str = field(metadata=config(field_name="newName")) + _new_schema_name: typing.Optional[str] = field( + default=None, + metadata=config( + field_name="newSchemaName", + exclude=lambda value: value is None, + ), + ) + + def __post_init__(self) -> None: + self._type = "rename" + + def __init__( + self, new_name: str, new_schema_name: typing.Optional[str] = None + ) -> None: + """ + Constructor for RenameTableRequest. + + Args: + new_name (str): the new name of the table + """ + self.__post_init__() + self._new_name = new_name + self._new_schema_name = new_schema_name + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_string_not_empty( + self._new_name, + '"newName" field is required and cannot be empty', + ) + + @property + def new_name(self) -> str: + return self._new_name + + @property + def new_schema_name(self) -> typing.Optional[str]: + return self._new_schema_name + + def table_change(self) -> TableChange.RenameTable: + return TableChange.rename(self._new_name, self._new_schema_name) + + @dataclass_json + @dataclass + class UpdateTableCommentRequest(TableUpdateRequestBase): + """ + Update request to change a table comment + """ + + _new_comment: str = field(metadata=config(field_name="newComment")) + + def __post_init__(self) -> None: + self._type = "updateComment" + + def __init__(self, new_comment: str) -> None: + """ + Constructor for UpdateTableCommentRequest. + + Args: + new_comment (str): the new comment of the table + """ + self.__post_init__() + self._new_comment = new_comment + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + # Validates the fields of the request. Always pass. + pass + + @property + def new_comment(self) -> str: + return self._new_comment + + def table_change(self) -> TableChange.UpdateComment: + return TableChange.update_comment(self._new_comment) + + @dataclass_json + @dataclass + class SetTablePropertyRequest(TableUpdateRequestBase): + """ + Update request to set a table property + """ + + _property: str = field(metadata=config(field_name="property")) + _value: str = field(metadata=config(field_name="value")) + + def __post_init__(self) -> None: + self._type = "setProperty" + + def __init__(self, prop: str, value: str) -> None: + """ + Constructor for SetTablePropertyRequest. + + Args: + pro (str): the property to set + value (str): the value to set + """ + self.__post_init__() + self._property = prop + self._value = value + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_string_not_empty( + self._property, + '"property" field is required', + ) + + Precondition.check_string_not_empty( + self._value, + '"value" field is required', + ) + + @property + def property(self) -> str: + return self._property + + @builtins.property Review Comment: Using `@builtins.property` is unconventional and will confuse readers. The conflict exists because the class also has a field named `_property`. Consider renaming the accessor to `get_value()` or `prop_value` to avoid the name clash and use the standard `@property` decorator instead. ########## clients/client-python/gravitino/utils/string_utils.py: ########## @@ -0,0 +1,42 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +class StringUtils: + @classmethod + def is_blank(cls, s: str) -> bool: + """Checks if a string is blank (null, empty, or only whitespace). + + Args: + s: The string to check. + + Returns: + True if the string is blank, False otherwise. + """ + return s is None or s.strip() == "" + + @classmethod + def is_not_blank(cls, s: str) -> bool: + """Checks if a string is not blank (null, empty, or only whitespace). + + Args: + s: The string to check. + + Returns: + True if the string is blank, False otherwise. Review Comment: Wrong docstring — says "True if the string is blank" but the method is `is_not_blank` and returns the opposite. Should read "True if the string is **not** blank, False otherwise." ########## clients/client-python/gravitino/dto/requests/table_updates_request.py: ########## @@ -0,0 +1,51 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +from dataclasses import dataclass, field + +from dataclasses_json import config + +from gravitino.dto.requests.table_update_request import TableUpdateRequest +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils.precondition import Precondition + + +@dataclass +class TableUpdatesRequest(RESTRequest): + """Represents a request to update a table.""" + + _updates: list[TableUpdateRequest] = field( + metadata=config(field_name="updates"), default_factory=list + ) + + def __init__(self, updates: list[TableUpdateRequest]) -> None: Review Comment: The `@dataclass` decorator's `__init__` generation is immediately overridden by the manual `__init__` below, so the decorator is doing nothing for initialization. Either remove the manual `__init__` and rely on the dataclass-generated one, or drop `@dataclass` if only the serialization from `@dataclass_json` is needed. ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,952 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import builtins +import typing +from abc import ABC, abstractmethod +from dataclasses import dataclass, field + +from dataclasses_json import config, dataclass_json + +from gravitino.api.rel.expressions.expression import Expression +from gravitino.api.rel.indexes.index import Index +from gravitino.api.rel.indexes.indexes import Indexes +from gravitino.api.rel.table_change import ( + DeleteColumn, + RenameColumn, + TableChange, + UpdateColumnAutoIncrement, + UpdateColumnComment, + UpdateColumnDefaultValue, + UpdateColumnNullability, + UpdateColumnPosition, + UpdateColumnType, +) +from gravitino.api.rel.types.json_serdes import TypeSerdes +from gravitino.api.rel.types.type import Type +from gravitino.dto.rel.expressions.json_serdes.column_default_value_serdes import ( + ColumnDefaultValueSerdes, +) +from gravitino.dto.rel.indexes.json_serdes.index_serdes import IndexSerdes +from gravitino.dto.rel.json_serdes.column_position_serdes import ColumnPositionSerdes +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils import StringUtils +from gravitino.utils.precondition import Precondition + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest, ABC): + """Base class for all table update requests.""" + + _type: str = field(init=False, metadata=config(field_name="@type")) + + @abstractmethod + def table_change(self) -> TableChange: + """Convert to table change operation""" + pass + + +class TableUpdateRequest: + """Namespace for all table update request types.""" + + @dataclass_json + @dataclass + class RenameTableRequest(TableUpdateRequestBase): + """ + Update request to rename a table + """ + + _new_name: str = field(metadata=config(field_name="newName")) + _new_schema_name: typing.Optional[str] = field( + default=None, + metadata=config( + field_name="newSchemaName", + exclude=lambda value: value is None, + ), + ) + + def __post_init__(self) -> None: + self._type = "rename" + + def __init__( + self, new_name: str, new_schema_name: typing.Optional[str] = None + ) -> None: + """ + Constructor for RenameTableRequest. + + Args: + new_name (str): the new name of the table + """ + self.__post_init__() + self._new_name = new_name + self._new_schema_name = new_schema_name + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_string_not_empty( + self._new_name, + '"newName" field is required and cannot be empty', + ) + + @property + def new_name(self) -> str: + return self._new_name + + @property + def new_schema_name(self) -> typing.Optional[str]: + return self._new_schema_name + + def table_change(self) -> TableChange.RenameTable: + return TableChange.rename(self._new_name, self._new_schema_name) + + @dataclass_json + @dataclass + class UpdateTableCommentRequest(TableUpdateRequestBase): + """ + Update request to change a table comment + """ + + _new_comment: str = field(metadata=config(field_name="newComment")) + + def __post_init__(self) -> None: + self._type = "updateComment" + + def __init__(self, new_comment: str) -> None: + """ + Constructor for UpdateTableCommentRequest. + + Args: + new_comment (str): the new comment of the table + """ + self.__post_init__() + self._new_comment = new_comment + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + # Validates the fields of the request. Always pass. + pass + + @property + def new_comment(self) -> str: + return self._new_comment + + def table_change(self) -> TableChange.UpdateComment: + return TableChange.update_comment(self._new_comment) + + @dataclass_json + @dataclass + class SetTablePropertyRequest(TableUpdateRequestBase): + """ + Update request to set a table property + """ + + _property: str = field(metadata=config(field_name="property")) + _value: str = field(metadata=config(field_name="value")) + + def __post_init__(self) -> None: + self._type = "setProperty" + + def __init__(self, prop: str, value: str) -> None: + """ + Constructor for SetTablePropertyRequest. + + Args: + pro (str): the property to set + value (str): the value to set + """ + self.__post_init__() + self._property = prop + self._value = value + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_string_not_empty( + self._property, + '"property" field is required', + ) + + Precondition.check_string_not_empty( + self._value, + '"value" field is required', + ) + + @property + def property(self) -> str: + return self._property + + @builtins.property + def value(self) -> str: + return self._value + + def table_change(self) -> TableChange.SetProperty: + return TableChange.set_property(self._property, self._value) + + @dataclass_json + @dataclass + class RemoveTablePropertyRequest(TableUpdateRequestBase): + """ + Update request to remove a table property + """ + + _property: str = field(metadata=config(field_name="property")) + + def __post_init__(self) -> None: + self._type = "removeProperty" + + def __init__(self, prop: str) -> None: + """ + Constructor for RemoveTablePropertyRequest. + + Args: + pro (str): the property to remove + """ + self.__post_init__() + self._property = prop + + def validate(self) -> None: + """ + Validates the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_string_not_empty( + self._property, + '"property" field is required', + ) + + @property + def property(self) -> str: + return self._property + + def table_change(self) -> TableChange.RemoveProperty: + return TableChange.remove_property(self._property) + + @dataclass_json + @dataclass + # pylint: disable=too-many-instance-attributes + class AddTableColumnRequest(TableUpdateRequestBase): + """Represents a request to add a column to a table.""" + + _field_name: list[str] = field(metadata=config(field_name="fieldName")) + _data_type: Type = field( + metadata=config( + field_name="type", + encoder=TypeSerdes.serialize, + decoder=TypeSerdes.deserialize, + ) + ) + _comment: typing.Optional[str] = field(metadata=config(field_name="comment")) + _position: typing.Optional[TableChange.ColumnPosition] = field( + metadata=config( + field_name="position", + encoder=ColumnPositionSerdes.serialize, + decoder=ColumnPositionSerdes.deserialize, + ) + ) + _default_value: typing.Optional[Expression] = field( + metadata=config( + field_name="defaultValue", + encoder=ColumnDefaultValueSerdes.serialize, + decoder=ColumnDefaultValueSerdes.deserialize, + exclude=lambda v: v is None, + ) + ) + _nullable: bool = field(default=True, metadata=config(field_name="nullable")) + _auto_increment: bool = field( + default=False, metadata=config(field_name="autoIncrement") + ) + + def __post_init__(self) -> None: + self._type = "addColumn" + + def __init__( + self, + field_name: list[str], + data_type: Type, + comment: typing.Optional[str], + position: TableChange.ColumnPosition, + default_value: typing.Optional[Expression], + nullable: bool, + auto_increment: bool, + ) -> None: + """ + Constructor for AddTableColumnRequest. + + Args: + field_name (list[str]): the field name to add + data_type (Type): the data type of the field to add + comment (typing.Optional[str]): the comment of the field to add + position (TableChange.ColumnPosition): the position of the field to add, null for default position + default_value (Expression): whether the field has default value + nullable (bool): whether the field to add is nullable + auto_increment (bool): whether the field to add is auto increment + """ + self.__post_init__() + self._field_name = field_name + self._data_type = data_type + self._comment = comment + self._position = position + self._default_value = default_value + self._nullable = nullable + self._auto_increment = auto_increment + + def validate(self) -> None: + """ + Validates the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + Precondition.check_argument( + self._field_name, + "Field name must be specified", + ) + Precondition.check_argument( + all(StringUtils.is_not_blank(name) for name in self._field_name), + 'elements in "field_name" cannot be empty', + ) + Precondition.check_argument( + self._data_type is not None, + '"type" field is required and cannot be empty', + ) + + @property + def field_name(self) -> list[str]: + return self._field_name + + @property + def data_type(self) -> Type: + return self._data_type + + @property + def comment(self) -> typing.Optional[str]: + return self._comment + + @property + def position(self) -> typing.Optional[TableChange.ColumnPosition]: + return self._position + + @property + def default_value(self) -> typing.Optional[Expression]: + return self._default_value + + @property + def is_nullable(self) -> bool: + return self._nullable + + @property + def is_auto_increment(self) -> bool: + return self._auto_increment + + def table_change(self): Review Comment: Missing return type annotation. All other `table_change()` overrides have a return type; this one is inconsistent. Should be `def table_change(self) -> TableChange.AddColumn:`. ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,952 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import builtins +import typing +from abc import ABC, abstractmethod +from dataclasses import dataclass, field + +from dataclasses_json import config, dataclass_json + +from gravitino.api.rel.expressions.expression import Expression +from gravitino.api.rel.indexes.index import Index +from gravitino.api.rel.indexes.indexes import Indexes +from gravitino.api.rel.table_change import ( + DeleteColumn, + RenameColumn, + TableChange, + UpdateColumnAutoIncrement, + UpdateColumnComment, + UpdateColumnDefaultValue, + UpdateColumnNullability, + UpdateColumnPosition, + UpdateColumnType, +) +from gravitino.api.rel.types.json_serdes import TypeSerdes +from gravitino.api.rel.types.type import Type +from gravitino.dto.rel.expressions.json_serdes.column_default_value_serdes import ( + ColumnDefaultValueSerdes, +) +from gravitino.dto.rel.indexes.json_serdes.index_serdes import IndexSerdes +from gravitino.dto.rel.json_serdes.column_position_serdes import ColumnPositionSerdes +from gravitino.rest.rest_message import RESTRequest +from gravitino.utils import StringUtils +from gravitino.utils.precondition import Precondition + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest, ABC): + """Base class for all table update requests.""" + + _type: str = field(init=False, metadata=config(field_name="@type")) + + @abstractmethod + def table_change(self) -> TableChange: + """Convert to table change operation""" + pass + + +class TableUpdateRequest: + """Namespace for all table update request types.""" + + @dataclass_json + @dataclass + class RenameTableRequest(TableUpdateRequestBase): + """ + Update request to rename a table + """ + + _new_name: str = field(metadata=config(field_name="newName")) + _new_schema_name: typing.Optional[str] = field( + default=None, + metadata=config( + field_name="newSchemaName", + exclude=lambda value: value is None, + ), + ) + + def __post_init__(self) -> None: Review Comment: Every subclass defines both `__post_init__` and a manual `__init__` that calls `self.__post_init__()`. The manual `__init__` overrides the dataclass-generated one, making the `@dataclass` decorator's init generation redundant across all subclasses. The idiomatic pattern is to remove the manual `__init__` and let dataclass generate it, using only `__post_init__` to set `_type`: ```python @dataclass_json @dataclass class RenameTableRequest(TableUpdateRequestBase): _new_name: str = field(metadata=config(field_name="newName")) def __post_init__(self): self._type = "rename" ``` This applies to all subclasses and removes ~5 lines of boilerplate per class. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
