tsungchih commented on code in PR #7904:
URL: https://github.com/apache/gravitino/pull/7904#discussion_r2257495997


##########
clients/client-python/gravitino/utils/serdes.py:
##########
@@ -0,0 +1,104 @@
+# 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 re
+from collections.abc import Mapping
+from types import MappingProxyType
+from typing import Final, Pattern, Set
+
+from gravitino.api.types.types import Name, Types
+
+
+class SerdesUtilsBase:
+    EXPRESSION_TYPE: Final[str] = "type"
+    DATA_TYPE: Final[str] = "dataType"
+    LITERAL_VALUE: Final[str] = "value"
+    FIELD_NAME: Final[str] = "fieldName"
+    FUNCTION_NAME: Final[str] = "funcName"
+    FUNCTION_ARGS: Final[str] = "funcArgs"
+    UNPARSED_EXPRESSION: Final[str] = "unparsedExpression"
+    TYPE: Final[str] = "type"
+    STRUCT: Final[str] = "struct"
+    FIELDS: Final[str] = "fields"
+    STRUCT_FIELD_NAME: Final[str] = "name"
+    STRUCT_FIELD_NULLABLE: Final[str] = "nullable"
+    STRUCT_FIELD_COMMENT: Final[str] = "comment"
+    LIST: Final[str] = "list"
+    LIST_ELEMENT_TYPE: Final[str] = "elementType"
+    LIST_ELEMENT_NULLABLE: Final[str] = "containsNull"
+    MAP: Final[str] = "map"
+    MAP_KEY_TYPE: Final[str] = "keyType"
+    MAP_VALUE_TYPE: Final[str] = "valueType"
+    MAP_VALUE_NULLABLE: Final[str] = "valueContainsNull"
+    UNION: Final[str] = "union"
+    UNION_TYPES: Final[str] = "types"
+    UNPARSED: Final[str] = "unparsed"
+    UNPARSED_TYPE: Final[str] = "unparsedType"
+    EXTERNAL: Final[str] = "external"
+    CATALOG_STRING: Final[str] = "catalogString"
+
+    PARTITION_TYPE: Final[str] = "type"
+    PARTITION_NAME: Final[str] = "name"
+    FIELD_NAMES: Final[str] = "fieldNames"
+    IDENTITY_PARTITION_VALUES: Final[str] = "values"
+    LIST_PARTITION_LISTS: Final[str] = "lists"
+    RANGE_PARTITION_UPPER: Final[str] = "upper"
+    RANGE_PARTITION_LOWER: Final[str] = "lower"
+
+    NON_PRIMITIVE_TYPES: Final[Set[Name]] = {
+        Name.STRUCT,
+        Name.LIST,
+        Name.MAP,
+        Name.UNION,
+        Name.UNPARSED,
+        Name.EXTERNAL,
+    }
+    PRIMITIVE_AND_NULL_TYPES: Final[Set[Name]] = set(list(Name)) - 
NON_PRIMITIVE_TYPES
+
+    DECIMAL_PATTERN: Final[Pattern[str]] = re.compile(
+        r"decimal\(\s*(\d+)\s*,\s*(\d+)\s*\)"
+    )
+    FIXED_PATTERN: Final[Pattern[str]] = re.compile(r"fixed\(\s*(\d+)\s*\)")
+    FIXEDCHAR_PATTERN: Final[Pattern[str]] = re.compile(r"char\(\s*(\d+)\s*\)")
+    VARCHAR_PATTERN: Final[Pattern[str]] = 
re.compile(r"varchar\(\s*(\d+)\s*\)")
+    TYPES: Final[Mapping] = MappingProxyType(
+        {
+            type_instance.simple_string(): type_instance
+            for type_instance in (

Review Comment:
   @unknowntpo After having tried the suggestion from Copilot to use `set` 
instead, the build process failed at `pylint` with the following error messages.
   
   ```shell
   ************* Module gravitino.utils.serdes
   gravitino/utils/serdes.py:81:33: C0208: Use a sequence type when iterating 
over values (use-sequence-for-iteration)
   
   --------------------------------------------------------------------
   Your code has been rated at 10.00/10 (previous run: 10.00/10, -0.00)
   
   
   > Task :clients:client-python:pylint FAILED
   
   Build gravitino FAILURE reason:
       Execution failed for task ':clients:client-python:pylint':
           org.gradle.process.internal.ExecException: Process 'command 'sh'' 
finished with non-zero exit value 16
   ``` 
   
   We can decide which one of the following we are going to go for:
   1. Accept Copilot's suggestion to use `set` and ignore the pylint warning.
   2. Ignore Copilot's suggestion and accept the code as-is.
   
   I'm fine with either one proposed above since we intent to construct an 
immutable `Mapping` to embrace all valid `Type` instances as the values and 
their string expressions (`simple_string()`) as keys that are hashable. And the 
`dictionary` is characterized by its use of unique keys, in our case - the 
string expression of `Type` instances. Even if we iterate over a `tuple` of 
`Type` instances with duplicate elements, we can get equivalent outcome. Here's 
the experiment results.
   
   ```python
   >>> from gravitino.api.types.types import Types
   >>> t = (Types.NullType.get(), Types.BooleanType.get(), 
Types.BooleanType.get())
   >>> d = {instance.simple_string(): instance for instance in t}
   >>> len(d)
   2
   >>> print(d.keys())
   dict_keys(['null', 'boolean'])
   ```



-- 
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]

Reply via email to