JonasJ-ap commented on code in PR #5870: URL: https://github.com/apache/iceberg/pull/5870#discussion_r996368485
########## python/pyiceberg/catalog/glue.py: ########## @@ -0,0 +1,247 @@ +# 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 uuid +from typing import ( + Any, + Dict, + List, + Optional, + Set, + Union, +) + +import boto3 + +from pyiceberg.catalog import ( + Catalog, + Identifier, + Properties, + PropertiesUpdateSummary, +) +from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchTableError, TableAlreadyExistsError +from pyiceberg.io import FileIO, load_file_io +from pyiceberg.schema import Schema +from pyiceberg.serializers import FromInputFile, ToOutputFile +from pyiceberg.table import Table +from pyiceberg.table.metadata import TableMetadata, new_table_metadata +from pyiceberg.table.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec +from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder +from pyiceberg.typedef import EMPTY_DICT + +ICEBERG = "ICEBERG" +EXTERNAL_TABLE_TYPE = "EXTERNAL_TABLE" + +PROP_TABLE_TYPE = "table_type" +PROP_WAREHOUSE = "warehouse" +PROP_METADATA_LOCATION = "metadata_location" + +PROP_GLUE_TABLE = "Table" +PROP_GLUE_TABLE_TYPE = "TableType" +PROP_GLUE_TABLE_DESCRIPTION = "description" +PROP_GLUE_TABLE_PARAMETERS = "Parameters" +PROP_GLUE_TABLE_DATABASE_NAME = "DatabaseName" +PROP_GLUE_TABLE_NAME = "Name" + +PROP_GLUE_DATABASE = "Database" +PROP_GLUE_DATABASE_LIST = "DatabaseList" +PROP_GLUE_DATABASE_NAME = "Name" +PROP_GLUE_DATABASE_LOCATION = "LocationUri" + + +def _construct_parameters(metadata_location: str) -> Properties: + return {PROP_TABLE_TYPE: ICEBERG, PROP_METADATA_LOCATION: metadata_location} + + +def _construct_table_input(table_name: str, metadata_location: str, properties: Properties) -> Dict[str, Any]: + table_input = { + PROP_GLUE_TABLE_NAME: table_name, + PROP_GLUE_TABLE_TYPE: EXTERNAL_TABLE_TYPE, + PROP_GLUE_TABLE_PARAMETERS: _construct_parameters(metadata_location), + } + + if table_description := properties.get(PROP_GLUE_TABLE_DESCRIPTION): + table_input[PROP_GLUE_TABLE_DESCRIPTION] = table_description + + return table_input + + +def _convert_glue_to_iceberg(glue_table: Dict[str, Any], io: FileIO) -> Table: + properties: Properties = glue_table[PROP_GLUE_TABLE_PARAMETERS] + + if PROP_TABLE_TYPE not in properties: + raise NoSuchTableError( + f"Property table_type missing, could not determine type: " + f"{glue_table[PROP_GLUE_TABLE_DATABASE_NAME]}.{glue_table[PROP_GLUE_TABLE_NAME]}" + ) + glue_table_type = properties.get(PROP_TABLE_TYPE) + if glue_table_type != ICEBERG: + raise NoSuchTableError( + f"Property table_type is {glue_table_type}, expected {ICEBERG}: " + f"{glue_table[PROP_GLUE_TABLE_DATABASE_NAME]}.{glue_table[PROP_GLUE_TABLE_NAME]}" + ) + if prop_meta_location := properties.get(PROP_METADATA_LOCATION): + metadata_location = prop_meta_location + else: + raise NoSuchTableError(f"Table property {PROP_METADATA_LOCATION} is missing") Review Comment: Thank you for your suggestion. I added an exception class called `NoSuchIcebergTableError` (similar to that in Java) and raised it when table type is not `iceberg`. I also find another exception called: https://github.com/apache/iceberg/blob/336d18acdb7dc0e783861151e2e871524cef9f6d/python/pyiceberg/exceptions.py#L79 which seems more suitable when a property is missing (like what happened here). Do you think it is a good idea to raise this exception here? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
