Lee-W commented on code in PR #76799:
URL: https://github.com/apache/airflow/pull/76799#discussion_r4597300035


##########
providers/common/ai/src/airflow/providers/common/ai/toolsets/sql.py:
##########
@@ -554,23 +623,106 @@ def add(schema: str | None, name: str, display: str) -> 
None:
         if self._include_default_schema:
             for name in inspector.get_table_names(schema=self._schema):
                 add(self._schema, name, name)
+        return tables
+
+    def _list_tables(self) -> str:
+        tables = self._find_allowed_tables()
+        if self._search_backend is not None and len(tables) > 
self._max_listed_tables:
+            return dumps_masked(
+                {
+                    "table_count": len(tables),
+                    "hint": (
+                        f"There are {len(tables)} tables, too many to list 
(the limit is "
+                        f"{self._max_listed_tables}). Use 
{self._build_tool_name('search_tables')} to find the ones you need."
+                    ),
+                }
+            )
+        if not self._include_table_comments:
+            return dumps_masked([display for _, _, display in tables])
+        return dumps_masked(
+            [
+                {"name": name, **({"comment": comment} if comment else {})}
+                for name, comment in self._get_table_entries(tables)
+            ]
+        )
+
+    def _get_table_entries(self, tables: list[tuple[str | None, str, str]]) -> 
list[TableEntry]:
+        """Pair each table's display name with its comment, capped, read once 
per schema."""
+        inspector = self._get_inspector()
+        comments = {
+            table_schema: self._get_table_comments(inspector, table_schema)
+            for table_schema in {table_schema for table_schema, _, _ in tables}
+        }
+        return [
+            (display, truncate_comment(comment) if (comment := 
comments[table_schema].get(name)) else None)
+            for table_schema, name, display in tables
+        ]
 
-        return dumps_masked(tables)
+    def _get_catalog(self) -> list[TableEntry]:
+        """Return the exposed tables with comments, introspected on first use 
and kept for this toolset."""
+        if self._catalog is None:
+            self._catalog = 
self._get_table_entries(self._find_allowed_tables())
+        return self._catalog
+
+    def _search_tables(self, query: str, limit: int | None) -> str:
+        if self._search_backend is None:
+            raise ValueError("search_tables requires a search_backend")
+        # The schema's maximum is only advice to the model; the cap has to 
hold here.
+        limit = min(max(_DEFAULT_SEARCH_LIMIT if limit is None else limit, 1), 
_MAX_SEARCH_LIMIT)
+        found = self._search_backend.search_tables(query, limit, 
get_catalog=self._get_catalog)
+        tables: list[dict[str, str]] = []
+        for display, comment in found:
+            schema, table = self._split_table_identifier(display)
+            if not self._is_ref_allowed("", schema, table):
+                continue
+            tables.append({"name": display, **({"comment": 
truncate_comment(comment)} if comment else {})})
+            if len(tables) == limit:
+                break
+        return dumps_masked({"tables": tables})
+
+    @staticmethod
+    def _get_table_comments(inspector: Inspector, schema: str | None) -> 
dict[str, str]:
+        """Return ``{table name: comment}`` for a schema in one call; empty 
when the dialect has none."""
+        # SQLAlchemy 1.4, which common-sql still supports, has no 
get_multi_table_comment.
+        # Reading comments one table at a time instead would cost a query per 
table, so
+        # on 1.4 the tables simply come back without comments.
+        if (get_comments := getattr(inspector, "get_multi_table_comment", 
None)) is None:
+            return {}
+        try:
+            reflected = get_comments(schema=schema)

Review Comment:
   Done. With `allowed_tables` set, only those names are passed as 
`filter_names`. And the docs are updated



-- 
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]

Reply via email to