-
Notifications
You must be signed in to change notification settings - Fork 581
Improve CLI describe entity detection #3934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+97
−28
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,8 +31,9 @@ | |
| from pyiceberg.cli.output import ConsoleOutput, JsonOutput, Output | ||
| from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchPropertyException, NoSuchTableError | ||
| from pyiceberg.io import WAREHOUSE | ||
| from pyiceberg.table import TableProperties | ||
| from pyiceberg.table import Table, TableProperties | ||
| from pyiceberg.table.refs import SnapshotRef, SnapshotRefType | ||
| from pyiceberg.typedef import Properties | ||
| from pyiceberg.utils.properties import property_as_int | ||
|
|
||
|
|
||
|
|
@@ -142,38 +143,62 @@ def list(ctx: Context, parent: str | None) -> None: # pylint: disable=redefined | |
|
|
||
|
|
||
| @run.command() | ||
| @click.option("--entity", type=click.Choice(["any", "namespace", "table"]), default="any") | ||
| @click.option( | ||
| "--entity", | ||
| type=click.Choice(["any", "namespace", "table"]), | ||
| default="any", | ||
| help="Entity type. 'any' auto-detects and requires --entity when ambiguous.", | ||
| ) | ||
| @click.argument("identifier") | ||
| @click.pass_context | ||
| @catch_exception() | ||
| def describe(ctx: Context, entity: Literal["name", "namespace", "table"], identifier: str) -> None: | ||
| def describe(ctx: Context, entity: Literal["any", "namespace", "table"], identifier: str) -> None: | ||
| """Describe a namespace or a table.""" | ||
| catalog, output = _catalog_and_output(ctx) | ||
| identifier_tuple = Catalog.identifier_to_tuple(identifier) | ||
|
|
||
| is_namespace = False | ||
| if entity in {"namespace", "any"} and len(identifier_tuple) > 0: | ||
| try: | ||
| namespace_properties = catalog.load_namespace_properties(identifier_tuple) | ||
| output.describe_properties(namespace_properties) | ||
| is_namespace = True | ||
| except NoSuchNamespaceError as exc: | ||
| if entity != "any" or len(identifier_tuple) == 1: # type: ignore | ||
| raise exc | ||
|
|
||
| is_table = False | ||
| if entity in {"table", "any"} and len(identifier_tuple) > 1: | ||
| try: | ||
| catalog_table = catalog.load_table(identifier) | ||
| output.describe_table(catalog_table) | ||
| is_table = True | ||
| except NoSuchTableError as exc: | ||
| if entity != "any": | ||
| raise exc | ||
|
|
||
| if is_namespace is False and is_table is False: | ||
| if entity == "namespace": | ||
| output.describe_properties(catalog.load_namespace_properties(identifier_tuple)) | ||
| return | ||
| if entity == "table": | ||
| output.describe_table(catalog.load_table(identifier)) | ||
| return | ||
|
|
||
| # For the default "any" entity, auto-detect the entity type. | ||
| if len(identifier_tuple) == 1: | ||
|
kevinjqliu marked this conversation as resolved.
|
||
| output.describe_properties(catalog.load_namespace_properties(identifier_tuple)) | ||
| return | ||
|
|
||
| matches: tuple[str, ...] = () | ||
| namespace_properties: Properties | None = None | ||
| catalog_table: Table | None = None | ||
|
|
||
| try: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: comment here describing that this is a namespace attempt could be good. |
||
| namespace_properties = catalog.load_namespace_properties(identifier_tuple) | ||
| matches += ("namespace",) | ||
| except NoSuchNamespaceError: | ||
| pass | ||
|
|
||
| try: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above but with table |
||
| catalog_table = catalog.load_table(identifier) | ||
| matches += ("table",) | ||
| except NoSuchTableError: | ||
| pass | ||
|
|
||
| if len(matches) > 1: | ||
| raise ValueError( | ||
| f"Identifier {identifier} matches multiple entity types: {', '.join(matches)}. Use --entity to disambiguate." | ||
| ) | ||
| if not matches: | ||
| raise NoSuchTableError(f"Table or namespace does not exist: {identifier}") | ||
|
|
||
| if matches[0] == "namespace": | ||
| assert namespace_properties is not None | ||
| output.describe_properties(namespace_properties) | ||
| else: | ||
| assert catalog_table is not None | ||
| output.describe_table(catalog_table) | ||
|
|
||
|
|
||
| @run.command() | ||
| @click.argument("identifier") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will clean up the if statement at line 167, but it's a bit harder to parse. Your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i actually did this intentionally. these ifs are for the individual checks namespace / table / and view (in #3926)
starting L167 is the "any" fallback behavior. i can add a inline comment for that to be more explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that would be great. I saw that's what you were trying to do, but that little bit of duplication was definitely bugging me 😂