crewAIInc / crewAIInc/crewAI

[BUG] DatabricksQueryTool skips row_limit when an identifier merely contains "limit"

Open
#7,218 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
58.8k
Forks
8.5k
Avg merge
1d 15h
Merged PRs (30d)
109

Description

Description

DatabricksQueryTool decides whether to append the row_limit clause with a substring check, so any table or column name that merely contains the letters "limit" silently disables the row cap:

# lib/crewai-tools/src/crewai_tools/tools/databricks_query_tool/databricks_query_tool.py (line ~67, main @ 3d72c707d5)
if self.row_limit and "limit" not in self.query.lower():
    self.query = f"{self.query.rstrip(';')} LIMIT {self.row_limit};"

SELECT * FROM limited_orders contains limit as a substring, so no LIMIT clause is ever appended and the tool fetches unbounded rows — the opposite of the guard's intent.

Steps to Reproduce
from crewai_tools import DatabricksQueryTool

tool = DatabricksQueryTool(catalog=..., schema=...)  # any valid connection
tool.run(query="SELECT * FROM limited_orders")       # default row_limit=1000

The generated SQL has no LIMIT 1000 appended, because "limit" in "select * from limited_orders" is True.

Expected behavior

A table/column name that happens to contain "limit" should not suppress the row cap; SELECT * FROM limited_orders should get LIMIT 1000 appended. Only an actual LIMIT/FETCH clause in the query should suppress it.

Screenshots/Code snippets
SELECT * FROM limited_orders;        -- "limit" is a substring of the identifier -> no cap appended (bug)
SELECT * FROM orders LIMIT 5;        -- real clause -> correctly left unchanged
SELECT * FROM orders;                -- no clause -> correctly gets LIMIT 1000 appended

A clause-aware check (e.g. regex for LIMIT n, LIMIT ALL, FETCH FIRST/NEXT n ROWS) instead of a substring check fixes the class.

Operating System

Ubuntu 24.04

Python Version

3.12

crewAI Version

1.15.18 (latest release)

crewAI Tools Version

main @ 3d72c707d5 (bug verified present there)

Virtual Environment

Venv

Evidence
TABLE_HAS_LIMIT: SELECT * FROM limited_orders

(no LIMIT clause appended, default row_limit=1000 in effect)

Regression tests covering LIMIT/FETCH detection and identifier substring cases: lib/crewai-tools/tests/tools/test_databricks_query_tool.py (9 passed against a candidate fix).

Possible Solution

Replace the substring check with clause detection before appending row_limit:

import re
_HAS_ROW_LIMIT = re.compile(r"\bLIMIT\s+\d+\b|\bLIMIT\s+ALL\b|\bFETCH\s+(FIRST|NEXT)\s+\d+\s+ROWS\b", re.IGNORECASE)

if self.row_limit and not _HAS_ROW_LIMIT.search(self.query):
    self.query = f"{self.query.rstrip(';')} LIMIT {self.row_limit};"
Additional context

I have a fix ready in PR #7121 (reopening it referencing this issue per the first-contribution gate). Analysis was assisted by an AI coding agent, disclosed there as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/crewai-tools/src/crewai_tools/tools/databricks_query_tool/databricks_query_tool.py around the row_limit check, then run lib/crewai-tools/tests/tools/test_databricks_query_tool.py. Done means identifiers containing "limit" still receive the row cap, while actual LIMIT or FETCH clauses do not receive an additional cap.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.