Skip to content

Commit f288654

Browse files
sahrizviclaude
andcommitted
fix(validate): quoted uppercase references, fold collisions, table-less contexts
Bot review on #1343, checked against the engine: it lowercases table keys at load, keeps column names as written, lowercases an unquoted reference and matches a quoted one exactly, and dedups columns case-insensitively (first wins) — so both spellings of a name cannot be emitted. - `foldQuotedIdentifierCase`: a quoted all-uppercase identifier in the SQL (`"ORDER_MONTH"`, dbt `quote_columns` / BI style on Snowflake) is lowercased inside its quotes when a schema is present, so it matches the folded metadata; same length, still quoted, string literals and comments stepped over, mixed-case left exact - A fold that would collide with an entry the schema already carries (`ORDERS` + `orders`, `FOO` + `foo` columns) keeps the name as written instead of overwriting; null-prototype maps so `__PROTO__` is an entry - `schemaProvided`: a `schema_context` counts only if it normalises to a table. `{ tables: {} }` / `{ users: {} }` used to fail the call outright ("Schema must define at least one table"); they now take the no-schema path - Test: engine-dependent modules imported lazily behind a `require.resolve` guard so the skip really skips; prior `ALTIMATE_TELEMETRY_DISABLED` restored; dispatcher reset after the suite; cases for each of the above Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent cf4a6ca commit f288654

3 files changed

Lines changed: 165 additions & 20 deletions

File tree

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

Lines changed: 3 additions & 3 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, resolveSchema, SchemaResolver } from "./schema-resolver"
14+
import { schemaOrEmpty, resolveSchema, SchemaResolver, schemaProvided, foldQuotedIdentifierCase } from "./schema-resolver"
1515
import type { AltimateCoreResult } from "./types"
1616

