MonkeyCanCode commented on code in PR #4075: URL: https://github.com/apache/polaris/pull/4075#discussion_r3019572569
########## client/python/apache_polaris/cli/command/tables.py: ########## @@ -0,0 +1,265 @@ +# +# 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 dataclasses import dataclass +from typing import List + +from apache_polaris.cli.command import Command +from apache_polaris.cli.command.utils import get_catalog_api_client +from apache_polaris.cli.constants import Subcommands, Arguments, UNIT_SEPARATOR +from apache_polaris.cli.options.option_tree import Argument +from apache_polaris.sdk.catalog import IcebergCatalogAPI +from apache_polaris.sdk.management import PolarisDefaultApi +from apache_polaris.sdk.catalog.api.policy_api import PolicyAPI +from apache_polaris.cli.command.utils import ( + handle_api_exception, + format_timestamp, + format_iceberg_type, +) +from prettytable import PrettyTable + + +@dataclass +class TableCommand(Command): + """ + A Command implemtation to represent `polaris tables`. It manages Iceberg tables within a Polaris Catalog. + + Example commands: + * ./polaris tables list --catalog my_catalog --namespace ns1 + * ./polaris tables get my_table --catalog my_catalog --namespace ns1 + * ./polaris tables summarize my_table --catalog my_catalog --namespace ns1 + * ./polaris tables delete my_table --catalog my_catalog --namesapce ns1 + """ + + table_subcommand: str + catalog_name: str + namespace: List[str] + table_name: str + + def validate(self) -> None: + if not self.catalog_name: + raise Exception( + f"Missing required argument: {Argument.to_flag_name(Arguments.CATALOG)}" + ) + if not self.namespace: + raise Exception( + f"Missing required argument: {Argument.to_flag_name(Arguments.NAMESPACE)}" + ) + if ( + self.table_subcommand == Subcommands.GET + or self.table_subcommand == Subcommands.SUMMARIZE + or self.table_subcommand == Subcommands.DELETE + ): + if not self.table_name.strip(): + raise Exception("The table name cannot be empty.") + + def execute(self, api: PolarisDefaultApi) -> None: + catalog_api = IcebergCatalogAPI(get_catalog_api_client(api)) + ns_str = UNIT_SEPARATOR.join(self.namespace) + if self.table_subcommand == Subcommands.LIST: + try: + result = catalog_api.list_tables( + prefix=self.catalog_name, namespace=ns_str + ) + for table_identifier in result.identifiers: + print(table_identifier.to_json()) + except Exception as e: + handle_api_exception(f"Table Listing ({'.'.join(self.namespace)})", e) + elif self.table_subcommand == Subcommands.GET: + try: + print( + catalog_api.load_table( + prefix=self.catalog_name, + namespace=ns_str, + table=self.table_name, + ).to_json() + ) + except Exception as e: + handle_api_exception(f"Table Load ({self.table_name})", e) + elif self.table_subcommand == Subcommands.DELETE: + print( + f"De-registering table {'.'.join(self.namespace)}.{self.table_name}..." + ) + try: + catalog_api.drop_table( + prefix=self.catalog_name, + namespace=ns_str, + table=self.table_name, + purge_requested=False, + ) + except Exception as e: + handle_api_exception(f"Table De-registration ({self.table_name})", e) + print( Review Comment: Good catch on this. Fixed now. -- 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]
