From 085f87382bb1ebc04d82bef6c5278f7b31e44889 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 2 Dec 2026 21:39:25 -0400 Subject: [PATCH] fix(llm): build /llm schema without GROUP_CONCAT truncation GROUP_CONCAT output is silently cut at group_concat_max_len (1024 bytes by default on MySQL), so wide tables lost columns in the schema sent to the LLM. Fetch columns ordered by table and ordinal position and group them in Python instead. Co-Authored-By: Claude Sonnet 5.5 --- changelog.md | 1 + mycli/packages/special_commands/llm.py | 12 ++++++--- mycli_test/pytests/test_special_llm.py | 37 ++++++++++++++++++++++---- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/changelog.md b/changelog.md index 5c010a46..1b9be83f 100644 --- a/changelog.md +++ b/changelog.md @@ -9,6 +9,7 @@ Features Bug Fixes -------- * Display DATE and TIME scalars returned by `.|` transforms. +* Stop truncating `/llm` table schemas at MySQL's `group_concat_max_len`. Documentation diff --git a/mycli/packages/special_commands/llm.py b/mycli/packages/special_commands/llm.py index 617e769d..44659d03 100644 --- a/mycli/packages/special_commands/llm.py +++ b/mycli/packages/special_commands/llm.py @@ -332,15 +332,19 @@ def get_schema(cur: Cursor, dbname: str, prompt_section_truncate: int) -> str: if dbname in SCHEMA_DATA_CACHE: return SCHEMA_DATA_CACHE[dbname] click.echo("Preparing schema information to feed the LLM") + # Group columns in Python rather than with GROUP_CONCAT, which MySQL + # silently truncates at group_concat_max_len (1024 bytes by default). schema_query = f""" - SELECT CONCAT(table_name, '(', GROUP_CONCAT(column_name, ' ', COLUMN_TYPE SEPARATOR ', '),')') AS `schema` + SELECT table_name, column_name, COLUMN_TYPE FROM information_schema.columns WHERE table_schema = '{dbname}' - GROUP BY table_name - ORDER BY table_name + ORDER BY table_name, ordinal_position """ cur.execute(schema_query) - db_schema = [row for (row,) in cur.fetchall()] + table_columns: dict[str, list[str]] = {} + for table_name, column_name, column_type in cur.fetchall(): + table_columns.setdefault(table_name, []).append(f"{column_name} {column_type}") + db_schema = [f"{table_name}({', '.join(columns)})" for table_name, columns in table_columns.items()] summary = '\n'.join(truncate_table_lines(db_schema, prompt_section_truncate)) SCHEMA_DATA_CACHE[dbname] = summary return summary diff --git a/mycli_test/pytests/test_special_llm.py b/mycli_test/pytests/test_special_llm.py index 8ebb8e2c..bb8b9984 100644 --- a/mycli_test/pytests/test_special_llm.py +++ b/mycli_test/pytests/test_special_llm.py @@ -433,13 +433,13 @@ class DummyCursor: def __init__(self) -> None: self.executed: list[str] = [] self.description: list[tuple[str, None]] = [] - self._rows: list[tuple[str]] = [] + self._rows: list[tuple[str, ...]] = [] self._row: tuple[int, str] | None = None def execute(self, query: str) -> None: self.executed.append(query) if "information_schema.columns" in query: - self._rows = [("orders(id int)",), ("users(name text)",)] + self._rows = [("orders", "id", "int"), ("users", "name", "text")] return if query == "SHOW TABLES": self._rows = [("orders",), ("broken",), ("empty",)] @@ -456,7 +456,7 @@ def execute(self, query: str) -> None: return raise AssertionError(f"unexpected query: {query}") - def fetchall(self) -> list[tuple[str]]: + def fetchall(self) -> list[tuple[str, ...]]: return self._rows def fetchone(self) -> tuple[int, str] | None: @@ -488,7 +488,7 @@ def __init__(self): def execute(self, query): self.executed.append(query) if "information_schema.columns" in query: - self._last = [("table1(col1 int,col2 text)",), ("table2(colA varchar(20))",)] + self._last = [("table1", "col1", "int"), ("table1", "col2", "text"), ("table2", "colA", "varchar(20)")] elif query.strip().upper().startswith("SHOW TABLES"): self._last = [("table1",), ("table2",)] elif query.strip().upper().startswith("SELECT * FROM"): @@ -517,7 +517,7 @@ def fetchone(self): llm_module.LLM_TEMPLATE_NAME, "--param", "db_schema", - "table1(col1 int,col2 text)\ntable2(colA varchar(20))", + "table1(col1 int, col2 text)\ntable2(colA varchar(20))", "--param", "sample_data", {"table1": [("col1", 1), ("col2", "abc")], "table2": [("col1", 1), ("col2", "abc")]}, @@ -559,3 +559,30 @@ def test_handle_llm_registered_aliases_without_args(prefix, executor, monkeypatc with pytest.raises(llm_module.FinishIteration) as exc_info: handle_llm(prefix, executor, 'mysql', 0, 0) assert exc_info.value.results == [SQLResult(preamble=USAGE)] + + +def test_get_schema_groups_columns_without_group_concat_truncation(monkeypatch) -> None: + llm_module.SCHEMA_DATA_CACHE.clear() + monkeypatch.setattr(llm_module.click, "echo", lambda message: None) + columns = [f"column_{i:03d}" for i in range(100)] + + class DummyCursor: + def __init__(self) -> None: + self.executed: list[str] = [] + + def execute(self, query: str) -> None: + self.executed.append(query) + + def fetchall(self) -> list[tuple[str, str, str]]: + return [("wide", column, "varchar(255)") for column in columns] + [("narrow", "id", "int")] + + cursor = DummyCursor() + + schema = get_schema(cast(Any, cursor), "wide_db", 0) + + assert "GROUP_CONCAT" not in cursor.executed[0] + assert "ordinal_position" in cursor.executed[0] + assert schema.splitlines() == [ + "wide(" + ", ".join(f"{column} varchar(255)" for column in columns) + ")", + "narrow(id int)", + ]