Skip to content

Commit 449513d

Browse files
sahrizviclaude
andcommitted
fix(validate): fold only names the schema holds, one preparation per schema source
Bot review of 8ecf029 on #1343. - The SQL fold touched every quoted all-uppercase token, and on MySQL/BigQuery/SQLite `"SHIPPED"` is a string literal: a query's values changed under validation. Only tokens whose lowercase form is a name the folded schema holds (table keys, each dotted segment, columns) are folded - `hasSchema` was not a fold flag: for a DDL file the head SQL was left as written but `base_sql` and lineage queries were folded, and the PII base comparison used the raw base. `prepareSql` exposes `foldSql`, the same preparation for any sibling SQL against that schema — identity for DDL — and `check` (lintDiff + PII subtraction), `equivalence` and `track_lineage` use it - `sql.analyze`, `sql.optimize`, `sql.fix`, `sql.rewrite`, `sql.diff` and `lineage.check` (`native/sql/register.ts`) match SQL against a schema too and were still on the unfolded resolver; they prepare the pair now - Test guard uses `require` (loads the binding) rather than `require.resolve`; the temp schema directory is removed; a comment named the wrong handler Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 8ecf029 commit 449513d

4 files changed

Lines changed: 124 additions & 58 deletions

File tree

‎packages/opencode/src/altimate/native/altimate-core.ts‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import * as core from "@altimateai/altimate-core"
1212
import { EngineCoerce } from "./engine-coerce"
1313
import { register } from "./dispatcher"
14-
import { schemaOrEmpty, SchemaResolver, prepareSql, foldQuotedIdentifierCase } from "./schema-resolver"
14+
import { schemaOrEmpty, SchemaResolver, prepareSql } from "./schema-resolver"
1515
import type { AltimateCoreResult } from "./types"
1616

