feat: add daemon sql batch analysis

This commit is contained in:
Andrey Avtomonov 2026-05-11 16:56:50 +02:00
parent c45d131a1f
commit ffbbaf417a
4 changed files with 260 additions and 0 deletions

View file

@ -280,6 +280,37 @@ def test_sql_parse_table_identifier_endpoint() -> None:
assert body["results"]["template"]["reason"] == "looker_template_unresolved"
def test_sql_analyze_batch_endpoint_returns_per_item_results() -> None:
client = TestClient(create_app())
response = client.post(
"/sql/analyze-batch",
json={
"dialect": "postgres",
"max_workers": 1,
"items": [
{
"id": "orders",
"sql": "select status from public.orders where created_at is not null",
},
{"id": "broken", "sql": "select * from where"},
],
},
)
assert response.status_code == 200
body = response.json()
assert body["results"]["orders"]["tables_touched"] == ["public.orders"]
assert body["results"]["orders"]["columns_by_clause"] == {
"select": ["status"],
"where": ["created_at"],
}
assert body["results"]["orders"]["error"] is None
assert body["results"]["broken"]["tables_touched"] == []
assert body["results"]["broken"]["columns_by_clause"] == {}
assert body["results"]["broken"]["error"] is not None
def test_semantic_query_endpoint_returns_sql() -> None:
client = TestClient(create_app())

View file

@ -0,0 +1,53 @@
from __future__ import annotations
from ktx_daemon.sql_analysis import (
AnalyzeSqlBatchItem,
AnalyzeSqlBatchRequest,
analyze_sql_batch_response,
)
def test_analyze_sql_batch_extracts_tables_and_clause_columns() -> None:
response = analyze_sql_batch_response(
AnalyzeSqlBatchRequest(
dialect="postgres",
items=[
AnalyzeSqlBatchItem(
id="orders_by_customer",
sql=(
"select o.status, count(*) "
"from public.orders o "
"join public.customers c on o.customer_id = c.id "
"where o.created_at >= current_date - interval '30 day' "
"group by o.status"
),
)
],
max_workers=1,
)
)
result = response.results["orders_by_customer"]
assert result.error is None
assert result.tables_touched == ["public.orders", "public.customers"]
assert result.columns_by_clause == {
"select": ["status"],
"where": ["created_at"],
"join": ["customer_id", "id"],
"groupBy": ["status"],
}
def test_analyze_sql_batch_returns_per_item_parse_errors() -> None:
response = analyze_sql_batch_response(
AnalyzeSqlBatchRequest(
dialect="postgres",
items=[AnalyzeSqlBatchItem(id="broken", sql="select * from where")],
max_workers=1,
)
)
result = response.results["broken"]
assert result.tables_touched == []
assert result.columns_by_clause == {}
assert result.error is not None