tsungchih commented on code in PR #9870: URL: https://github.com/apache/gravitino/pull/9870#discussion_r2776822078
########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,833 @@ +# 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 typing +from abc import 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._helper.serdes_utils import SerdesUtils +from gravitino.api.rel.types.type import Type +from gravitino.rest.rest_message import RESTRequest + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest): + """Base class for all table update requests.""" + + _type: str = field(metadata=config(field_name="@type")) + + def __init__(self, action_type: str) -> None: + self._type = action_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")) Review Comment: `_new_schema_name` is missed here. ([ref](https://github.com/apache/gravitino/blob/40007c804c21572b9ec015e5e3c1b04665d4dcce/common/src/main/java/org/apache/gravitino/dto/requests/TableUpdateRequest.java#L108-L110)) ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,833 @@ +# 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 typing +from abc import 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._helper.serdes_utils import SerdesUtils +from gravitino.api.rel.types.type import Type +from gravitino.rest.rest_message import RESTRequest + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest): + """Base class for all table update requests.""" + + _type: str = field(metadata=config(field_name="@type")) + + def __init__(self, action_type: str) -> None: Review Comment: I would recommend to use [`__post_init__`](https://docs.python.org/3.9/library/dataclasses.html#post-init-processing) for initializing `_type` in its subclasses. Then this base class would look like. ```python @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""" ``` In turn, its subclass will look like. ```python @dataclass class RenameTableRequest(TableUpdateRequestBase): """ Update request to rename a table """ _new_name: str = field(metadata=config(field_name="newName")) def __post_init__(self): self._type = "rename" def validate(self) -> None: ... ``` ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,833 @@ +# 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 typing +from abc import 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._helper.serdes_utils import SerdesUtils +from gravitino.api.rel.types.type import Type +from gravitino.rest.rest_message import RESTRequest + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest): + """Base class for all table update requests.""" + + _type: str = field(metadata=config(field_name="@type")) + + def __init__(self, action_type: str) -> None: + self._type = action_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")) + + def __init__(self, new_name: str) -> None: + """ + Constructor for RenameTableRequest. + + Args: + new_name (str): the new name of the table + """ + super().__init__("rename") + self._new_name = new_name + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._new_name: + raise ValueError('"newName" field is required and cannot be empty') + + def get_new_name(self) -> str: + return self._new_name + + def table_change(self) -> TableChange.RenameTable: + return TableChange.rename(self._new_name) + + @dataclass_json + @dataclass + class UpdateTableCommentRequest(TableUpdateRequestBase): + """ + Update request to change a table comment + """ + + _new_comment: str = field(metadata=config(field_name="newComment")) + + def __init__(self, new_comment: str) -> None: + """ + Constructor for UpdateTableCommentRequest. + + Args: + new_comment (str): the new comment of the table + """ + super().__init__("updateComment") + 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 + + def get_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 __init__(self, prop: str, value: str) -> None: + """ + Constructor for SetTablePropertyRequest. + + Args: + pro (str): the property to set + value (str): the value to set + """ + super().__init__("setProperty") + self._property = prop + self._value = value + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._property: + raise ValueError('"property" field is required') + if not self._value: + raise ValueError('"value" field is required') + + def get_property(self) -> str: + return self._property + + def get_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 __init__(self, prop: str) -> None: + """ + Constructor for RemoveTablePropertyRequest. + + Args: + pro (str): the property to remove + """ + super().__init__("removeProperty") + self._property = prop + + def validate(self) -> None: + """ + Validates the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._property: + raise ValueError('"property" field is required') + + def get_property(self) -> str: + return self._property + + def table_change(self) -> TableChange.RemoveProperty: + return TableChange.remove_property(self._property) + + @dataclass_json + @dataclass + 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=SerdesUtils.write_data_type, + decoder=SerdesUtils.read_data_type, + ) + ) + _comment: typing.Optional[str] = field(metadata=config(field_name="comment")) + _position: typing.Optional[TableChange.ColumnPosition] = field( + metadata=config( + field_name="position", + encoder=SerdesUtils.column_position_encoder, + decoder=SerdesUtils.column_position_decoder, + ) + ) + _default_value: typing.Optional[Expression] = field( + metadata=config( + field_name="defaultValue", + encoder=SerdesUtils.column_default_value_encoder, + decoder=SerdesUtils.column_default_value_decoder, Review Comment: I think we can reuse the [implemented Ser/Des](https://github.com/apache/gravitino/blob/40007c804c21572b9ec015e5e3c1b04665d4dcce/clients/client-python/gravitino/dto/rel/expressions/json_serdes/column_default_value_serdes.py) for column default value. ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## Review Comment: For each subclass of `TableUpdateRequestBase`, I would recommend to - leverage [precondition](https://github.com/apache/gravitino/blob/40007c804c21572b9ec015e5e3c1b04665d4dcce/clients/client-python/gravitino/utils/precondition.py) in the `validate()` method; - use `@property` if we want a field to be public accessed. ########## clients/client-python/gravitino/dto/requests/table_update_request.py: ########## @@ -0,0 +1,833 @@ +# 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 typing +from abc import 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._helper.serdes_utils import SerdesUtils +from gravitino.api.rel.types.type import Type +from gravitino.rest.rest_message import RESTRequest + + +@dataclass_json +@dataclass +class TableUpdateRequestBase(RESTRequest): + """Base class for all table update requests.""" + + _type: str = field(metadata=config(field_name="@type")) + + def __init__(self, action_type: str) -> None: + self._type = action_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")) + + def __init__(self, new_name: str) -> None: + """ + Constructor for RenameTableRequest. + + Args: + new_name (str): the new name of the table + """ + super().__init__("rename") + self._new_name = new_name + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._new_name: + raise ValueError('"newName" field is required and cannot be empty') + + def get_new_name(self) -> str: + return self._new_name + + def table_change(self) -> TableChange.RenameTable: + return TableChange.rename(self._new_name) + + @dataclass_json + @dataclass + class UpdateTableCommentRequest(TableUpdateRequestBase): + """ + Update request to change a table comment + """ + + _new_comment: str = field(metadata=config(field_name="newComment")) + + def __init__(self, new_comment: str) -> None: + """ + Constructor for UpdateTableCommentRequest. + + Args: + new_comment (str): the new comment of the table + """ + super().__init__("updateComment") + 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 + + def get_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 __init__(self, prop: str, value: str) -> None: + """ + Constructor for SetTablePropertyRequest. + + Args: + pro (str): the property to set + value (str): the value to set + """ + super().__init__("setProperty") + self._property = prop + self._value = value + + def validate(self) -> None: + """ + Validate the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._property: + raise ValueError('"property" field is required') + if not self._value: + raise ValueError('"value" field is required') + + def get_property(self) -> str: + return self._property + + def get_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 __init__(self, prop: str) -> None: + """ + Constructor for RemoveTablePropertyRequest. + + Args: + pro (str): the property to remove + """ + super().__init__("removeProperty") + self._property = prop + + def validate(self) -> None: + """ + Validates the request. + + Raises: + ValueError: If the request is invalid, this exception is thrown. + """ + if not self._property: + raise ValueError('"property" field is required') + + def get_property(self) -> str: + return self._property + + def table_change(self) -> TableChange.RemoveProperty: + return TableChange.remove_property(self._property) + + @dataclass_json + @dataclass + 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=SerdesUtils.write_data_type, + decoder=SerdesUtils.read_data_type, Review Comment: I think we can use the [implemented Ser/Des](https://github.com/apache/gravitino/blob/40007c804c21572b9ec015e5e3c1b04665d4dcce/clients/client-python/gravitino/api/rel/types/json_serdes/type_serdes.py) for `Type`. -- 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]