1717
// ---------------------------------------------------------------------------
@@ -190,9 +190,10 @@ export function registerAll(): void {
190190
// 6. altimate_core.check — composite: validate + lint + scan_sql
191191
register("altimate_core.check", async (params) => {
192192
try {
193-
const { sql, schema, hasSchema } = prepareSql(params.sql, params.schema_path, params.schema_context)
194-
// The base SQL is matched against the same schema by lintDiff, so it gets the same fold.
195-
const baseSql = params.base_sql && hasSchema ? foldQuotedIdentifierCase(params.base_sql) : params.base_sql
193+
const { sql, schema, foldSql } = prepareSql(params.sql, params.schema_path, params.schema_context)
194+
// The base SQL is matched against the same schema (lintDiff, the PII subtraction),
195+
// so it gets exactly the preparation the head SQL got.
196+
const baseSql = params.base_sql ? foldSql(params.base_sql) : params.base_sql
196197
// NOTE: validation is deliberately NOT diff-scoped against base_sql.
197198
// The engine validates fail-fast (only the FIRST error is reported), so
198199
// subtracting base errors can hide genuinely new breakage behind a
@@ -265,15 +266,15 @@ export function registerAll(): void {
265266
let pii: Record<string, unknown>
266267
try {
267268
pii = toData(core.checkQueryPii(sql, schema))
268-
if (params.base_sql && Array.isArray(pii.pii_columns) && (pii.pii_columns as any[]).length) {
269+
if (baseSql && Array.isArray(pii.pii_columns) && (pii.pii_columns as any[]).length) {
269270
try {
270271
// Pre-existing exposures are not introduced by this change. The
271272
// identity INCLUDES the sorted output aliases — adding or renaming
272273
// a SELECT-list alias for an already-exposed column is a NEW
273274
// output exposure and must still surface.
274275
const exposureKey = (c: any) =>
275276
`${c.table}|${c.column}|${[...(c.query_targets ?? [])].sort().join(",")}`
276-
const baseExposed = new Set(core.checkQueryPii(params.base_sql, schema).pii_columns.map(exposureKey))
277+
const baseExposed = new Set(core.checkQueryPii(baseSql, schema).pii_columns.map(exposureKey))
277278
const remaining = (pii.pii_columns as any[]).filter((c: any) => !baseExposed.has(exposureKey(c)))
278279
pii = {
279280
...pii,
@@ -432,14 +433,13 @@ export function registerAll(): void {
432433
register("altimate_core.equivalence", async (params) => {
433434
try {
434435
const one = prepareSql(params.sql1, params.schema_path, params.schema_context)
435-
const two = prepareSql(params.sql2, params.schema_path, params.schema_context)
436436
// Pass the optional dialect hint so dialect-specific compiled warehouse SQL
437437
// (e.g. Snowflake semi-structured `col:field`) parses and the pair is
438438
// decidable instead of abstaining on a syntax error. Supported since
439439
// [email protected]. dialectHint coerces "" (the ReviewConfig default)
440440
// to undefined: the engine throws on an unknown dialect "", and "" must
441441
// mean auto-detect, not a real dialect.
442-
const raw = await core.checkEquivalence(one.sql, two.sql, one.schema, EngineCoerce.dialectHint(params.dialect))
442+
const raw = await core.checkEquivalence(one.sql, one.foldSql(params.sql2), one.schema, EngineCoerce.dialectHint(params.dialect))
443443
const data = toData(raw)
444444
return ok(true, data)
445445
} catch (e) {
@@ -562,9 +562,8 @@ export function registerAll(): void {
562562
// 21. altimate_core.track_lineage
563563
register("altimate_core.track_lineage", async (params) => {
564564
try {
565-
const { schema, hasSchema } = prepareSql("", params.schema_path, params.schema_context)
566-
const queries = hasSchema ? params.queries.map(foldQuotedIdentifierCase) : params.queries
567-
const raw = core.trackLineage(queries, schema)
565+
const { schema, foldSql } = prepareSql("", params.schema_path, params.schema_context)
566+
const raw = core.trackLineage(params.queries.map(foldSql), schema)
568567
return ok(true, toData(raw))
569568
} catch (e) {
570569
return fail(e)

‎packages/opencode/src/altimate/native/schema-resolver.ts‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,41 @@ export function foldSchemaCase(def: { tables: Record<string, any> }): { tables:
142142
* reserved word (`"ORDER"`) stays an identifier. String literals and comments are stepped
143143
* over, not searched. Mixed-case quoted names are the exact-match case and are untouched.
144144
*/
145-
export function foldQuotedIdentifierCase(sql: string): string {
145+
export function foldQuotedIdentifierCase(sql: string, names?: ReadonlySet<string>): string {
146146
// Skipped spans, in order: dollar-quoted strings ($$…$$, $tag$…$tag$), E'…' strings
147147
// with backslash escapes, ordinary '…' strings ('' doubles), line and block comments.
148148
// Then the two quoted-identifier forms: "…" (SQL) and `…` (BigQuery/Databricks), a
149149
// doubled quote inside either kept as written — that name is not a plain identifier.
150+
// With `names`, only a token whose lowercase form (or last dotted segment) is a name
151+
// the folded schema holds is touched: on MySQL/BigQuery/SQLite `"SHIPPED"` is a
152+
// string literal, and a query's values must not change under validation.
150153
return sql.replace(
151154
/\$([A-Za-z_][A-Za-z0-9_]*)?\$[\s\S]*?\$\1\$|[eE]'(?:[^'\\]|\\[\s\S]|'')*'|'(?:[^']|'')*'|--[^\n]*|\/\*[\s\S]*?\*\/|"((?:[^"]|"")*)"|`((?:[^`]|``)*)`/g,
152155
(match, _tag, dq?: string, bq?: string) => {
153156
const quoted = dq ?? bq
154157
if (quoted === undefined) return match
155158
// A doubled quote inside the name (`"A""B"`) is not a plain identifier: kept.
156159
if (!/^[A-Z_][A-Z0-9_$.]*$/.test(quoted)) return match
160+
const lower = quoted.toLowerCase()
161+
if (names && !names.has(lower) && !names.has(lower.slice(lower.lastIndexOf(".") + 1))) return match
157162
const mark = match[0]
158-
return `${mark}${quoted.toLowerCase()}${mark}`
163+
return `${mark}${lower}${mark}`
159164
},
160165
)
161166
}
162167

168+
/** Every folded table key and column name in a definition, plus each dotted segment
169+
* of a table key, so `"DB"."SCHEMA"."ORDERS"` can meet a `db.schema.orders` key. */
170+
function schemaNames(def: { tables: Record<string, any> }): Set<string> {
171+
const names = new Set<string>()
172+
for (const [table, value] of Object.entries(def.tables ?? {})) {
173+
names.add(table)
174+
for (const segment of table.split(".")) names.add(segment)
175+
if (Array.isArray(value?.columns)) for (const c of value.columns) if (typeof c?.name === "string") names.add(c.name)
176+
}
177+
return names
178+
}
179+
163180
/**
164181
* Normalize a schema_context into SchemaDefinition JSON format.
165182
* Accepts both flat and SchemaDefinition formats.
@@ -215,42 +232,52 @@ export interface PreparedSql {
215232
schema: Schema
216233
/** A schema was really supplied (see `schemaProvided`). */
217234
hasSchema: boolean
235+
/** The same preparation for another SQL text matched against this schema (a base
236+
* query, a lineage batch): folded exactly when `sql` was, untouched otherwise. */
237+
foldSql: (other: string) => string
218238
}
219239

220240
/**
221241
* The SQL and schema for an operation that matches one against the other: the schema
222242
* with its identifier case folded and the SQL's quoted all-uppercase identifiers folded
223243
* to meet it (#1333) — both halves or neither, since a folded schema against unfolded
224-
* SQL, or the reverse, is the mismatch the fold exists to remove. A `schema_path`
225-
* in JSON or YAML goes through the same normalisation as an inline context; a DDL file
226-
* carries its own case semantics and is loaded as-is, with the SQL left alone.
244+
* SQL, or the reverse, is the mismatch the fold exists to remove. Only quoted names the
245+
* folded schema holds are touched. A `schema_path` in JSON or YAML goes through the
246+
* same normalisation as an inline context; a DDL file carries its own case semantics
247+
* and is loaded as-is, with the SQL left alone.
227248
*/
228249
export function prepareSql(sql: string, schemaPath?: string, schemaContext?: Record<string, any>): PreparedSql {
250+
const asIs = (other: string) => other
229251
if (schemaPath) {
230252
const loaded = loadSchemaFile(schemaPath)
231-
if (loaded.folded) return { sql: foldQuotedIdentifierCase(sql), schema: loaded.schema, hasSchema: true }
232-
return { sql, schema: loaded.schema, hasSchema: true }
253+
if (loaded.folded) {
254+
const names = schemaNames(loaded.folded)
255+
const foldSql = (other: string) => foldQuotedIdentifierCase(other, names)
256+
return { sql: foldSql(sql), schema: loaded.schema, hasSchema: true, foldSql }
257+
}
258+
return { sql, schema: loaded.schema, hasSchema: true, foldSql: asIs }
233259
}
234260
if (schemaProvided(undefined, schemaContext)) {
235-
return {
236-
sql: foldQuotedIdentifierCase(sql),
237-
schema: Schema.fromJson(normalizeSchemaContext(schemaContext!, { fold: true })),
238-
hasSchema: true,
239-
}
261+
const def = normalizedSchemaDefinition(schemaContext!, { fold: true })
262+
const names = schemaNames(def)
263+
const foldSql = (other: string) => foldQuotedIdentifierCase(other, names)
264+
return { sql: foldSql(sql), schema: Schema.fromJson(JSON.stringify(def)), hasSchema: true, foldSql }
240265
}
241-
return { sql, schema: EMPTY_SCHEMA(), hasSchema: false }
266+
return { sql, schema: EMPTY_SCHEMA(), hasSchema: false, foldSql: asIs }
242267
}
243268

244-
function loadSchemaFile(schemaPath: string): { schema: Schema; folded: boolean } {
269+
/** `folded` carries the folded definition when the file was normalised here. */
270+
function loadSchemaFile(schemaPath: string): { schema: Schema; folded?: { tables: Record<string, any> } } {
245271
const ext = path.extname(schemaPath).toLowerCase()
246272
if (ext === ".json" || ext === ".yaml" || ext === ".yml") {
247273
const text = fs.readFileSync(schemaPath, "utf8")
248274
const parsed = ext === ".json" ? JSON.parse(text) : YAML.parse(text)
249275
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
250-
return { schema: Schema.fromJson(normalizeSchemaContext(parsed, { fold: true })), folded: true }
276+
const folded = normalizedSchemaDefinition(parsed, { fold: true })
277+
return { schema: Schema.fromJson(JSON.stringify(folded)), folded }
251278
}
252279
}
253-
return { schema: Schema.fromFile(schemaPath), folded: false }
280+
return { schema: Schema.fromFile(schemaPath) }
254281
}
255282

256283
const EMPTY_SCHEMA = () => Schema.fromDdl("CREATE TABLE _empty_ (id INT);")

‎packages/opencode/src/altimate/native/sql/register.ts‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as core from "@altimateai/altimate-core"
1010
import { register } from "../dispatcher"
11-
import { schemaOrEmpty, resolveSchema } from "../schema-resolver"
11+
import { prepareSql } from "../schema-resolver"
1212
import { preprocessIff, postprocessQualify } from "../altimate-core"
1313
import { EngineCoerce } from "../engine-coerce"
1414
import type {
@@ -29,11 +29,11 @@ export function registerAllSql(): void {
2929
// ---------------------------------------------------------------------------
3030
register("sql.analyze", async (params) => {
3131
try {
32-
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
32+
const { sql, schema } = prepareSql(params.sql, params.schema_path, params.schema_context)
3333
const [lintRaw, semanticsRaw, safetyRaw] = await Promise.all([
34-
core.lint(params.sql, schema),
35-
core.checkSemantics(params.sql, schema),
36-
core.scanSql(params.sql),
34+
core.lint(sql, schema),
35+
core.checkSemantics(sql, schema),
36+
core.scanSql(sql),
3737
])
3838

3939
const lint = JSON.parse(JSON.stringify(lintRaw))
@@ -135,8 +135,8 @@ export function registerAllSql(): void {
135135
// ---------------------------------------------------------------------------
136136
register("sql.optimize", async (params) => {
137137
try {
138-
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
139-
const [rewriteRaw, lintRaw] = await Promise.all([core.rewrite(params.sql, schema), core.lint(params.sql, schema)])
138+
const { sql, schema } = prepareSql(params.sql, params.schema_path, params.schema_context)
139+
const [rewriteRaw, lintRaw] = await Promise.all([core.rewrite(sql, schema), core.lint(sql, schema)])
140140

141141
const rewrite = JSON.parse(JSON.stringify(rewriteRaw))
142142
const lint = JSON.parse(JSON.stringify(lintRaw))
@@ -203,8 +203,8 @@ export function registerAllSql(): void {
203203
// ---------------------------------------------------------------------------
204204
register("sql.fix", async (params) => {
205205
try {
206-
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
207-
const raw = await core.fix(params.sql, schema)
206+
const { sql, schema } = prepareSql(params.sql, params.schema_path, params.schema_context)
207+
const raw = await core.fix(sql, schema)
208208
const result = JSON.parse(JSON.stringify(raw))
209209

210210
const suggestions = (result.fixes_applied ?? []).map((f: any) => ({
@@ -356,10 +356,11 @@ export function registerAllSql(): void {
356356
// ---------------------------------------------------------------------------
357357
register("sql.diff", async (params) => {
358358
try {
359-
const schema = params.schema_context ? (resolveSchema(undefined, params.schema_context) ?? undefined) : undefined
359+
const prepared = prepareSql(params.original ?? params.sql_a, undefined, params.schema_context)
360+
const schema = prepared.hasSchema ? prepared.schema : undefined
360361

361-
const sqlA = params.original ?? params.sql_a
362-
const sqlB = params.modified ?? params.sql_b
362+
const sqlA = prepared.sql
363+
const sqlB = prepared.foldSql(params.modified ?? params.sql_b)
363364

364365
// `|| undefined`: coerce a default empty-string dialect to "no hint" — the
365366
// engine throws on an unknown dialect "".
@@ -404,8 +405,8 @@ export function registerAllSql(): void {
404405
// ---------------------------------------------------------------------------
405406
register("sql.rewrite", async (params) => {
406407
try {
407-
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
408-
const raw = core.rewrite(params.sql, schema)
408+
const { sql, schema } = prepareSql(params.sql, params.schema_path, params.schema_context)
409+
const raw = core.rewrite(sql, schema)
409410
const result = JSON.parse(JSON.stringify(raw))
410411
return {
411412
success: true,
@@ -463,8 +464,8 @@ export function registerAllSql(): void {
463464
// ---------------------------------------------------------------------------
464465
register("lineage.check", async (params) => {
465466
try {
466-
const schema = params.schema_context ? (resolveSchema(undefined, params.schema_context) ?? undefined) : undefined
467-
const raw = core.columnLineage(params.sql, EngineCoerce.dialectHint(params.dialect), schema ?? undefined)
467+
const prepared = prepareSql(params.sql, undefined, params.schema_context)
468+
const raw = core.columnLineage(prepared.sql, EngineCoerce.dialectHint(params.dialect), prepared.hasSchema ? prepared.schema : undefined)
468469
const result = JSON.parse(JSON.stringify(raw))
469470
return {
470471
success: true,

0 commit comments

Comments
 (0)