Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- @abu-tahir-0 for five fixes on their first day: strict `check --range` bounds (#85), one row-count history per connection (#86), whole-number and non-boolean budget limits (#84), refusing unknown `gulliver.toml` sections (#83) and GV013 for leading `_` (#82).

### Fixed
- `run --max-rows N` refuses a value below 0 with `status: error` and a `next_step`, exits 1, and skips estimating, running, or writing a receipt, while `--max-rows 0` stays valid (#119).
- `spend --days N`, `calibration --days N` and `top --limit N` refuse a value below 1 with `status: error` and a `next_step`, and exit 1 instead of quietly reporting an empty or off-by-one window. `top --analyze` refuses a negative value too, while `--analyze 0` still lists the groups without analysing them (#92).
- `check --range` refuses `nan`, infinite and non-numeric bounds and an inverted `min > max` before any query runs, naming the column. Before, `nan` / `inf` reached the engine as column names and an inverted range silently failed every row (#74, #85).
- `check --max-change` keys the row-count history on the resolved connection name, so a check with and without `-c default` shares one history. Histories saved under the old key are still read and migrated (#75, #86).
Expand Down
5 changes: 5 additions & 0 deletions src/gulliver/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ def _rehearse(settings: Settings, sql: str, connection: str | None, fractions: l
# -- run -------------------------------------------------------------------------------
def run(settings: Settings, sql: str, connection: str | None = None, max_rows: int | None = None,
allow_write: bool = False) -> dict[str, Any]:
if max_rows is not None and (isinstance(max_rows, bool) or not isinstance(max_rows, int) or max_rows < 0):
return _error(
f"--max-rows must be at least 0, got {max_rows}.",
"Pass a non-negative integer for --max-rows (0 returns row counts without preview rows).",
)
try:
return _run(settings, sql, connection, max_rows, allow_write)
except ConfigError as exc:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli_and_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,18 @@ def test_cli_translate_output_file(tmp_path, capsys):
data = json.loads(capsys.readouterr().out)
assert data["warnings"] # still printed
assert out.read_text(encoding="utf-8") == data["sql"]


def test_cli_run_refuses_negative_max_rows_and_allows_zero(demo_db, state, capsys):
assert main(["--dsn", demo_db, "run", "--max-rows", "-5", "--json", "select count(*) from users"]) == 1
data = json.loads(capsys.readouterr().out)
assert data["status"] == "error" and "-5" in data["error"] and data["next_step"]

assert main(["--dsn", demo_db, "run", "--max-rows", "-2", "select count(*) from users"]) == 1
plain = json.loads(capsys.readouterr().out)
assert plain["status"] == "error" and "-2" in plain["error"]

assert main(["--dsn", demo_db, "run", "--max-rows", "0", "--json", "select count(*) from users"]) == 0
zero = json.loads(capsys.readouterr().out)
assert zero["status"] == "ok" and zero["preview"] == [] and zero["actual"]["rows"] == 1

17 changes: 17 additions & 0 deletions tests/test_flow_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,20 @@ def test_budget_defaults_are_sane():
b = Budget()
assert b.approval_over_usd < b.max_query_usd < b.day_usd
assert Connection("x", "duckdb").public_dict() == {"name": "x", "engine": "duckdb"}


def test_run_refuses_negative_max_rows_and_allows_zero(settings):
err = core.run(settings, GOOD, max_rows=-5)
assert err["status"] == "error"
assert "-5" in err["error"] and "--max-rows" in err["error"] and err["next_step"]
assert core.list_receipts(settings)["receipts"] == []

zero = core.run(settings, GOOD, max_rows=0)
assert zero["status"] == "ok"
assert zero["preview"] == []
assert zero["actual"]["rows"] == 8

over = core.run(settings, GOOD, max_rows=settings.budget.max_output_rows + 50)
assert over["status"] == "ok"
assert len(over["preview"]) <= settings.budget.max_output_rows