Skip to content

Commit c9a9cf9

Browse files
hkaltaiclaude
andcommitted
fix: Oracle, Trino and Fabric keep their reads; TRUNCATE /* c */ ( is the function
- Oracle, Trino and Fabric are lexed as the supported dialects they are, so backslashes and # in their reads no longer make the SQL uncertain. - TRUNCATE followed by a closed block comment and ( counts as the function. - The dbt adapter-type tests cover the modification-time part of the cache key and the saved altimate-dbt config. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1311bf7 commit c9a9cf9

4 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎packages/opencode/src/altimate/tools/sql-classify.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,15 @@ function mentionsHardDeny(sql: string): boolean {
211211
if (word === "DROP") drop = true
212212
else if (drop && (word === "DATABASE" || word === "SCHEMA")) return true
213213
else if (word === "TRUNCATE") {
214+
// The TRUNCATE() function: "(" next, past whitespace and closed block comments.
214215
let k = j
215-
while (k < sql.length && /\s/.test(sql[k])) k++
216+
for (;;) {
217+
while (k < sql.length && /\s/.test(sql[k])) k++
218+
if (!sql.startsWith("/*", k)) break
219+
const end = sql.indexOf("*/", k + 2)
220+
if (end < 0) break
221+
k = end + 2
222+
}
216223
if (sql[k] !== "(") return true
217224
}
218225
i = j

‎packages/opencode/src/altimate/tools/sql-tokenize.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ export function tokenizeSql(sql: string, dialect?: string): { tokens: SqlToken[]
1717
// and PostgreSQL backticks can be operators. Unknown dialects keep both visible.
1818
const backtickIdentifiers = /^(mysql|mariadb|sqlite|bigquery|clickhouse|spark|databricks|hive)$/i.test(dialect ?? "")
1919
const bracketIdentifiers = /^(sqlserver|tsql|mssql|sqlite)$/i.test(dialect ?? "")
20-
// A dialect these rules do not model (none, "generic", "unknown", oracle, trino...) may still
21-
// escape a quote with a backslash or start a comment with #, so both make the SQL uncertain.
20+
// A dialect that is not one of the supported warehouse types (none, "generic", "unknown"...)
21+
// may still escape a quote with a backslash or start a comment with #, so both make the SQL
22+
// uncertain. Oracle, Trino and Fabric do neither.
2223
const unmodeled =
23-
!/^(mysql|mariadb|bigquery|clickhouse|sqlite|postgres|postgresql|duckdb|sqlserver|tsql|mssql|spark|databricks|hive|redshift|snowflake)$/i.test(dialect ?? "")
24+
!/^(mysql|mariadb|bigquery|clickhouse|sqlite|postgres|postgresql|duckdb|sqlserver|tsql|mssql|fabric|spark|databricks|hive|redshift|snowflake|oracle|trino)$/i.test(
25+
dialect ?? "",
26+
)
2427
let complete = true
2528
let i = 0
2629
let code = 0

‎packages/opencode/test/altimate/native/dbt-adapter-type.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,31 @@ test("a missing or unreadable manifest still reports a dbt project", async () =>
4545

4646
test("re-reads the manifest only when it changes", async () => {
4747
manifest(path.join(dir, "target"), "postgres")
48+
const file = path.join(dir, "target", "manifest.json")
49+
const first = new Date(Date.now() - 60_000)
50+
utimesSync(file, first, first)
51+
expect((await dbtProjectAdapterType()).type).toBe("postgres")
52+
// Same size, so only the modification time can tell the new manifest apart.
53+
manifest(path.join(dir, "target"), "bigquery")
54+
utimesSync(file, first, first)
4855
expect((await dbtProjectAdapterType()).type).toBe("postgres")
49-
manifest(path.join(dir, "target"), "duckdb")
5056
const later = new Date(Date.now() + 5_000)
51-
utimesSync(path.join(dir, "target", "manifest.json"), later, later)
52-
expect((await dbtProjectAdapterType()).type).toBe("duckdb")
57+
utimesSync(file, later, later)
58+
expect((await dbtProjectAdapterType()).type).toBe("bigquery")
59+
})
60+
61+
test("a saved altimate-dbt config names the project, and a malformed one falls back to discovery", async () => {
62+
const { path: configPath } = await import("../../../../dbt-tools/src/config")
63+
const other = mkdtempSync(path.join(os.tmpdir(), "dbt-adapter-type-other-"))
64+
try {
65+
manifest(path.join(other, "target"), "redshift")
66+
mkdirSync(path.dirname(configPath()), { recursive: true })
67+
writeFileSync(configPath(), JSON.stringify({ projectRoot: other }))
68+
expect(await dbtProjectAdapterType()).toEqual({ project: true, type: "redshift" })
69+
writeFileSync(configPath(), "{not json")
70+
manifest(path.join(dir, "target"), "duckdb")
71+
expect(await dbtProjectAdapterType()).toEqual({ project: true, type: "duckdb" })
72+
} finally {
73+
rmSync(other, { recursive: true, force: true })
74+
}
5375
})

‎packages/opencode/test/altimate/tools/sql-classify.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,16 @@ describe("literal and comment lexing where the dialect decides", () => {
403403
expect(performance.now() - started).toBeLessThan(2_000)
404404
expect(classifyAndCheck("SELECT 'x\\'' ; DROP -- c\n SCHEMA s").blocked).toBe(true)
405405
// A dialect the lexer does not model gets the same guards as none.
406-
for (const dialect of ["generic", "unknown", "oracle"])
406+
for (const dialect of ["generic", "unknown"])
407407
expect(classifyAndCheck("SELECT 'x\\'' ; DROP SCHEMA s; -- '", dialect).blocked).toBe(true)
408+
// Oracle, Trino and Fabric neither escape quotes with a backslash nor comment with #.
409+
for (const dialect of ["oracle", "trino", "fabric"]) {
410+
expect(classifyAndCheck("SELECT * FROM t WHERE c LIKE '%\\_%' ESCAPE '\\'", dialect)).toEqual({ queryType: "read", blocked: false })
411+
expect(classifyAndCheck("SELECT 'C:\\reports\\' AS p FROM t", dialect)).toEqual({ queryType: "read", blocked: false })
412+
}
413+
expect(classifyAndCheck("SELECT order# FROM orders", "oracle").queryType).toBe("read")
414+
// A comment between TRUNCATE and ( keeps it the function.
415+
expect(classifyAndCheck("SELECT TRUNCATE /* c */ (amount, 2), 'a\\'b' AS v FROM t", "mysql").blocked).toBe(false)
408416
// Complete SQL keeps its literals: hard-deny text inside a string is not a statement.
409417
expect(classifyAndCheck("SELECT '; DROP SCHEMA x' AS v", "postgres")).toEqual({ queryType: "read", blocked: false })
410418
})

0 commit comments

Comments
 (0)