tsungchih commented on code in PR #7357: URL: https://github.com/apache/gravitino/pull/7357#discussion_r2147408842
########## clients/client-python/gravitino/dto/rel/expressions/function_arg.py: ########## @@ -0,0 +1,73 @@ +# 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 + +from abc import abstractmethod +from enum import Enum, unique +from typing import TYPE_CHECKING, ClassVar, List + +from gravitino.api.expressions.expression import Expression +from gravitino.dto.rel.partition_utils import PartitionUtils + +if TYPE_CHECKING: + from gravitino.dto.rel.column_dto import ColumnDTO + + +class FunctionArg(Expression): + """An argument of a function.""" + + EMPTY_ARGS: ClassVar[List[FunctionArg]] = [] + + @abstractmethod + def arg_type(self) -> ArgType: + """Arguments type of the function. + + Returns: + ArgType: The type of this argument. + """ + pass + + def validate(self, columns: List[ColumnDTO]) -> None: + """Validates the function argument. + + Args: + columns (List[ColumnDTO]): The columns of the table. + + Raises: + IllegalArgumentException: If the function argument is invalid. + """ + validate_field_existence = PartitionUtils.validate_field_existence + for ref in self.references(): + validate_field_existence(columns, ref.field_name()) Review Comment: Two intentions here not to directly call to `PartitionUtils.validate_field_existence`, one is to reduce the length of the line (it's more readable for me) and on the other hand is to speed up the for loop by reducing overhead of function lookups in the class. Here's the test results on my laptop using `timeit` to show the overhead of function lookups in the class. ```shell Profiling calling classmethod directly for running 100000 times for 5 iterations... Min: 0.018136 seconds, Max: 0.018823, Avg: 0.018469 Profiling calling classmethod via variable for running 100000 times for 5 iterations... Min: 0.011842 seconds, Max: 0.012436, Avg: 0.012046 ``` The setup statement is listed as follows. ```python import random class TestClass: @classmethod def get_random(cls) -> float: return random.random() get_random = TestClass.get_random ``` And the statement for each test case is `TestClass.get_random()` and `get_random()` respectively. -- 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]
