Copilot commented on code in PR #12040: URL: https://github.com/apache/gravitino/pull/12040#discussion_r3600937648
########## clients/client-python/gravitino/api/rel/view_change.py: ########## @@ -0,0 +1,214 @@ +# 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 abc import ABC +from dataclasses import dataclass +from typing import Optional + +from gravitino.api.rel.column import Column +from gravitino.api.rel.representation import Representation +from gravitino.utils.precondition import Precondition + + +class ViewChange(ABC): + """Defines changes that can be applied to a view.""" + + @staticmethod + def rename(new_name: str) -> "RenameView": + """Create a change for renaming a view.""" + return RenameView(new_name) + + @staticmethod + def set_property(property_name: str, value: str) -> "SetProperty": + """Create a change for setting a view property.""" + return SetProperty(property_name, value) + + @staticmethod + def remove_property(property_name: str) -> "RemoveProperty": + """Create a change for removing a view property.""" + return RemoveProperty(property_name) + + @staticmethod + def replace_view( + columns: list[Column], + representations: list[Representation], + default_catalog: Optional[str] = None, + default_schema: Optional[str] = None, + comment: Optional[str] = None, + ) -> "ReplaceView": + """Create a change for replacing the view body.""" + return ReplaceView( + columns, representations, default_catalog, default_schema, comment + ) + + +@dataclass(frozen=True) +class RenameView(ViewChange): + """A ViewChange to rename a view.""" + + _new_name: str + + def __post_init__(self): + Precondition.check_string_not_empty( + self._new_name, "newName must not be null or empty" + ) + + def new_name(self) -> str: + """Returns the new view name.""" + return self._new_name + + def __eq__(self, other: object) -> bool: + if not isinstance(other, RenameView): + return False + return self._new_name == other.new_name() + + def __hash__(self) -> int: + return hash(self._new_name) + + def __str__(self) -> str: + return f"RENAMEVIEW {self._new_name}" + + +@dataclass(frozen=True) +class SetProperty(ViewChange): + """A ViewChange to set a view property.""" + + _property: str + _value: str + + def __post_init__(self): + Precondition.check_string_not_empty( + self._property, "property must not be null or empty" + ) + Review Comment: `SetProperty.__post_init__` validates only the property name; it does not validate `_value`. This allows creating `ViewChange.set_property("k", "")`, which is inconsistent with existing update request validation patterns (e.g., table setProperty requires a non-empty value) and will likely fail later at request/serialization time. ########## clients/client-python/tests/unittests/api/rel/test_view_change.py: ########## @@ -0,0 +1,150 @@ +# 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. + +import unittest + +from gravitino.api.rel.column import Column +from gravitino.api.rel.dialects import Dialects +from gravitino.api.rel.sql_representation import SQLRepresentation +from gravitino.api.rel.types.types import Types +from gravitino.api.rel.view_change import ( + RemoveProperty, + RenameView, + ReplaceView, + SetProperty, + ViewChange, +) + + +class TestViewChange(unittest.TestCase): + def test_rename_view(self): + change = ViewChange.rename("new_view") + equal_change = ViewChange.rename("new_view") + + self.assertIsInstance(change, RenameView) + self.assertEqual("new_view", change.new_name()) + self.assertEqual("RENAMEVIEW new_view", str(change)) + self.assertEqual(change, equal_change) + self.assertEqual(hash(change), hash(equal_change)) + self.assertNotEqual(change, ViewChange.rename("another_view")) + self.assertNotEqual(change, "invalid_change") + + with self.assertRaisesRegex(ValueError, "newName must not be null or empty"): + ViewChange.rename("") + + def test_set_property(self): + change = ViewChange.set_property("key", "value") + equal_change = ViewChange.set_property("key", "value") + + self.assertIsInstance(change, SetProperty) + self.assertEqual("key", change.property()) + self.assertEqual("value", change.value()) + self.assertEqual("SETPROPERTY key value", str(change)) + self.assertEqual(change, equal_change) + self.assertEqual(hash(change), hash(equal_change)) + self.assertNotEqual(change, ViewChange.set_property("key", "another_value")) + self.assertNotEqual(change, "invalid_change") + + with self.assertRaisesRegex(ValueError, "property must not be null or empty"): + ViewChange.set_property("", "value") + Review Comment: After adding non-empty validation for `SetProperty` values, the unit tests should cover the empty-value case to prevent regressions. -- 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]
