Keep source, config, and deploy files. Leave runtime data, uploads, and logs out of git. Co-authored-by: Cursor <cursoragent@cursor.com>
68 lines
2.0 KiB
Python
68 lines
2.0 KiB
Python
"""Push-down hints in sql_query observations (loaded without tools/__init__.py)."""
|
|
|
|
import importlib.util
|
|
from pathlib import Path
|
|
|
|
_SQL_QUERY_PATH = (
|
|
Path(__file__).resolve().parents[2]
|
|
/ "kamidso"
|
|
/ "engine"
|
|
/ "app"
|
|
/ "openapi"
|
|
/ "api_v1"
|
|
/ "tools"
|
|
/ "sql_query.py"
|
|
)
|
|
|
|
|
|
def _sql_query_mod():
|
|
spec = importlib.util.spec_from_file_location(
|
|
"kamidso_sql_query_isolated", _SQL_QUERY_PATH
|
|
)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
assert spec.loader is not None
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
def test_sql_lacks_server_aggregation_skips_limit_and_group():
|
|
mod = _sql_query_mod()
|
|
assert mod._sql_lacks_server_aggregation("SELECT id FROM users") is True
|
|
assert mod._sql_lacks_server_aggregation("SELECT id FROM users LIMIT 20") is False
|
|
assert mod._sql_lacks_server_aggregation(
|
|
"SELECT user_id, COUNT(*) FROM orders GROUP BY user_id"
|
|
) is False
|
|
assert mod._sql_lacks_server_aggregation(
|
|
"SELECT u.id FROM users u JOIN orders o ON o.user_id = u.id"
|
|
) is False
|
|
|
|
|
|
def test_capped_sql_observation_suggests_join_group(tmp_path, monkeypatch):
|
|
mod = _sql_query_mod()
|
|
monkeypatch.setattr(
|
|
"kamidso.engine.core.configs.model_config.PILOT_PATH",
|
|
str(tmp_path),
|
|
raising=False,
|
|
)
|
|
rows = [[i] for i in range(200)]
|
|
content = mod.render_query_observation(
|
|
{"conv_id": "conv_cap"}, ["id"], rows, "mysql", work_dir=str(tmp_path)
|
|
)
|
|
assert "JOIN / GROUP BY / COUNT(*)" in content
|
|
assert "Python may only format" in content
|
|
|
|
|
|
def test_capped_mongo_observation_suggests_lookup_group(tmp_path):
|
|
mod = _sql_query_mod()
|
|
rows = [[i] for i in range(200)]
|
|
content = mod.render_query_observation(
|
|
{"conv_id": "conv_mongo_cap"},
|
|
["id"],
|
|
rows,
|
|
"mongodb",
|
|
work_dir=str(tmp_path),
|
|
)
|
|
assert "$group" in content
|
|
assert "$lookup" in content
|
|
assert "Do not group these 200 rows in Python" in content
|