Coverage for src/chuck_data/commands/list_tables.py: 0%

44 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-06-05 22:56 -0700

1""" 

2Command for listing tables in a Unity Catalog schema. 

3""" 

4 

5from typing import Optional, Any 

6from ..clients.databricks import DatabricksAPIClient 

7from ..command_registry import CommandDefinition 

8from ..commands.base import CommandResult 

9from ..config import get_active_catalog, get_active_schema 

10import logging 

11 

12 

13def handle_command( 

14 client: Optional[DatabricksAPIClient], **kwargs: Any 

15) -> CommandResult: 

16 """ 

17 List tables in a Unity Catalog schema. 

18 

19 Args: 

20 client: DatabricksAPIClient instance for API calls 

21 **kwargs: Command parameters 

22 - catalog_name: Name of the catalog containing the schema 

23 - schema_name: Name of the schema to list tables from 

24 - include_delta_metadata: Whether delta metadata should be included (optional) 

25 - omit_columns: Whether to omit columns from the response (optional) 

26 - include_browse: Whether to include tables with selective metadata access (optional) 

27 

28 Returns: 

29 CommandResult with list of tables 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 ) 

36 

37 # Extract parameters 

38 catalog_name = kwargs.get("catalog_name") 

39 schema_name = kwargs.get("schema_name") 

40 include_delta_metadata = kwargs.get("include_delta_metadata", False) 

41 omit_columns = kwargs.get("omit_columns", False) 

42 include_browse = kwargs.get("include_browse", False) 

43 

44 # If catalog_name not provided, try to use active catalog 

45 if not catalog_name: 

46 catalog_name = get_active_catalog() 

47 if not catalog_name: 

48 return CommandResult( 

49 False, 

50 message="No catalog specified and no active catalog selected. Please provide a catalog_name or select a catalog first using /select-catalog.", 

51 ) 

52 

53 # If schema_name not provided, try to use active schema 

54 if not schema_name: 

55 schema_name = get_active_schema() 

56 if not schema_name: 

57 return CommandResult( 

58 False, 

59 message="No schema specified and no active schema selected. Please provide a schema_name or select a schema first using /select-schema.", 

60 ) 

61 

62 try: 

63 # List tables in the schema 

64 result = client.list_tables( 

65 catalog_name=catalog_name, 

66 schema_name=schema_name, 

67 include_delta_metadata=include_delta_metadata, 

68 omit_columns=omit_columns, 

69 include_browse=include_browse, 

70 ) 

71 

72 tables = result.get("tables", []) 

73 

74 if not tables: 

75 return CommandResult( 

76 True, 

77 message=f"No tables found in schema '{catalog_name}.{schema_name}'.", 

78 ) 

79 

80 # Format table information for display 

81 formatted_tables = [] 

82 for table in tables: 

83 # Extract key information 

84 table_info = { 

85 "name": table.get("name"), 

86 "full_name": table.get("full_name"), 

87 "table_type": table.get("table_type", ""), 

88 "data_source_format": table.get("data_source_format", ""), 

89 "comment": table.get("comment", ""), 

90 "created_at": table.get("created_at"), 

91 "created_by": table.get("created_by", ""), 

92 "owner": table.get("owner", ""), 

93 "row_count": table.get("properties", {}).get("row_count", "Unknown"), 

94 "size_bytes": table.get("properties", {}).get("size_bytes", "Unknown"), 

95 } 

96 

97 # Include columns if available and not omitted 

98 if not omit_columns: 

99 columns = table.get("columns", []) 

100 table_info["column_count"] = len(columns) 

101 if columns: 

102 column_list = [] 

103 for col in columns: 

104 column_list.append( 

105 { 

106 "name": col.get("name"), 

107 "type": col.get( 

108 "type_text", col.get("type", {}).get("name", "") 

109 ), 

110 "nullable": col.get("nullable", True), 

111 } 

112 ) 

113 table_info["columns"] = column_list 

114 

115 formatted_tables.append(table_info) 

116 

117 return CommandResult( 

118 True, 

119 data={ 

120 "tables": formatted_tables, 

121 "total_count": len(formatted_tables), 

122 "catalog_name": catalog_name, 

123 "schema_name": schema_name, 

124 }, 

125 message=f"Found {len(formatted_tables)} table(s) in '{catalog_name}.{schema_name}'.", 

126 ) 

127 except Exception as e: 

128 logging.error(f"Error listing tables: {str(e)}") 

129 return CommandResult(False, message=f"Failed to list tables: {str(e)}", error=e) 

130 

131 

132DEFINITION = CommandDefinition( 

133 name="list-tables", 

134 description="List tables in a Unity Catalog schema.", 

135 handler=handle_command, 

136 parameters={ 

137 "catalog_name": { 

138 "type": "string", 

139 "description": "Name of the catalog containing the schema.", 

140 }, 

141 "schema_name": { 

142 "type": "string", 

143 "description": "Name of the schema to list tables from.", 

144 }, 

145 "include_delta_metadata": { 

146 "type": "boolean", 

147 "description": "Whether delta metadata should be included.", 

148 "default": False, 

149 }, 

150 "omit_columns": { 

151 "type": "boolean", 

152 "description": "Whether to omit columns from the response.", 

153 "default": False, 

154 }, 

155 "include_browse": { 

156 "type": "boolean", 

157 "description": "Whether to include tables with selective metadata access.", 

158 "default": False, 

159 }, 

160 }, 

161 required_params=[], # Not required anymore as we'll try to get them from active config 

162 tui_aliases=["/tables"], 

163 needs_api_client=True, 

164 visible_to_user=True, 

165 visible_to_agent=True, 

166 agent_display="full", # Show full table list to agents 

167 usage_hint="Usage: /list-tables [--catalog_name <catalog>] [--schema_name <schema>] [--omit_columns true|false]\n(Uses active catalog/schema if not specified)", 

168)