Skip to content

fix(databricks): keep result columns in schema order - #9708

Merged
Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
Kiko0301:fix/databricks-column-order
Nov 24, 2026
Merged

Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
crewAIInc:mainfrom
Kiko0301:fix/databricks-column-order

Conversation

@Kiko0301

Copy link
Copy Markdown
Contributor

Fixes #9703

Summary

DatabricksQueryTool rebuilt each result row from a set of column names and formatted the table from the first row's key order. The rendered columns were therefore ordered by set iteration order: they changed from process to process and never followed the query's SELECT list. Generated Column_N names for extra values also landed in arbitrary positions.

This change tracks the columns in an ordered list seeded from the result schema, appending generated names after the schema columns.

Changes

  • lib/crewai-tools/src/crewai_tools/tools/databricks_query_tool/databricks_query_tool.py
    • Use all_columns = list(columns) and append generated Column_N names instead of adding them to a set.
  • lib/crewai-tools/tests/tools/test_databricks_query_tool.py
    • Add tests with a fake workspace client: one asserts the header follows schema order, one asserts an extra value becomes Column_5 at the end.

Verification

  • With the fix: pytest lib/crewai-tools/tests/tools/test_databricks_query_tool.py → 2 passed.
  • Against the base revision of databricks_query_tool.py (test file kept): 2 failed; the observed header was gamma | alpha | delta | beta | epsilon for schema order alpha, beta, gamma, delta, epsilon.
  • The tests use a fake workspace client and do not call Databricks.

AI assistance

I used an AI coding agent to investigate, implement, and test this change, and reviewed its results. Per CONTRIBUTING.md, please apply the llm-generated label; external contributors cannot add it with my current permissions.

@strix-security

strix-security Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 4 commits after the last Strix review (29b8b1e). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ No open security findings.

PR summary: Fixes result-column ordering in `DatabricksQueryTool` by tracking column names in an ordered list seeded from the query's schema instead of a set, so headers follow the SELECT list and generated `Column_N` names are appended at the end. Adds two tests using a fake Databricks workspace client.

Review summary

Reviewed the result-column ordering fix in DatabricksQueryTool: the diff replaces set-based column tracking with an ordered list seeded from the result schema, and appends generated Column_N names only when absent. I traced the changed lines through the row-normalization loop and _format_results, and checked the surrounding _run flow (LIMIT appending, statement execution, credential handling) plus the new test file. The column list stays bounded and deduplicated, no new data source, sink or trust boundary is introduced, and the change is purely presentational — no security issues found.

Review 1 on this PR · 29b8b1e · requested Nov 24 03:22 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings · Try a deeper review with @strix-security /deep

The result normalizer collected every column name in a set and rebuilt each
row from that set. Column order in the formatted table therefore depended on
set iteration order, so the same query could return columns in a different
order from run to run and never matched the SELECT list.

Track the columns as an ordered list seeded from the result schema, and
append generated names for extra values after the schema columns.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this is a clean, minimal fix: an ordered all_columns list makes the table columns follow the schema, with Column_N names appended at the end.

I checked the tests against the code before and after your change. Both new tests in tests/tools/test_databricks_query_tool.py fail without your change and pass with it. A separate 12-column ordering test behaves the same way.

Optional follow-up: add a test for the result.result.data fallback path, which your change at line ~760 also covers.

@Vidit-Ostwal
Vidit Ostwal (Vidit-Ostwal) merged commit 5f2f342 into crewAIInc:main Nov 24, 2026
55 checks passed
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.

DatabricksQueryTool formats result columns in random order

2 participants