mchades commented on code in PR #12858: URL: https://github.com/apache/gravitino/pull/12858#discussion_r4083974510
########## clients/client-python/gravitino/api/semantic/dataset.py: ########## @@ -0,0 +1,165 @@ +# 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 typing import Optional + +from gravitino.api.semantic.ai_context import AIContext +from gravitino.api.semantic.custom_extension import CustomExtension +from gravitino.api.semantic.field import Field +from gravitino.api.semantic.semantic_utils import ( + check_no_none_elements, + check_non_empty_string_elements, +) +from gravitino.name_identifier import NameIdentifier +from gravitino.utils.precondition import Precondition + + +class Dataset: # pylint: disable=too-many-instance-attributes + """A source-backed dataset exposed by a Semantic Model. + + Dataset names are unique within a Semantic Model. The source is a three-part + `NameIdentifier` that must resolve to a table or a logical view in the same + metalake, inline query sources are not supported. + """ + + def __init__( + self, + name: str, + source: NameIdentifier, + primary_key: Optional[list[str]] = None, + unique_keys: Optional[list[list[str]]] = None, + description: Optional[str] = None, + ai_context: Optional[AIContext] = None, + fields: Optional[list[Field]] = None, + custom_extensions: Optional[list[CustomExtension]] = None, + ): + Precondition.check_argument( + name is not None and name != "", "name must not be null or empty" + ) + Precondition.check_argument(source is not None, "source must not be null") + check_non_empty_string_elements("primaryKey", primary_key) + _check_unique_keys(unique_keys) + check_no_none_elements("fields", fields) + check_no_none_elements("customExtensions", custom_extensions) + + self._name = name + self._source = source Review Comment: Python `NameIdentifier`/`Namespace` are mutable (`Namespace.levels()` returns its backing list). This stores the caller's `source` and returns the same object, so `source.namespace().levels()[0] = "other"` changes the already-built `Dataset` and the hash of its `SemanticModelDefinition`. Please defensively copy the identifier and namespace on input and output, and test both mutation paths. ########## clients/client-python/gravitino/api/semantic/ai_context_object.py: ########## @@ -0,0 +1,189 @@ +# 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 copy +import math +from typing import Any, Final, Optional + +from gravitino.api.semantic.semantic_utils import check_no_none_elements +from gravitino.exceptions.base import IllegalArgumentException +from gravitino.utils.precondition import Precondition + +MAX_ADDITIONAL_PROPERTY_NESTING_DEPTH: Final[int] = 100 + +_STANDARD_PROPERTIES: Final[frozenset] = frozenset( + {"instructions", "synonyms", "examples"} +) + + +class AIContextObject: + """The structured form of AI context attached to a Semantic Model member. + + Unknown JSON-compatible properties are exposed through + :meth:`additional_properties` and are retained losslessly. + """ + + def __init__( + self, + instructions: Optional[str] = None, + synonyms: Optional[list[str]] = None, + examples: Optional[list[str]] = None, + additional_properties: Optional[dict[str, Any]] = None, + ): + check_no_none_elements("synonyms", synonyms) + check_no_none_elements("examples", examples) + + self._instructions = instructions + self._synonyms = None if synonyms is None else list(synonyms) + self._examples = None if examples is None else list(examples) + self._additional_properties = _normalize_additional_properties( + additional_properties + ) + + def instructions(self) -> Optional[str]: + """Returns the free-form instructions, or `None` if it is not set.""" + return self._instructions + + def synonyms(self) -> Optional[list[str]]: + """Returns the synonyms, or `None` if they are not set.""" + return None if self._synonyms is None else list(self._synonyms) + + def examples(self) -> Optional[list[str]]: + """Returns the examples, or `None` if they are not set.""" + return None if self._examples is None else list(self._examples) + + def additional_properties(self) -> dict[str, Any]: + """Returns the additional JSON-compatible properties, empty if none are set.""" + return copy.deepcopy(self._additional_properties) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, AIContextObject): + return False + return ( + self._instructions == other.instructions() + and self._synonyms == other.synonyms() + and self._examples == other.examples() + and self._additional_properties == other.additional_properties() Review Comment: Python dict equality treats `True` and `1` as equal. Consequently, `AIContextObject(additional_properties={"value": True})` equals one with `{"value": 1}`, although the JSON payloads differ; definitions containing them also compare equal. Please compare normalized JSON values with type-aware semantics and add a boolean-versus-number regression test. ########## clients/client-python/gravitino/api/semantic/ai_context_object.py: ########## @@ -0,0 +1,189 @@ +# 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 copy +import math +from typing import Any, Final, Optional + +from gravitino.api.semantic.semantic_utils import check_no_none_elements +from gravitino.exceptions.base import IllegalArgumentException +from gravitino.utils.precondition import Precondition + +MAX_ADDITIONAL_PROPERTY_NESTING_DEPTH: Final[int] = 100 + +_STANDARD_PROPERTIES: Final[frozenset] = frozenset( + {"instructions", "synonyms", "examples"} +) + + +class AIContextObject: + """The structured form of AI context attached to a Semantic Model member. + + Unknown JSON-compatible properties are exposed through + :meth:`additional_properties` and are retained losslessly. + """ + + def __init__( + self, + instructions: Optional[str] = None, + synonyms: Optional[list[str]] = None, + examples: Optional[list[str]] = None, + additional_properties: Optional[dict[str, Any]] = None, + ): + check_no_none_elements("synonyms", synonyms) Review Comment: `synonyms` and `examples` use `check_no_none_elements`, so non-string entries are accepted. I reproduced `synonyms=[{"term": "sales"}]`: mutating the caller's dict changes this object, and `hash()` raises `TypeError`. Please validate string elements before storing them and add a test. The existing `check_non_empty_string_elements` comment does not cover this path. -- 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]
