Repository navigation
Description
Description
DatabricksQueryTool._run normalizes result rows through a set of column names:
# Keep track of all dynamic columns we create
all_columns = set(columns)
...
normalized_results = []
for row in chunk_results:
normalized_row = {
col: row.get(col, None) for col in all_columns
}
normalized_results.append(normalized_row)_format_results then takes the header from list(results[0].keys()). Because all_columns is a set, the order of the rebuilt dictionaries depends on Python's hash randomization. The formatted table's columns therefore come out in an arbitrary order that changes between processes and does not follow the query's SELECT list. Extra values that get generated names (Column_5, ...) are also placed arbitrarily among the real columns.
Reproduction
Run this against current main (d1aa368749f932311a6c1916e63a756877cab55a), with a fake workspace client that returns schema columns alpha, beta, gamma, delta, epsilon and one row of five values:
from types import SimpleNamespace
from crewai_tools import DatabricksQueryTool
names = ["alpha", "beta", "gamma", "delta", "epsilon"]
result = SimpleNamespace(
status=SimpleNamespace(state="SUCCEEDED"),
manifest=SimpleNamespace(
schema=SimpleNamespace(columns=[SimpleNamespace(name=n) for n in names])
),
result=SimpleNamespace(data_array=[[[1, 2, 3, 4, 5]]], data=None),
)
class Statement:
def execute_statement(self, **kwargs):
return SimpleNamespace(statement_id="s1")
def get_statement(self, statement_id):
return result
tool = DatabricksQueryTool(default_warehouse_id="wh")
tool._workspace_client = SimpleNamespace(statement_execution=Statement())
print(tool._run(query="SELECT * FROM t").splitlines()[0])Observed header in one process: gamma | alpha | delta | beta | epsilon. Re-running gives a different order.
Expected behavior
The output columns follow the order reported by the result schema, with generated Column_N columns appended after them.
Suggested fix
Track the columns in an ordered list seeded from columns instead of a set, and append generated names rather than adding them to a set. A fix PR will follow.
Tests
New tests in lib/crewai-tools/tests/tools/test_databricks_query_tool.py assert the header order for a normal row and for a row with an extra value (Column_5 last). Both fail on unmodified main with the shuffled header above.
Prepared with an AI coding agent. Please apply the llm-generated label; this account cannot set repository labels.
Activity