Skip to content

Commit 103f84c

Browse files
authored
[DCV-3638] Detect TOP and FETCH in has_limit to avoid unnecessary query wrapping (#18)
1 parent 3cfac50 commit 103f84c

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/dbt_core_interface/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,13 @@ def run_sql(
288288
original_code = model_context["compiled_code"]
289289
sql_header = model_context["config"].get("sql_header") or ""
290290

291-
# Check if query already has a LIMIT clause at the end - if so, execute as-is
291+
# Check if query already has a row-limiting clause - if so, execute as-is
292292
query: str
293-
has_limit = bool(re.search(r"\slimit\s+\d+(\s+offset\s+\d+)?\s*;?\s*$", original_code, re.IGNORECASE))
293+
has_limit = bool(
294+
re.search(r"\slimit\s+\d+(\s+offset\s+\d+)?\s*;?\s*$", original_code, re.IGNORECASE)
295+
or re.search(r"^\s*select\s+top\s+\(?\d+\)?", original_code, re.IGNORECASE)
296+
or re.search(r"fetch\s+(?:first|next)\s+\d+\s+rows?\s+only\s*;?\s*$", original_code, re.IGNORECASE)
297+
)
294298

295299
if has_limit:
296300
query = f"{sql_header}\n{original_code}" if sql_header else original_code

0 commit comments

Comments
 (0)