Skip to content

Commit 82c4cc7

Browse files
fix(server): accept curation preview SQL that ends in a comment (#741)
Preview wraps the caller's SQL in subqueries. A trailing line comment swallowed the wrapper's closing parenthesis, and a semicolon followed by a comment was not removed, so both returned a 400 that report accepts. Find the terminator the way the SDK does (the last non-comment DuckDB token, by UTF-8 byte offset), and end the body on a new line when it ends in a comment. SQL without a trailing comment is passed through unchanged, and semicolons or comment markers inside strings keep their meaning. Fixes #738
1 parent 7d202bc commit 82c4cc7

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎packages/hflow-server/src/hflow_server/_curation.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ def _stripped_sql_or_refuse(raw_sql: str) -> str:
154154
return stripped_sql
155155

156156

157+
def _subquery_body(user_sql: str) -> str:
158+
"""The SQL as the body of ``(...)``: no statement terminator, and a
159+
trailing comment ends on its own line, so a line comment cannot swallow
160+
the wrapper's closing parenthesis. SQL without either is unchanged.
161+
162+
The terminator is found the way the SDK finds it (``hflow.curation``):
163+
the last token DuckDB does not classify as a comment, so ``; -- end``
164+
and ``; /* end */`` lose their semicolon too, while semicolons and
165+
comment markers inside string literals are left alone. Positions are
166+
UTF-8 byte offsets, hence the encode/decode.
167+
"""
168+
sql_bytes = user_sql.encode("utf-8")
169+
tokens = duckdb.tokenize(user_sql)
170+
code_tokens = [position for position, kind in tokens if kind != duckdb.token_type.comment]
171+
if code_tokens and sql_bytes[code_tokens[-1] : code_tokens[-1] + 1] == b";":
172+
sql_bytes = sql_bytes[: code_tokens[-1]] + sql_bytes[code_tokens[-1] + 1 :]
173+
ends_in_comment = bool(tokens) and tokens[-1][1] == duckdb.token_type.comment
174+
return sql_bytes.decode("utf-8") + ("\n" if ends_in_comment else "")
175+
176+
157177
def _bad_sql_refusal(error: duckdb.Error) -> HTTPException:
158178
# DuckDB's parser/binder message IS the useful diagnostic; bad SQL is the
159179
# caller's mistake, never a server fault (so 400, never 500).
@@ -286,6 +306,7 @@ def run_preview(
286306
connection: duckdb.DuckDBPyConnection, user_sql: str, *, limit: int, include_stats: bool
287307
) -> CurationPreviewResponse:
288308
"""Preview rows, the full count, and (optionally) SUMMARIZE column stats."""
309+
user_sql = _subquery_body(user_sql)
289310
columns = _described_columns(connection, user_sql)
290311
replace_clause = _timestamp_replace_clause(columns)
291312
select_head = f"SELECT * {replace_clause}" if replace_clause else "SELECT *"

‎packages/hflow-server/tests/test_server_curation_preview.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,40 @@ def test_preview_accepts_from_first_and_parenthesized_selects(api: TestClient) -
283283
):
284284
response = api.post("/api/v1/curation/preview", json={"sql": sql})
285285
assert response.status_code == 200, response.text
286+
287+
288+
@pytest.mark.parametrize(
289+
"sql",
290+
[
291+
"SELECT 1 AS value -- end",
292+
"SELECT 1 AS value; -- end",
293+
"SELECT 1 AS value; /* end */",
294+
"SELECT 1 AS value -- end\n",
295+
],
296+
)
297+
@pytest.mark.parametrize("stats", [False, True])
298+
def test_preview_accepts_sql_ending_in_a_comment(api: TestClient, sql: str, stats: bool) -> None:
299+
plain = _preview(api, sql="SELECT 1 AS value", stats=stats)
300+
payload = _preview(api, sql=sql, stats=stats)
301+
assert payload["rows"] == plain["rows"] == [{"value": 1}]
302+
assert payload["row_count"] == plain["row_count"] == 1
303+
assert (payload["column_stats"] is None) is (not stats)
304+
305+
306+
def test_preview_keeps_semicolons_and_comment_markers_inside_strings(api: TestClient) -> None:
307+
text = "a; -- not a comment /* nor this */ tiếng Việt;"
308+
payload = _preview(api, sql=f"SELECT '{text}' AS value; -- end")
309+
assert payload["rows"] == [{"value": text}]
310+
311+
312+
def test_preview_still_refuses_two_statements_hidden_behind_a_comment(api: TestClient) -> None:
313+
response = api.post(
314+
"/api/v1/curation/preview", json={"sql": "SELECT 1 AS value; SELECT 2 -- end"}
315+
)
316+
assert response.status_code == 400
317+
assert response.json()["detail"] == "sql must be exactly one read-only SELECT statement"
318+
319+
320+
def test_preview_sql_is_unchanged_when_it_has_no_trailing_comment(api: TestClient) -> None:
321+
payload = _preview(api, sql="SELECT 1 AS value", limit=5)
322+
assert payload["sql"] == "SELECT * FROM (SELECT 1 AS value) LIMIT 5"

0 commit comments

Comments
 (0)