Skip to content

fix(agents): return tables from all databases in get_usable_table_names#12411

Open
chon3806 wants to merge 2 commits into
mindsdb:mainfrom
chon3806:fix/agent-multi-db-table-listing
Open

fix(agents): return tables from all databases in get_usable_table_names#12411
chon3806 wants to merge 2 commits into
mindsdb:mainfrom
chon3806:fix/agent-multi-db-table-listing

Conversation

@chon3806
Copy link
Copy Markdown

Description

In MindsDBQuery.get_usable_table_names, the result_tables list is reset to [] on every iteration of the for db_name in self.tables.databases: loop. As a result, when an agent is configured with tables from more than one data source (e.g. data = {\"tables\": [\"db1.*\", \"db2.*\"]}), only the tables from the last iterated database are ever returned. Tables from earlier databases are silently dropped.

This affects:

  • AgentsController.check_agent_data (called via get_usable_table_names(lazy=False) during agent creation), which can spuriously fail validation.
  • The agent runtime's view of which tables it has access to whenever a wildcard is used across multiple data sources.

The fix is a one-line change: move the result_tables = [] initialization out of the per-database loop so matching tables are accumulated across all configured databases.

A unit test (tests/unit/interfaces/agents/test_sql_toolkit.py) is added covering:

  • Single-database wildcard expansion
  • Multi-database wildcard expansion (regression test for this bug)
  • Lazy mode returning items as-is
  • Empty tables returning an empty list

Type of change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

Verification Process

  • Test Location: tests/unit/interfaces/agents/test_sql_toolkit.py
  • Verification Steps:
    1. pytest tests/unit/interfaces/agents/test_sql_toolkit.py -v
    2. Manual: create an agent with data = {\"tables\": [\"db1.*\", \"db2.*\"]} and confirm tables from both databases are listed.

Checklist

  • My code follows the style guidelines (PEP 8) of MindsDB.
  • I have appropriately commented on my code, especially in complex areas.
  • Necessary documentation updates are either made or tracked in issues.
  • Relevant unit tests are added.

The result_tables list was being reset on every iteration of the
databases loop, so when an agent was configured with tables from
multiple data sources (e.g. data.tables = ['db1.*', 'db2.*']), only
the tables from the last iterated database were returned.

Move the reset out of the loop so all matching tables across all
databases are accumulated.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 25, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@entelligence-ai-pr-reviews
Copy link
Copy Markdown
Contributor

EntelligenceAI PR Summary

Removes dead code in the SQL toolkit and adds unit test coverage for MindsDBQuery.get_usable_table_names.

  • Deleted unused result_tables = [] variable in mindsdb/interfaces/agents/utils/sql_toolkit.py
  • Added tests/unit/interfaces/agents/test_sql_toolkit.py with four test cases: single-database wildcard, multi-database wildcard (regression), lazy mode, and empty input
  • Test infrastructure uses _make_query and _make_handler factory functions with unittest.mock.Mock to isolate tests from SessionController and live DB dependencies

Confidence Score: 5/5 - Safe to Merge

Safe to merge — this PR removes a dead result_tables = [] variable in sql_toolkit.py that was never read or returned, and adds four focused unit tests for MindsDBQuery.get_usable_table_names covering the single-database wildcard, multi-database wildcard regression, lazy mode, and empty input cases. No logic changes are introduced to production code paths, and the new test file is cleanly isolated using unittest.mock.Mock factory helpers. No review comments were raised and no pre-existing unresolved issues touch the modified code.

Key Findings:

  • The only production code change is deletion of the unused result_tables = [] assignment in sql_toolkit.py, which has zero runtime impact since the variable was never referenced after initialization.
  • The new tests/unit/interfaces/agents/test_sql_toolkit.py correctly exercises the multi-database wildcard path, which appears to be the regression scenario motivating this PR, providing a safety net against future regressions.
  • Test isolation via _make_query and _make_handler Mock factories is appropriate for unit-level coverage and avoids any dependency on live database connections.
  • No automated review comments were generated and heuristic analysis found zero medium, significant, or critical issues across both changed files.
Files requiring special attention
  • mindsdb/interfaces/agents/utils/sql_toolkit.py
  • tests/unit/interfaces/agents/test_sql_toolkit.py

Adds an additional regression test proving that with
tables=['db1.users', 'db2.*'] the match() filter still correctly
restricts db1 to the listed table while expanding db2 fully.
@chon3806
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Apr 25, 2026
@torrmal
Copy link
Copy Markdown
Contributor

torrmal commented May 2, 2026

thank you!! can you please create a pull request in github.com/mindsdb/engine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants