Coverage for src/chuck_data/commands/table.py: 0%
40 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
1"""
2Command for showing details of a specific Unity Catalog table.
3"""
5from typing import Optional, Any
6from ..clients.databricks import DatabricksAPIClient
7from ..commands.base import CommandResult
8from ..catalogs import get_table as get_table_details
9from ..config import get_active_catalog, get_active_schema
10from ..command_registry import CommandDefinition
11import logging
14def handle_command(
15 client: Optional[DatabricksAPIClient], **kwargs: Any
16) -> CommandResult:
17 """
18 Get details of a specific table from Unity Catalog.
20 Args:
21 client: DatabricksAPIClient instance for API calls
22 **kwargs: Command parameters
23 - name: Name of the table to get details for
24 - schema_name: Name of the schema containing the table (optional, uses active schema if not provided)
25 - catalog_name: Name of the catalog containing the schema (optional, uses active catalog if not provided)
26 - include_delta_metadata: Whether delta metadata should be included (optional)
28 Returns:
29 CommandResult with table details if successful
30 """
31 if not client:
32 return CommandResult(
33 False,
34 message="No Databricks client available. Please set up your workspace first.",
35 )
37 # Extract parameters
38 table_name = kwargs.get("name")
39 schema_name = kwargs.get("schema_name")
40 catalog_name = kwargs.get("catalog_name")
41 include_delta_metadata = kwargs.get("include_delta_metadata", False)
43 # If schema_name not provided, try to use active schema
44 if not schema_name:
45 schema_name = get_active_schema()
46 if not schema_name:
47 return CommandResult(
48 False,
49 message="No schema specified and no active schema selected. Please provide a schema_name or select a schema first.",
50 )
52 # If catalog_name not provided, try to use active catalog
53 if not catalog_name:
54 catalog_name = get_active_catalog()
55 if not catalog_name:
56 return CommandResult(
57 False,
58 message="No catalog specified and no active catalog selected. Please provide a catalog_name or select a catalog first.",
59 )
61 try:
62 # Construct full name for table lookup
63 full_name = f"{catalog_name}.{schema_name}.{table_name}"
65 # Get table details
66 table = get_table_details(
67 client, full_name, include_delta_metadata=include_delta_metadata
68 )
70 if not table:
71 return CommandResult(
72 False,
73 message=f"Table '{table_name}' not found in schema '{catalog_name}.{schema_name}'.",
74 )
76 # Extract column information in a user-friendly format
77 columns = table.get("columns", [])
78 formatted_columns = []
79 for col in columns:
80 formatted_column = {
81 "name": col.get("name"),
82 "type": col.get("type_text", col.get("type", {}).get("name", "")),
83 "nullable": col.get("nullable", True),
84 "comment": col.get("comment", ""),
85 "position": col.get("position"),
86 }
87 formatted_columns.append(formatted_column)
89 # Create a separate user-friendly table structure
90 table_info = {
91 "name": table.get("name"),
92 "full_name": table.get("full_name"),
93 "table_type": table.get("table_type", ""),
94 "data_source_format": table.get("data_source_format", ""),
95 "columns": formatted_columns,
96 "column_count": len(formatted_columns),
97 "comment": table.get("comment", ""),
98 "created_at": table.get("created_at"),
99 "created_by": table.get("created_by", ""),
100 "owner": table.get("owner", ""),
101 "properties": table.get("properties", {}),
102 }
104 # Include delta table details if available and requested
105 if include_delta_metadata and "delta" in table:
106 table_info["delta"] = table.get("delta", {})
108 return CommandResult(
109 True,
110 data={
111 "table": table_info,
112 "raw_table": table, # Include the raw table data as well for completeness
113 },
114 message=f"Table details for '{full_name}' ({len(formatted_columns)} columns).",
115 )
116 except Exception as e:
117 logging.error(f"Error getting table details: {str(e)}")
118 return CommandResult(
119 False, message=f"Failed to get table details: {str(e)}", error=e
120 )
123DEFINITION = CommandDefinition(
124 name="table",
125 description="Show details of a specific Unity Catalog table.",
126 handler=handle_command,
127 parameters={
128 "name": {
129 "type": "string",
130 "description": "Name of the table to get details for.",
131 },
132 "schema_name": {
133 "type": "string",
134 "description": "Name of the schema containing the table (uses active schema if not provided).",
135 },
136 "catalog_name": {
137 "type": "string",
138 "description": "Name of the catalog containing the schema (uses active catalog if not provided).",
139 },
140 "include_delta_metadata": {
141 "type": "boolean",
142 "description": "Whether Delta Lake metadata should be included.",
143 "default": False,
144 },
145 },
146 required_params=["name"],
147 tui_aliases=["/table", "/show-table"],
148 needs_api_client=True,
149 visible_to_user=True,
150 visible_to_agent=True,
151 agent_display="full", # Show full table details to agents
152 usage_hint="Usage: /table --name <table_name> [--schema_name <schema_name>] [--catalog_name <catalog_name>] [--include_delta_metadata true|false]",
153)