diff --git a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts index 8f2aba9adf..3426360316 100644 --- a/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts +++ b/apps/desktop/src/lib/__tests__/sql/sqlStatementRanges.spec.ts @@ -771,6 +771,14 @@ describe("buildExecutionCandidates", () => { expect(candidateKinds(candidates)).toEqual(["cursor", "all"]); }); + it("preserves leading optimizer hints in current statement candidates", () => { + const hintedSql = "/*+ SET(polar_csi.enable_query on) SET(polar_csi.cost_threshold 0)*/\nselect count(1) from xxx"; + const sql = `select 1;\n${hintedSql};`; + const candidates = buildExecutionCandidates(sql, indexOf(sql, "count"), "postgres"); + + expect(candidates[0].sql).toBe(hintedSql); + }); + it("uses the cursor statement for the first candidate when there is no selection", () => { const sql = "SELECT *\nFROM users\nWHERE active = 1"; const candidates = buildExecutionCandidates(sql, indexOf(sql, "users")); diff --git a/apps/desktop/src/lib/sql/sqlStatementRanges.ts b/apps/desktop/src/lib/sql/sqlStatementRanges.ts index 5230ad6b72..7a314f5b07 100644 --- a/apps/desktop/src/lib/sql/sqlStatementRanges.ts +++ b/apps/desktop/src/lib/sql/sqlStatementRanges.ts @@ -282,6 +282,7 @@ export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType let statementStart = -1; let statementEnd = -1; let statementHitStart = 0; + let pendingHintStart = -1; let customDelimiter: string | null = null; let state: QuoteState = "none"; let dollarTag = ""; @@ -290,13 +291,17 @@ export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType const isWhitespace = (ch: string) => ch === " " || ch === "\t" || ch === "\r" || ch === "\n"; const markContent = (pos: number) => { - if (statementStart === -1) statementStart = pos; + if (statementStart === -1) { + statementStart = pendingHintStart === -1 ? pos : pendingHintStart; + pendingHintStart = -1; + } statementEnd = pos + 1; }; const flush = (to = statementEnd) => { if (statementStart === -1) { statementEnd = -1; + pendingHintStart = -1; return; } const trimmedTo = trimRangeEnd(sql, statementStart, to); @@ -305,6 +310,7 @@ export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType } statementStart = -1; statementEnd = -1; + pendingHintStart = -1; }; while (i < len) { @@ -418,6 +424,7 @@ export function splitSqlStatementRanges(sql: string, databaseType?: DatabaseType } // Block comments consume until the closing */. if (ch === "/" && next === "*") { + if (statementStart === -1 && pendingHintStart === -1 && sql[i + 2] === "+") pendingHintStart = i; const close = sql.indexOf("*/", i + 2); i = close === -1 ? len : close + 2; continue;