spark-test commented on code in PR #55648: URL: https://github.com/apache/spark/pull/55648#discussion_r4509112118
########## python/pyspark/sql/mcp/tools/catalog.py: ########## @@ -0,0 +1,218 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""Catalog browsing tools.""" + +from __future__ import annotations + +from dataclasses import asdict, is_dataclass +from typing import Any, Dict, List + +from pyspark.sql.mcp.session import SessionHolder +from pyspark.sql.mcp.tools.registry import ToolSpec + + +# --------------------------------------------------------------------------- +# Result shaping +# --------------------------------------------------------------------------- + + +def _to_dict(obj: Any) -> Dict[str, Any]: + """Best-effort conversion of catalog dataclasses (CatalogMetadata, Database, + Table, Column, Function) into plain dicts.""" + if is_dataclass(obj) and not isinstance(obj, type): + return asdict(obj) + if hasattr(obj, "_asdict"): # NamedTuple + return dict(obj._asdict()) + if isinstance(obj, dict): + return obj + # Fallback: scrape public attributes. + return { + k: getattr(obj, k) + for k in dir(obj) + if not k.startswith("_") and not callable(getattr(obj, k)) + } + + +def _paginate(items: List[Any], limit: int, offset: int) -> Dict[str, Any]: + total = len(items) + end = offset + limit if limit is not None else total + page = items[offset:end] + return { + "items": [_to_dict(x) for x in page], + "offset": offset, + "limit": limit, + "returned": len(page), + "total": total, + "truncated": end < total, + } + + +# --------------------------------------------------------------------------- +# list_catalogs +# --------------------------------------------------------------------------- + + +async def _handle_list_catalogs(args: Dict[str, Any], holder: SessionHolder) -> Dict[str, Any]: + spark = holder.get() + catalogs = list(spark.catalog.listCatalogs()) + limit = int(args.get("limit", holder.config.default_page_size)) + offset = int(args.get("offset", 0)) + return _paginate(catalogs, limit, offset) + + +def list_catalogs_spec() -> ToolSpec: + return ToolSpec( + name="list_catalogs", + description="List catalogs available to the current Spark session.", + input_schema={ + "type": "object", + "properties": { + "limit": {"type": "integer", "minimum": 1, "maximum": 1000, "default": 100}, + "offset": {"type": "integer", "minimum": 0, "default": 0}, + }, + "additionalProperties": False, + }, + handler=_handle_list_catalogs, + ) + + +# --------------------------------------------------------------------------- +# list_databases +# --------------------------------------------------------------------------- + + +async def _handle_list_databases(args: Dict[str, Any], holder: SessionHolder) -> Dict[str, Any]: + spark = holder.get() + catalog = args.get("catalog") + pattern = args.get("pattern") + + if catalog: + spark.catalog.setCurrentCatalog(catalog) + if pattern is not None: + databases = list(spark.catalog.listDatabases(pattern)) + else: + databases = list(spark.catalog.listDatabases()) + + limit = int(args.get("limit", holder.config.default_page_size)) + offset = int(args.get("offset", 0)) + return _paginate(databases, limit, offset) + + +def list_databases_spec() -> ToolSpec: + return ToolSpec( + name="list_databases", + description=( + "List databases (a.k.a. schemas) in the current or named catalog. " + "Optional name pattern uses Spark's LIKE semantics." Review Comment: **Nit (P3):** `listDatabases(pattern)` runs SHOW NAMESPACES, which filters with `StringUtils.filterPattern`: only `*` (any characters) and `|` (alternatives) are special, and the rest is a case-insensitive regex. An LLM that follows 'LIKE semantics' writes `sales%`, gets no matches, and may conclude that the databases do not exist. list_tables and list_functions take the same kind of pattern with no description; stating the real syntax in all three fixes it. **Verification:** - **Inspection:** The pattern descriptions match Spark's catalog pattern semantics, including `*`, `|` and case-insensitivity. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
