Skip to content

Commit cf4a6ca

Browse files
sahrizviclaude
andcommitted
fix(validate): fold uppercase metadata to the engine's identifier case; skip existence checks without a schema
`altimate_core_validate` rejected correct Snowflake SQL against real metadata. The engine compares an unquoted identifier in lowercase and a quoted one exactly; `schema_inspect` and `snowflake_get_table_stats` return UPPERCASE names, so every query came back ColumnNotFound — with the engine's own DidYouMean pointing at the same column in uppercase at confidence 1 — and uppercasing the SQL did not help because the engine lowercased it again. The model looped three or four validation rounds per warehouse demo before giving up on the validator (#1333). `normalizeSchemaContext` now folds an all-uppercase table or column name — the storage form of an identifier created unquoted — to lowercase before the schema reaches the engine. Mixed-case names were created quoted, must be referenced quoted, and are matched exactly by the engine, so they are left alone; lowercase names are already in the engine's form. Applies to both schema_context shapes. Without a schema the engine ran against the `_empty_` placeholder and reported every table as missing, while the tool's description promised existence checks are skipped. The handler now drops TableNotFound / ColumnNotFound findings when no schema was given and recomputes `valid` from what remains, so syntax and dialect errors still surface. Tests: the exact query and uppercase metadata from the issue validate (lower and upper SQL); a genuinely missing column is still reported; no schema → valid for a correct query, still invalid for a syntax error; fold and existence-classifier unit cases. Both guards were deleted once to confirm the tests fail. Closes #1333 Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 13cab51 commit cf4a6ca

3 files changed

Lines changed: 163 additions & 4 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ import type { AltimateCoreResult } from "./types"
1919
// ---------------------------------------------------------------------------
2020

2121
/** Spread a rich TypeScript object into a plain Record for the data field. */
22+
// altimate_change start — the engine's existence findings, which mean nothing without a schema
23+
const EXISTENCE_CODES = new Set(["E001", "E002"])
24+
const EXISTENCE_KINDS = new Set(["TableNotFound", "ColumnNotFound"])
25+
export function isExistenceError(err: unknown): boolean {
26+
const e = err as { code?: unknown; kind?: { type?: unknown } } | null
27+
return EXISTENCE_CODES.has(String(e?.code)) || EXISTENCE_KINDS.has(String(e?.kind?.type))
28+
}
29+
// altimate_change end
30+
2231
function toData(obj: unknown): Record<string, unknown> {
2332
if (obj === null || obj === undefined) return {}
2433
if (typeof obj !== "object") return { value: obj }
@@ -89,9 +98,21 @@ export function registerAll(): void {
8998
// 1. altimate_core.validate
9099
register("altimate_core.validate", async (params) => {
91100
try {
101+
const hasSchema = !!(params.schema_path || (params.schema_context && Object.keys(params.schema_context).length > 0))
92102
const schema = schemaOrEmpty(params.schema_path, params.schema_context)
93103
const raw = await core.validate(params.sql, schema)
94104
const data = toData(raw)
105+
// altimate_change start — without a schema the engine still runs against the
106+
// `_empty_` placeholder and reports every table as missing. The tool promises that
107+
// existence checks are skipped when no schema is given, so those findings are
108+
// dropped here and `valid` is recomputed from what remains (syntax, dialect).
109+
const errors = (data as { errors?: unknown }).errors
110+
if (!hasSchema && Array.isArray(errors)) {
111+
const kept = errors.filter((err) => !isExistenceError(err))
112+
;(data as Record<string, unknown>).errors = kept
113+
;(data as Record<string, unknown>).valid = kept.length === 0
114+
}
115+
// altimate_change end
95116
return ok(true, data)
96117
} catch (e) {
97118
return fail(e)

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,40 @@ function flatToSchemaDefinition(flat: Record<string, any>): Record<string, any>
8080
return { tables }
8181
}
8282

83+
/**
84+
* The engine compares an UNQUOTED identifier from the SQL in lowercase and a QUOTED one
85+
* exactly, while warehouse metadata (`schema_inspect`, `snowflake_get_table_stats`) comes
86+
* back in the warehouse's storage case — uppercase on Snowflake. A correct query validated
87+
* against real metadata therefore failed with ColumnNotFound, and uppercasing the SQL did
88+
* not help because the engine lowercased it again (#1333).
89+
*
90+
* An all-uppercase name is the storage form of an identifier that was created unquoted, so
91+
* it is stored lowercase here to meet the engine's unquoted comparison. A mixed-case name
92+
* was created quoted and must be referenced quoted, which the engine matches exactly, so it
93+
* is left alone. Lowercase names are already in the engine's form.
94+
*/
95+
export function foldIdentifierCase(name: string): string {
96+
return name !== name.toLowerCase() && name === name.toUpperCase() ? name.toLowerCase() : name
97+
}
98+
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 ?? {})) {
102+
const columns = Array.isArray(table?.columns)
103+
? table.columns.map((c: any) => (typeof c?.name === "string" ? { ...c, name: foldIdentifierCase(c.name) } : c))
104+
: table?.columns
105+
tables[foldIdentifierCase(tableName)] = { ...table, columns }
106+
}
107+
return { ...def, tables }
108+
}
109+
83110
/**
84111
* Normalize a schema_context into SchemaDefinition JSON format.
85112
* Accepts both flat and SchemaDefinition formats.
86113
*/
87114
export function normalizeSchemaContext(ctx: Record<string, any>): string {
88-
if (isSchemaDefinitionFormat(ctx)) {
89-
return JSON.stringify(ctx)
90-
}
91-
return JSON.stringify(flatToSchemaDefinition(ctx))
115+
const def = (isSchemaDefinitionFormat(ctx) ? ctx : flatToSchemaDefinition(ctx)) as { tables: Record<string, any> }
116+
return JSON.stringify(foldSchemaCase(def))
92117
}
93118

94119
/**
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Regression for #1333: `altimate_core_validate` against warehouse metadata.
2+
//
3+
// The engine compares an UNQUOTED identifier in lowercase and a QUOTED one exactly;
4+
// Snowflake metadata comes back UPPERCASE. A correct query therefore failed with
5+
// ColumnNotFound (the engine's own DidYouMean pointed at the same column in uppercase,
6+
// confidence 1), and with no schema at all the tool reported TableNotFound although its
7+
// description promises existence checks are skipped.
8+
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"
11+
12+
const SQL = `select customer_region, sum(net_revenue) as net_revenue
13+
from TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION
14+
where order_month >= '1997-01-01' group by 1`
15+
16+
// Exactly what `schema_inspect` / `snowflake_get_table_stats` return on Snowflake.
17+
const UPPER = {
18+
"TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION": {
19+
columns: [
20+
{ name: "ORDER_MONTH", type: "DATE" },
21+
{ name: "CUSTOMER_REGION", type: "VARCHAR" },
22+
{ name: "NET_REVENUE", type: "NUMBER" },
23+
],
24+
},
25+
}
26+
27+
describe("foldIdentifierCase", () => {
28+
test("an all-uppercase name (created unquoted) folds to the engine's lowercase form", () => {
29+
expect(foldIdentifierCase("ORDER_MONTH")).toBe("order_month")
30+
expect(foldIdentifierCase("TPCH_ANALYTICS.PUBLIC_REPORTING.RPT_MONTHLY_SALES_BY_REGION")).toBe(
31+
"tpch_analytics.public_reporting.rpt_monthly_sales_by_region",
32+
)
33+
})
34+
35+
test("a mixed-case name (created quoted, referenced quoted) is kept exact; lowercase is untouched", () => {
36+
expect(foldIdentifierCase("Customer_Region")).toBe("Customer_Region")
37+
expect(foldIdentifierCase("customer_region")).toBe("customer_region")
38+
expect(foldIdentifierCase("ORDER_2024")).toBe("order_2024") // digits do not make it mixed-case
39+
expect(foldIdentifierCase("")).toBe("")
40+
})
41+
42+
test("normalizeSchemaContext folds table keys and column names in both input shapes", () => {
43+
const fromDefinition = JSON.parse(normalizeSchemaContext(UPPER))
44+
expect(Object.keys(fromDefinition.tables)).toEqual(["tpch_analytics.public_reporting.rpt_monthly_sales_by_region"])
45+
expect(fromDefinition.tables["tpch_analytics.public_reporting.rpt_monthly_sales_by_region"].columns.map((c: any) => c.name)).toEqual([
46+
"order_month",
47+
"customer_region",
48+
"net_revenue",
49+
])
50+
const fromFlat = JSON.parse(normalizeSchemaContext({ ORDERS: { ORDER_ID: "NUMBER", "Mixed_Col": "VARCHAR" } }))
51+
expect(fromFlat.tables.orders.columns.map((c: any) => c.name)).toEqual(["order_id", "Mixed_Col"])
52+
})
53+
})
54+
55+
describe("isExistenceError", () => {
56+
test("recognises the engine's table/column-not-found by code or kind, and nothing else", () => {
57+
expect(isExistenceError({ code: "E001", kind: { type: "TableNotFound" } })).toBe(true)
58+
expect(isExistenceError({ code: "E002", kind: { type: "ColumnNotFound" } })).toBe(true)
59+
expect(isExistenceError({ kind: { type: "ColumnNotFound" } })).toBe(true)
60+
expect(isExistenceError({ code: "E003", kind: { type: "SyntaxError" } })).toBe(false)
61+
expect(isExistenceError(null)).toBe(false)
62+
expect(isExistenceError("E001")).toBe(false)
63+
})
64+
})
65+
66+
const hasCore = await import("@altimateai/altimate-core").then(() => true).catch(() => false)
67+
const describeIf = hasCore ? describe : describe.skip
68+
69+
describeIf("altimate_core.validate through the dispatcher (#1333)", () => {
70+
let D: any
71+
beforeAll(async () => {
72+
process.env.ALTIMATE_TELEMETRY_DISABLED = "true"
73+
D = await import("../../src/altimate/native/dispatcher")
74+
const core = await import("../../src/altimate/native/altimate-core")
75+
core.registerAll()
76+
})
77+
afterAll(() => {
78+
delete process.env.ALTIMATE_TELEMETRY_DISABLED
79+
})
80+
81+
test("a lowercase Snowflake query validates against UPPERCASE metadata", async () => {
82+
const r = await D.call("altimate_core.validate", { sql: SQL, schema_context: UPPER })
83+
expect(r.data.valid).toBe(true)
84+
expect(r.data.errors).toEqual([])
85+
})
86+
87+
test("the same query in uppercase validates too", async () => {
88+
const r = await D.call("altimate_core.validate", { sql: SQL.toUpperCase(), schema_context: UPPER })
89+
expect(r.data.valid).toBe(true)
90+
})
91+
92+
test("a genuinely missing column is still reported against uppercase metadata", async () => {
93+
const r = await D.call("altimate_core.validate", {
94+
sql: SQL.replace("customer_region", "customer_reggion"),
95+
schema_context: UPPER,
96+
})
97+
expect(r.data.valid).toBe(false)
98+
expect(r.data.errors.some((e: any) => e.kind?.type === "ColumnNotFound")).toBe(true)
99+
})
100+
101+
test("with no schema, existence findings are dropped and a correct query is valid", async () => {
102+
const r = await D.call("altimate_core.validate", { sql: SQL, schema_path: "", schema_context: {} })
103+
expect(r.data.valid).toBe(true)
104+
expect(r.data.errors).toEqual([])
105+
})
106+
107+
test("with no schema, a syntax error is still reported", async () => {
108+
const r = await D.call("altimate_core.validate", { sql: "selec x fro t", schema_context: {} })
109+
expect(r.data.valid).toBe(false)
110+
expect(r.data.errors.length).toBeGreaterThan(0)
111+
expect(r.data.errors.every((e: any) => !["TableNotFound", "ColumnNotFound"].includes(e.kind?.type))).toBe(true)
112+
})
113+
})

0 commit comments

Comments
 (0)