Skip to content

Commit ff28d71

Browse files
kevinjqliuCopilot
andcommitted
Improve CLI describe behavior
Short-circuit entity discovery, preserve literal view SQL output, and add focused coverage for inferred and explicit entity selection. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent aca11a0 commit ff28d71

3 files changed

Lines changed: 68 additions & 36 deletions

File tree

‎pyiceberg/cli/console.py‎

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -151,38 +151,40 @@ def describe(ctx: Context, entity: Literal["any", "namespace", "table", "view"],
151151
catalog, output = _catalog_and_output(ctx)
152152
identifier_tuple = Catalog.identifier_to_tuple(identifier)
153153

154-
is_namespace = False
155-
if entity in {"namespace", "any"} and len(identifier_tuple) > 0:
156-
try:
157-
namespace_properties = catalog.load_namespace_properties(identifier_tuple)
158-
output.describe_properties(namespace_properties)
159-
is_namespace = True
160-
except NoSuchNamespaceError as exc:
161-
if entity != "any" or len(identifier_tuple) == 1:
162-
raise exc
163-
164-
is_table = False
165-
if entity in {"table", "any"} and len(identifier_tuple) > 1:
166-
try:
167-
catalog_table = catalog.load_table(identifier)
168-
output.describe_table(catalog_table)
169-
is_table = True
170-
except NoSuchTableError as exc:
171-
if entity != "any":
172-
raise exc
173-
174-
is_view = False
175-
if entity in {"view", "any"} and len(identifier_tuple) > 1:
176-
try:
177-
catalog_view = catalog.load_view(identifier)
178-
output.describe_view(catalog_view)
179-
is_view = True
180-
except (NoSuchViewError, NotImplementedError) as exc:
181-
if entity != "any":
182-
raise exc
183-
184-
if is_namespace is False and is_table is False and is_view is False:
185-
raise NoSuchTableError(f"Table, view, or namespace does not exist: {identifier}")
154+
if entity == "namespace":
155+
output.describe_properties(catalog.load_namespace_properties(identifier_tuple))
156+
return
157+
if entity == "table":
158+
output.describe_table(catalog.load_table(identifier))
159+
return
160+
if entity == "view":
161+
output.describe_view(catalog.load_view(identifier))
162+
return
163+
164+
# For "any", describe the first match in namespace, table, then view order.
165+
if len(identifier_tuple) == 1:
166+
output.describe_properties(catalog.load_namespace_properties(identifier_tuple))
167+
return
168+
169+
try:
170+
output.describe_properties(catalog.load_namespace_properties(identifier_tuple))
171+
return
172+
except NoSuchNamespaceError:
173+
pass
174+
175+
try:
176+
output.describe_table(catalog.load_table(identifier))
177+
return
178+
except NoSuchTableError:
179+
pass
180+
181+
try:
182+
output.describe_view(catalog.load_view(identifier))
183+
return
184+
except (NoSuchViewError, NotImplementedError):
185+
pass
186+
187+
raise NoSuchTableError(f"Table, view, or namespace does not exist: {identifier}")
186188

187189

188190
@run.command()

‎pyiceberg/cli/output.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from rich.console import Console
2525
from rich.table import Table as RichTable
26+
from rich.text import Text
2627
from rich.tree import Tree
2728

2829
from pyiceberg.partitioning import PartitionSpec
@@ -140,7 +141,7 @@ def describe_view(self, view: View) -> None:
140141
current_version = view.current_version()
141142
representations_tree = Tree("SQL representations")
142143
for repr in current_version.representations:
143-
representations_tree.add(f"[{repr.root.dialect}] {repr.root.sql}")
144+
representations_tree.add(Text(f"{repr.root.dialect}: {repr.root.sql}"))
144145

145146
output_table = self._table
146147
output_table.add_row("View format version", str(metadata.format_version))

‎tests/cli/test_console.py‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,12 @@ def test_list_namespace(catalog: InMemoryCatalog) -> None:
163163
assert result.output == "default.my_table\n"
164164

165165

166-
def test_describe_namespace(catalog: InMemoryCatalog, namespace_properties: Properties) -> None:
166+
@pytest.mark.parametrize("entity_option", [[], ["--entity=namespace"]], ids=["any", "namespace"])
167+
def test_describe_namespace(catalog: InMemoryCatalog, namespace_properties: Properties, entity_option: list[str]) -> None:
167168
catalog.create_namespace(TEST_TABLE_NAMESPACE, namespace_properties)
168169

169170
runner = CliRunner()
170-
result = runner.invoke(run, ["describe", "default"])
171+
result = runner.invoke(run, ["describe", *entity_option, "default"])
171172

172173
assert result.exit_code == 0
173174
assert result.output == "location s3://warehouse/database/location\n"
@@ -1208,7 +1209,7 @@ def test_describe_view(catalog_with_view: tuple[InMemoryCatalog, View]) -> None:
12081209
result = runner.invoke(run, ["describe", "--entity=view", "default.my_view"])
12091210
assert result.exit_code == 0
12101211
assert "b30125c8-7284-442c-9aea-15fee620737c" in result.output
1211-
assert "SELECT * FROM my_table" in result.output
1212+
assert "spark: SELECT * FROM my_table" in result.output
12121213

12131214

12141215
def test_describe_view_does_not_exist(catalog: InMemoryCatalog) -> None:
@@ -1230,6 +1231,34 @@ def test_describe_any_falls_through_to_view(catalog_with_view: tuple[InMemoryCat
12301231
assert "b30125c8-7284-442c-9aea-15fee620737c" in result.output
12311232

12321233

1234+
@pytest.mark.parametrize("entity_option", [[], ["--entity=table"]], ids=["any", "table"])
1235+
def test_describe_table_entity_selection(catalog: InMemoryCatalog, entity_option: list[str]) -> None:
1236+
catalog.create_namespace(TEST_TABLE_NAMESPACE)
1237+
table = catalog.create_table(identifier=TEST_TABLE_IDENTIFIER, schema=TEST_TABLE_SCHEMA)
1238+
catalog.load_view = MagicMock(side_effect=AssertionError("View lookup should not be called")) # type: ignore
1239+
1240+
runner = CliRunner()
1241+
result = runner.invoke(run, ["describe", *entity_option, "default.my_table"])
1242+
1243+
assert result.exit_code == 0
1244+
assert str(table.metadata.table_uuid) in result.output
1245+
catalog.load_view.assert_not_called()
1246+
1247+
1248+
def test_describe_any_prefers_namespace(catalog: InMemoryCatalog) -> None:
1249+
catalog.create_namespace(("default", "shared"), {"type": "namespace"})
1250+
catalog.load_table = MagicMock(side_effect=AssertionError("Table lookup should not be called")) # type: ignore
1251+
catalog.load_view = MagicMock(side_effect=AssertionError("View lookup should not be called")) # type: ignore
1252+
1253+
runner = CliRunner()
1254+
result = runner.invoke(run, ["describe", "default.shared"])
1255+
1256+
assert result.exit_code == 0
1257+
assert result.output == "type namespace\n"
1258+
catalog.load_table.assert_not_called()
1259+
catalog.load_view.assert_not_called()
1260+
1261+
12331262
def test_drop_view(catalog_with_view: tuple[InMemoryCatalog, View]) -> None:
12341263
catalog, _ = catalog_with_view
12351264

0 commit comments

Comments
 (0)