Abyss-lord commented on code in PR #9870: URL: https://github.com/apache/gravitino/pull/9870#discussion_r2973570273
########## 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: fix -- 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]
