Skip to content

feat(tools): add chDB tools for in-process ClickHouse SQL#6533

Open
ShawnChen-Sirius wants to merge 3 commits into
crewAIInc:mainfrom
ShawnChen-Sirius:feat/chdb-tools
Open

feat(tools): add chDB tools for in-process ClickHouse SQL#6533
ShawnChen-Sirius wants to merge 3 commits into
crewAIInc:mainfrom
ShawnChen-Sirius:feat/chdb-tools

Conversation

@ShawnChen-Sirius

Copy link
Copy Markdown

AI disclosure: this PR was authored with AI assistance (Claude Code), per the contributing guide it should carry the llm-generated label — I don't have permission to add labels on this repo, so please apply it. The design, testing, and review of every line were done by a ClickHouse engineer (chDB team).

What

Seven tools that give agents analytical SQL over local and remote data with chDB, the in-process ClickHouse engine, wrapping chdb.agents.ChDBTool (the agent-tool surface the chdb package ships and conformance-tests):

  • ChDBRunSelectQueryTool (run_select_query) — read-only ClickHouse SQL with {name:Type} parameter binding
  • ChDBListDatabasesTool, ChDBListTablesTool, ChDBDescribeTableTool, ChDBGetSampleDataTool, ChDBListFunctionsTool — schema discovery, including describing table-function expressions like s3('…','Parquet')
  • ChDBAttachFileTool (attach_file) — register a local file as a named table (writable sessions only)
  • chdb_tools() — builds the suite over one shared engine, so a table one tool attaches is visible to the others

Why (vs existing SQL tools)

  • In-process: no server, no connection string, no credentials, no env vars — the engine runs inside the Python process and reads Parquet/CSV/JSON files, S3/URLs, and remote databases through ClickHouse table functions.
  • Engine-level safety, not prompt-level: sessions default to ClickHouse readonly=2 (INSERT/CREATE/ALTER/DROP rejected by the engine while SELECT and file()/s3()/url() keep working — same direction as the NL2SQLTool read-only hardening); max_rows/max_bytes cap every payload with an explicit truncated flag; optional file_allowlist confines file-like sources to listed prefixes and refuses DSN-based sources.
  • Errors feed the model: every call returns a JSON envelope ({"ok": true, "result": …} / {"ok": false, "error": {code, type, message}}), so a wrong query becomes feedback the agent corrects instead of an exception that kills the crew run.

