fix(datastore): SQL now emits ARRAY_CONTAINS OR CONTAINS for array+string support

This commit is contained in:
saravanakumardb1 2026-03-02 01:50:09 -08:00
parent fc1fef9c70
commit 6fe41de481
2 changed files with 12 additions and 5 deletions

View File

@ -266,6 +266,13 @@ describe('filterToCosmosSQL', () => {
expect(result.parameters).toHaveLength(2);
});
it('$contains emits ARRAY_CONTAINS OR CONTAINS', () => {
const result = filterToCosmosSQL({ tags: { $contains: 'urgent' } });
expect(result.query).toContain('ARRAY_CONTAINS(c.tags, @p0)');
expect(result.query).toContain('CONTAINS(c.tags, @p0)');
expect(result.parameters).toEqual([{ name: '@p0', value: 'urgent' }]);
});
it('$or', () => {
const result = filterToCosmosSQL({ $or: [{ a: 1 }, { b: 2 }] });
expect(result.query).toContain('OR');

View File

@ -139,11 +139,11 @@ export function filterToCosmosSQL(filter: FilterMap): SqlQuery {
}
if (op.$contains !== undefined) {
// Could be array or string — use CONTAINS for string, ARRAY_CONTAINS for array
// In Cosmos SQL, we use ARRAY_CONTAINS for arrays and CONTAINS for strings
// Since we can't know the type at SQL-gen time, use CONTAINS (works for strings)
// Callers dealing with arrays should use ARRAY_CONTAINS via rawQuery
parts.push(`CONTAINS(c.${key}, ${nextParam(op.$contains)})`);
// Could be array or string — emit both ARRAY_CONTAINS and CONTAINS.
// Cosmos returns false (not error) when the function doesn't match the field type,
// so OR-ing them handles both array membership and string substring correctly.
const param = nextParam(op.$contains);
parts.push(`(ARRAY_CONTAINS(c.${key}, ${param}) OR CONTAINS(c.${key}, ${param}))`);
}
if (op.$in !== undefined) {