1717
// ---------------------------------------------------------------------------
@@ -98,9 +98,9 @@ export function registerAll(): void {
9898
// 1. altimate_core.validate
9999
register("altimate_core.validate", async (params) => {
100100
try {
101-
const hasSchema = !!(params.schema_path || (params.schema_context && Object.keys(params.schema_context).length > 0))
101+
const hasSchema = schemaProvided(params.schema_path, params.schema_context)
102102
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
103-
const raw = await core.validate(params.sql, schema)
103+
const raw = await core.validate(hasSchema ? foldQuotedIdentifierCase(params.sql) : params.sql, schema)
104104
const data = toData(raw)
105105
// altimate_change start — without a schema the engine still runs against the
106106
// `_empty_` placeholder and reports every table as missing. The tool promises that

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

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function isSchemaDefinitionFormat(ctx: Record<string, any>): boolean {
4848
* Output: `{ "tables": { "customers": { "columns": [{ "name": "id", "type": "INTEGER" }, ...] } } }`
4949
*/
5050
function flatToSchemaDefinition(flat: Record<string, any>): Record<string, any> {
51-
const tables: Record<string, any> = {}
51+
const tables: Record<string, any> = Object.create(null)
5252
for (const [tableName, colsOrDef] of Object.entries(flat)) {
5353
if (colsOrDef === null || colsOrDef === undefined) continue
5454

@@ -96,24 +96,79 @@ export function foldIdentifierCase(name: string): string {
9696
return name !== name.toLowerCase() && name === name.toUpperCase() ? name.toLowerCase() : name
9797
}
9898

99-
function foldSchemaCase(def: { tables: Record<string, any> }): { tables: Record<string, any> } {
100-
const tables: Record<string, any> = {}
101-
for (const [tableName, table] of Object.entries(def.tables ?? {})) {
99+
/**
100+
* Fold every table key and column name. A fold that would land on a name the schema
101+
* already carries (metadata with both `ORDERS` and `orders`, or `FOO` and `"foo"` columns)
102+
* keeps the entry as written instead: the two are distinct objects in the warehouse, the
103+
* schema shape has no quote identity to tell them apart, and overwriting one would drop
104+
* its columns from validation. The engine resolves such a pair by its own rule (tables by
105+
* lowercase key, columns first-wins case-insensitively), which is what it did before the
106+
* fold existed. Null-prototype maps, so a table named `__PROTO__` is an entry, not a
107+
* prototype write.
108+
*/
109+
export function foldSchemaCase(def: { tables: Record<string, any> }): { tables: Record<string, any> } {
110+
const source: Record<string, any> = def.tables ?? {}
111+
const tables: Record<string, any> = Object.create(null)
112+
const target = (name: string) => {
113+
const folded = foldIdentifierCase(name)
114+
return folded !== name && Object.hasOwn(source, folded) ? name : folded
115+
}
116+
for (const [tableName, table] of Object.entries(source)) {
117+
const present = new Set<string>(
118+
Array.isArray(table?.columns) ? table.columns.map((c: any) => c?.name).filter((n: unknown) => typeof n === "string") : [],
119+
)
102120
const columns = Array.isArray(table?.columns)
103-
? table.columns.map((c: any) => (typeof c?.name === "string" ? { ...c, name: foldIdentifierCase(c.name) } : c))
121+
? table.columns.map((c: any) => {
122+
if (typeof c?.name !== "string") return c
123+
const folded = foldIdentifierCase(c.name)
124+
return { ...c, name: folded !== c.name && present.has(folded) ? c.name : folded }
125+
})
104126
: table?.columns
105-
tables[foldIdentifierCase(tableName)] = { ...table, columns }
127+
tables[target(tableName)] = { ...table, columns }
106128
}
107129
return { ...def, tables }
108130
}
109131

132+
/**
133+
* The other half of the fold, on the SQL. The engine matches a quoted identifier exactly,
134+
* so once metadata `ORDER_MONTH` is stored as `order_month` a query that writes
135+
* `"ORDER_MONTH"` — dbt with `quote_columns`, most BI tools on Snowflake — would miss it.
136+
* On the warehouses whose metadata comes back uppercase, `"ORDER_MONTH"` names the same
137+
* column as `order_month` unquoted, so the quoted all-uppercase spelling is lowercased
138+
* inside its quotes. Same length, so positions in findings do not move; still quoted, so a
139+
* reserved word (`"ORDER"`) stays an identifier. String literals and comments are stepped
140+
* over, not searched. Mixed-case quoted names are the exact-match case and are untouched.
141+
*/
142+
export function foldQuotedIdentifierCase(sql: string): string {
143+
return sql.replace(/'(?:[^']|'')*'|--[^\n]*|\/\*[\s\S]*?\*\/|"([^"]*)"/g, (match, quoted?: string) => {
144+
if (quoted === undefined) return match
145+
return /^[A-Z_][A-Z0-9_$]*$/.test(quoted) ? `"${quoted.toLowerCase()}"` : match
146+
})
147+
}
148+
110149
/**
111150
* Normalize a schema_context into SchemaDefinition JSON format.
112151
* Accepts both flat and SchemaDefinition formats.
113152
*/
114153
export function normalizeSchemaContext(ctx: Record<string, any>): string {
154+
return JSON.stringify(normalizedSchemaDefinition(ctx))
155+
}
156+
157+
function normalizedSchemaDefinition(ctx: Record<string, any>): { tables: Record<string, any> } {
115158
const def = (isSchemaDefinitionFormat(ctx) ? ctx : flatToSchemaDefinition(ctx)) as { tables: Record<string, any> }
116-
return JSON.stringify(foldSchemaCase(def))
159+
return foldSchemaCase(def)
160+
}
161+
162+
/**
163+
* Whether the caller really supplied a schema to check existence against. A `schema_path`
164+
* counts as one. A `schema_context` counts only if it normalises to at least one table:
165+
* `{ tables: {} }` and `{ users: {} }` are reachable inputs that carry no table; the
166+
* engine refuses them, and the no-schema path is what the caller meant.
167+
*/
168+
export function schemaProvided(schemaPath?: string, schemaContext?: Record<string, any>): boolean {
169+
if (schemaPath) return true
170+
if (!schemaContext || Object.keys(schemaContext).length === 0) return false
171+
return Object.keys(normalizedSchemaDefinition(schemaContext).tables).length > 0
117172
}
118173

119174
/**
@@ -127,8 +182,11 @@ export function resolveSchema(
127182
if (schemaPath) {
128183
return Schema.fromFile(schemaPath)
129184
}
130-
if (schemaContext && Object.keys(schemaContext).length > 0) {
131-
return Schema.fromJson(normalizeSchemaContext(schemaContext))
185+
// A context that normalises to no table is no schema: the engine refuses an empty
186+
// definition outright ("Schema must define at least one table"), so `{ tables: {} }`
187+
// used to fail the call instead of running the no-schema path.
188+
if (schemaProvided(undefined, schemaContext)) {
189+
return Schema.fromJson(normalizeSchemaContext(schemaContext!))
132190
}
133191
return null
134192
}

‎packages/opencode/test/altimate/validate-identifier-case.test.ts‎

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
// confidence 1), and with no schema at all the tool reported TableNotFound although its
77
// description promises existence checks are skipped.
88
import { afterAll, beforeAll, describe, expect, test } from "bun:test"
9-
import { foldIdentifierCase, normalizeSchemaContext } from "../../src/altimate/native/schema-resolver"
10-
import { isExistenceError } from "../../src/altimate/native/altimate-core"
9+
10+
// Both source modules import `@altimateai/altimate-core` statically, so they are loaded
11+
// lazily: a static import here would fail the whole file where the napi binding cannot
12+
// load, and the skip guard below would never run. (bot review)
13+
const hasCore = (() => {
14+
try {
15+
require.resolve("@altimateai/altimate-core")
16+
return true
17+
} catch {
18+
return false
19+
}
20+
})()
21+
const describeIf = hasCore ? describe : describe.skip
1122

1223
const SQL = `select customer_region, sum(net_revenue) as net_revenue
1324
from TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION
@@ -24,7 +35,17 @@ const UPPER = {
2435
},
2536
}
2637

27-
describe("foldIdentifierCase", () => {
38+
describeIf("foldIdentifierCase", () => {
39+
let foldIdentifierCase: typeof import("../../src/altimate/native/schema-resolver").foldIdentifierCase
40+
let foldQuotedIdentifierCase: typeof import("../../src/altimate/native/schema-resolver").foldQuotedIdentifierCase
41+
let normalizeSchemaContext: typeof import("../../src/altimate/native/schema-resolver").normalizeSchemaContext
42+
let schemaProvided: typeof import("../../src/altimate/native/schema-resolver").schemaProvided
43+
beforeAll(async () => {
44+
;({ foldIdentifierCase, foldQuotedIdentifierCase, normalizeSchemaContext, schemaProvided } = await import(
45+
"../../src/altimate/native/schema-resolver"
46+
))
47+
})
48+
2849
test("an all-uppercase name (created unquoted) folds to the engine's lowercase form", () => {
2950
expect(foldIdentifierCase("ORDER_MONTH")).toBe("order_month")
3051
expect(foldIdentifierCase("TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION")).toBe(
@@ -50,9 +71,52 @@ describe("foldIdentifierCase", () => {
5071
const fromFlat = JSON.parse(normalizeSchemaContext({ ORDERS: { ORDER_ID: "NUMBER", "Mixed_Col": "VARCHAR" } }))
5172
expect(fromFlat.tables.orders.columns.map((c: any) => c.name)).toEqual(["order_id", "Mixed_Col"])
5273
})
74+
75+
test("a fold that would collide with an entry the schema already has keeps the name as written", () => {
76+
// Metadata with both `ORDERS` and `orders`: distinct warehouse objects, and folding one
77+
// onto the other would drop its columns. Both survive, the uppercase one unfolded.
78+
const tables = JSON.parse(
79+
normalizeSchemaContext({ tables: { ORDERS: { columns: [{ name: "A", type: "INT" }] }, orders: { columns: [{ name: "b", type: "INT" }] } } }),
80+
).tables
81+
expect(Object.keys(tables).sort()).toEqual(["ORDERS", "orders"])
82+
expect(tables.ORDERS.columns[0].name).toBe("a")
83+
// Same rule inside a table: `FOO` and `foo` columns both stay.
84+
const cols = JSON.parse(
85+
normalizeSchemaContext({ t: { columns: [{ name: "FOO", type: "INT" }, { name: "foo", type: "INT" }, { name: "BAR", type: "INT" }] } }),
86+
).tables.t.columns.map((c: any) => c.name)
87+
expect(cols).toEqual(["FOO", "foo", "bar"])
88+
})
89+
90+
test("a table named __PROTO__ is an entry in the folded schema, not a prototype write", () => {
91+
const def = JSON.parse(normalizeSchemaContext({ __PROTO__: { ID: "INT" } }))
92+
expect(Object.keys(def.tables)).toEqual(["__proto__"])
93+
expect(def.tables.__proto__.columns).toEqual([{ name: "id", type: "INT" }])
94+
})
95+
96+
test("foldQuotedIdentifierCase lowercases quoted all-uppercase identifiers and nothing else", () => {
97+
const sql = `select "ORDER_MONTH", "Mixed_Col", "ORDER", 'lit "KEEP"', x -- "NOPE"\n, /* "NOT" */ "A1$" from "ORDERS" where n = 'it''s "OK"'`
98+
expect(foldQuotedIdentifierCase(sql)).toBe(
99+
`select "order_month", "Mixed_Col", "order", 'lit "KEEP"', x -- "NOPE"\n, /* "NOT" */ "a1$" from "orders" where n = 'it''s "OK"'`,
100+
)
101+
expect(foldQuotedIdentifierCase(sql)).toHaveLength(sql.length)
102+
})
103+
104+
test("schemaProvided counts a schema_path, and a schema_context only when it normalises to a table", () => {
105+
expect(schemaProvided("/some/schema.json")).toBe(true)
106+
expect(schemaProvided(undefined, UPPER)).toBe(true)
107+
expect(schemaProvided(undefined, {})).toBe(false)
108+
expect(schemaProvided(undefined, { tables: {} })).toBe(false)
109+
expect(schemaProvided(undefined, { users: {} })).toBe(false)
110+
expect(schemaProvided("", undefined)).toBe(false)
111+
})
53112
})
54113

55-
describe("isExistenceError", () => {
114+
describeIf("isExistenceError", () => {
115+
let isExistenceError: typeof import("../../src/altimate/native/altimate-core").isExistenceError
116+
beforeAll(async () => {
117+
;({ isExistenceError } = await import("../../src/altimate/native/altimate-core"))
118+
})
119+
56120
test("recognises the engine's table/column-not-found by code or kind, and nothing else", () => {
57121
expect(isExistenceError({ code: "E001", kind: { type: "TableNotFound" } })).toBe(true)
58122
expect(isExistenceError({ code: "E002", kind: { type: "ColumnNotFound" } })).toBe(true)
@@ -63,19 +127,20 @@ describe("isExistenceError", () => {
63127
})
64128
})
65129

66-
const hasCore = await import("@altimateai/altimate-core").then(() => true).catch(() => false)
67-
const describeIf = hasCore ? describe : describe.skip
68-
69130
describeIf("altimate_core.validate through the dispatcher (#1333)", () => {
70131
let D: any
132+
const telemetry = process.env.ALTIMATE_TELEMETRY_DISABLED
71133
beforeAll(async () => {
72134
process.env.ALTIMATE_TELEMETRY_DISABLED = "true"
73135
D = await import("../../src/altimate/native/dispatcher")
74136
const core = await import("../../src/altimate/native/altimate-core")
75137
core.registerAll()
76138
})
77139
afterAll(() => {
78-
delete process.env.ALTIMATE_TELEMETRY_DISABLED
140+
if (telemetry === undefined) delete process.env.ALTIMATE_TELEMETRY_DISABLED
141+
else process.env.ALTIMATE_TELEMETRY_DISABLED = telemetry
142+
// The handlers went into the process-global dispatcher; leave it as found.
143+
D.reset()
79144
})
80145

81146
test("a lowercase Snowflake query validates against UPPERCASE metadata", async () => {
@@ -98,6 +163,28 @@ describeIf("altimate_core.validate through the dispatcher (#1333)", () => {
98163
expect(r.data.errors.some((e: any) => e.kind?.type === "ColumnNotFound")).toBe(true)
99164
})
100165

166+
test("a quoted all-uppercase reference — dbt quote_columns style — validates against uppercase metadata", async () => {
167+
const quoted = `select "CUSTOMER_REGION", sum("NET_REVENUE") as net_revenue
168+
from "TPCH_ANALYTICS"."PUBLIC_REPORTING"."RPT_MONTHLY_SALES_BY_REGION" where "ORDER_MONTH" >= '1997-01-01' group by 1`
169+
const r = await D.call("altimate_core.validate", { sql: quoted, schema_context: UPPER })
170+
expect(r.data.errors).toEqual([])
171+
expect(r.data.valid).toBe(true)
172+
// A quoted mixed-case name is the exact-match case and still has to be exact.
173+
const mixed = await D.call("altimate_core.validate", {
174+
sql: `select "Customer_Region" from TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION`,
175+
schema_context: UPPER,
176+
})
177+
expect(mixed.data.valid).toBe(false)
178+
})
179+
180+
test("a schema_context with no tables is treated as no schema", async () => {
181+
for (const schema_context of [{ tables: {} }, { users: {} }]) {
182+
const r = await D.call("altimate_core.validate", { sql: SQL, schema_context })
183+
expect(r.data.errors).toEqual([])
184+
expect(r.data.valid).toBe(true)
185+
}
186+
})
187+
101188
test("with no schema, existence findings are dropped and a correct query is valid", async () => {
102189
const r = await D.call("altimate_core.validate", { sql: SQL, schema_path: "", schema_context: {} })
103190
expect(r.data.valid).toBe(true)

0 commit comments

Comments
 (0)