Notes for reviewers

  • chdb is an optional dependency (crewai-tools[chdb]). import crewai_tools works without it; tools raise a descriptive ImportError at first use.
  • chdb 4.2.1 (the extra's minimum) was released 2026-07-13, inside the rolling 3-day exclude-newer window, so the second commit adds it to the existing exclude-newer-package overrides. From 2026-07-16 the entry is no longer needed and can be dropped whenever convenient. uv.lock is intentionally untouched (CI re-resolves; happy to include the regenerated lock if you prefer).
  • tool.specs.json is intentionally untouched — the Generate Tool Specifications workflow doesn't run for fork PRs; happy to include the regenerated file instead.
  • Async is native: _arun delegates to the engine's acall() rather than blocking the loop.
  • ChDBBaseTool is importable from the subpackage but not exported at the top level, mirroring DaytonaBaseTool.
  • Tool names default to chDB's agent-tool names (run_select_query, …); they can be renamed at construction if a crew combines multiple SQL suites.

Testing

  • 19 tests in tests/tools/chdb_tool_test.py: unit tests against a mocked engine (no chdb needed) plus integration tests (pytest.importorskip("chdb")) covering parameter binding, the readonly=2 write rejection envelope, attach-then-query engine sharing, read-only attachments, and the introspection tools.
  • ruff check / ruff format and strict mypy (repo config, pydantic plugin) clean.
  • Verified import crewai_tools succeeds and tools construct with chdb absent (import blocked via a meta-path hook), with the lazy ImportError pointing at crewai-tools[chdb].

ShawnChen-Sirius and others added 2 commits July 14, 2026 11:51
Seven tools over chdb.agents.ChDBTool, the agent-tool surface the chdb
package ships: run_select_query (read-only ClickHouse SQL with parameter
binding), list_databases, list_tables, describe_table, get_sample_data,
list_functions, and attach_file, plus a chdb_tools() factory that builds
the suite over one shared engine so attached tables are visible across
tools.

The engine runs inside the Python process, so unlike server-backed SQL
tools there is no connection string, credential, or env var. Safety is
engine-level rather than prompt-level: sessions default to the ClickHouse
readonly=2 setting, results are capped by max_rows/max_bytes with a
truncated flag, and every call returns a JSON envelope (ok/result or
ok/error) so the agent can read engine errors and self-correct instead
of crashing the run.

chdb is an optional dependency (crewai-tools[chdb]): importing
crewai_tools works without it, and tools raise a descriptive ImportError
at first use. Unit tests run against a mocked engine; integration tests
skip when chdb is not installed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
chdb 4.2.1 is the minimum for the new crewai-tools chdb extra and was
released 2026-07-13, inside the rolling 3-day exclude-newer window; add
it to the per-package overrides so resolution succeeds, following the
existing entries.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 32004c82-413e-4334-a606-0eaa130c4f97

📥 Commits

Reviewing files that changed from the base of the PR and between 66e5345 and 0639b36.

📒 Files selected for processing (1)
  • lib/crewai-tools/tests/tools/chdb_tool_test.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/crewai-tools/tests/tools/chdb_tool_test.py

📝 Walkthrough

Walkthrough

Adds an optional chDB dependency and a CrewAI tool suite for queries, introspection, file attachments, shared engine lifecycle, JSON responses, and read-only enforcement. Public exports, documentation, package configuration, unit tests, and integration tests are included.

Changes

ChDB Tool Suite

Layer / File(s) Summary
Package dependency and public exports
lib/crewai-tools/pyproject.toml, lib/crewai-tools/src/crewai_tools/__init__.py, lib/crewai-tools/src/crewai_tools/tools/__init__.py, lib/crewai-tools/src/crewai_tools/tools/chdb_tool/__init__.py, pyproject.toml
Adds the chdb optional dependency, exposes ChDB tools through package APIs, and configures the uv package cutoff.
Shared engine and tool implementations
lib/crewai-tools/src/crewai_tools/tools/chdb_tool/chdb_tool.py
Implements lazy dependency loading, shared engine lifecycle, sync/async dispatch, query and introspection tools, file attachment, and read-only suite composition.
Behavior validation and usage documentation
lib/crewai-tools/src/crewai_tools/tools/chdb_tool/README.md, lib/crewai-tools/tests/tools/chdb_tool_test.py
Documents installation, safety behavior, lifecycle, and usage while testing dispatch, JSON envelopes, async calls, attachments, read-only enforcement, and integrations.

Sequence Diagram(s)

sequenceDiagram
  participant Agent
  participant ChDBRunSelectQueryTool
  participant ChDBTool
  participant ChDBEngine
  Agent->>ChDBRunSelectQueryTool: submit SQL and params
  ChDBRunSelectQueryTool->>ChDBTool: dispatch run_select_query
  ChDBTool->>ChDBEngine: call or acall
  ChDBEngine-->>ChDBTool: JSON success or error envelope
  ChDBTool-->>Agent: return serialized response
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the addition of chDB tools for in-process ClickHouse SQL.
Description check ✅ Passed The description matches the changeset and accurately summarizes the new chDB tools, shared engine, safety, and tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/crewai-tools/src/crewai_tools/tools/chdb_tool/chdb_tool.py`:
- Around line 11-20: Update ChDBBaseTool.close in
lib/crewai-tools/src/crewai_tools/tools/chdb_tool/chdb_tool.py:11-20 to close
the underlying session or guard the cleanup so it does not unconditionally call
the unsupported ChDBTool.close method. The references in
lib/crewai-tools/pyproject.toml:151-154 and pyproject.toml:175-176 require no
direct changes.

In `@lib/crewai-tools/tests/tools/chdb_tool_test.py`:
- Around line 169-171: Scope the chdb dependency skip to only the real-engine
integration tests, not the mocked unit tests in chdb_tool_test.py. Move those
integration tests and the pytest.importorskip("chdb") call into a separate test
module, or apply per-test/class-level skipping to the integration-test symbols
while keeping the mocked tests runnable without chdb.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 484a1f90-3612-4e88-b17f-964e8d233d72

📥 Commits

Reviewing files that changed from the base of the PR and between 9d72e26 and 66e5345.

📒 Files selected for processing (8)
  • lib/crewai-tools/pyproject.toml
  • lib/crewai-tools/src/crewai_tools/__init__.py
  • lib/crewai-tools/src/crewai_tools/tools/__init__.py
  • lib/crewai-tools/src/crewai_tools/tools/chdb_tool/README.md
  • lib/crewai-tools/src/crewai_tools/tools/chdb_tool/__init__.py
  • lib/crewai-tools/src/crewai_tools/tools/chdb_tool/chdb_tool.py
  • lib/crewai-tools/tests/tools/chdb_tool_test.py
  • pyproject.toml

Comment thread lib/crewai-tools/src/crewai_tools/tools/chdb_tool/chdb_tool.py
Comment thread lib/crewai-tools/tests/tools/chdb_tool_test.py Outdated
A module-level pytest.importorskip skips the entire file, so the mocked
unit tests were skipped along with the integration tests on machines
without chdb. Guard the integration tests individually with a skipif
marker instead: without chdb the 13 unit tests still run (6 integration
tests skip); with chdb all 19 run.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant