Skip to content

Commit d5333c3

Browse files
committed
SDK fixes
1 parent 7f17826 commit d5333c3

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

packages/sdk/src/validation/schema-validator.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ function validateStatement(
220220
code: 'E201',
221221
});
222222
}
223+
} else if (!tableName && referencedTables.size === 0 && schemaMap.size > 0) {
224+
// No FROM clause - check column against all tables in the schema
225+
let found = false;
226+
for (const [, tableSchema] of schemaMap) {
227+
if (
228+
tableSchema.columns.some((c) => c.name.toLowerCase() === colName)
229+
) {
230+
found = true;
231+
break;
232+
}
233+
}
234+
235+
if (!found) {
236+
errors.push({
237+
message: `Unknown column '${colName}'`,
238+
severity,
239+
code: 'E201',
240+
});
241+
}
223242
}
224243
}
225244

packages/sdk/src/validation/validation.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,45 @@ describe('validateWithSchema', () => {
196196
});
197197
});
198198

199+
describe('column validation without FROM clause', () => {
200+
it('should detect unknown columns when no FROM clause is present', () => {
201+
const result = validateWithSchema('SELECT scooby', schema);
202+
expect(result.valid).toBe(false);
203+
const error = result.errors.find((e) => e.code === 'E201');
204+
expect(error).toBeDefined();
205+
expect(error?.message).toContain('scooby');
206+
});
207+
208+
it('should pass known columns even without FROM clause', () => {
209+
const result = validateWithSchema('SELECT id', schema);
210+
expect(result.valid).toBe(true);
211+
});
212+
213+
it('should detect unknown columns with dialect specified', () => {
214+
const clickhouseSchema: Schema = {
215+
tables: [
216+
{
217+
name: 'demo_daily_orders',
218+
columns: [
219+
{ name: 'date', type: 'Date' },
220+
{ name: 'category', type: 'String' },
221+
{ name: 'transactions', type: 'Int64' },
222+
],
223+
},
224+
],
225+
};
226+
const result = validateWithSchema(
227+
'SELECT scooby',
228+
clickhouseSchema,
229+
'clickhouse',
230+
);
231+
expect(result.valid).toBe(false);
232+
const error = result.errors.find((e) => e.code === 'E201');
233+
expect(error).toBeDefined();
234+
expect(error?.message).toContain('scooby');
235+
});
236+
});
237+
199238
describe('strict mode', () => {
200239
it('should report errors in strict mode (default)', () => {
201240
const result = validateWithSchema('SELECT unknown FROM users', schema);

0 commit comments

Comments
 (0)