unknowntpo commented on code in PR #7357: URL: https://github.com/apache/gravitino/pull/7357#discussion_r2144436841
########## 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: Why not just calling `PartitionUtils.validate_field_existence` ? ``` PartitionUtils.validate_field_existence(columns, ref.field_name()) ``` ########## clients/client-python/gravitino/dto/rel/partition_utils.py: ########## @@ -0,0 +1,57 @@ +# 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 itertools import filterfalse +from typing import TYPE_CHECKING, List + +from gravitino.utils.precondition import Precondition + +if TYPE_CHECKING: + from gravitino.dto.rel.column_dto import ColumnDTO + + +class PartitionUtils: + """Validates the existence of the partition field in the table.""" + + @staticmethod + def validate_field_existence( + columns: List["ColumnDTO"], field_name: List[str] + ) -> None: + """Validates the existence of the partition field in the table. + + Args: + columns (List[ColumnDTO]): The columns of the table. + field_name (List[str]): The name of the field to validate. + + Raises: + IllegalArgumentException: If the field does not exist in the table, this exception is thrown. + """ + Precondition.check_argument( Review Comment: I think it will be great to keep implementation of `PartitionUtils.validate_field_existence` the same as `Java`. ``` public static void validateFieldExistence(ColumnDTO[] columns, String[] fieldName) throws IllegalArgumentException { Preconditions.checkArgument(ArrayUtils.isNotEmpty(columns), "columns cannot be null or empty"); List<ColumnDTO> partitionColumn = Arrays.stream(columns) // (TODO) Need to consider the case sensitivity issues. // To be optimized. .filter(c -> c.name().equalsIgnoreCase(fieldName[0])) .collect(Collectors.toList()); Preconditions.checkArgument( partitionColumn.size() == 1, "Field '%s' not found in table", fieldName[0]); // TODO: should validate nested fieldName after column type support namedStruct } ``` , for example: ``` @staticmethod def validate_field_existence( columns: List["ColumnDTO"], field_name: List[str] ) -> None: """Validates the existence of the partition field in the table. Args: columns (List[ColumnDTO]): The columns of the table. field_name (List[str]): The name of the field to validate. Raises: IllegalArgumentException: If the field does not exist in the table, this exception is thrown. """ Precondition.check_argument( columns is not None and len(columns) > 0, "columns cannot be null or empty" ) # Filter columns that match the first field name (case-insensitive) # TODO: Need to consider the case sensitivity issues. To be optimized. partition_column = [ c for c in columns if c.name().lower() == field_name[0].lower() ] Precondition.check_argument( len(partition_column) == 1, f"Field '{field_name[0]}' not found in table" ) # TODO: should validate nested fieldName after column type support namedStruct ``` which is more intuitive in my opinion. -- 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]
