Skip to content

Commit

Permalink
lint fix, fix test/introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchwadair committed Dec 17, 2024
1 parent 286e96f commit e8553c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion drizzle-kit/src/introspect-singlestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ const column = (
const [dimensions, elementType] = lowered.substring('vector'.length + 1, lowered.length - 1).split(',');
let out = `${casing(name)}: vector(${
dbColumnName({ name, casing: rawCasing, withMode: true })
}{ dimensions: ${dimensions}${elementType ? `, elementType: ${elementType}` : ''} })`;
}{ dimensions: ${dimensions}, elementType: ${elementType} })`;

out += defaultValue ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return out;
Expand Down
3 changes: 1 addition & 2 deletions drizzle-orm/src/singlestore-core/columns/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export class SingleStoreVector<T extends ColumnBaseConfig<'array', 'SingleStoreV
}

getSQLType(): string {
const et = this.elementType === undefined ? '' : `, ${this.elementType}`;
return `vector(${this.dimensions}${et})`;
return `vector(${this.dimensions}, ${this.elementType || 'F32'})`;
}

override mapToDriverValue(value: Array<number>) {
Expand Down
54 changes: 37 additions & 17 deletions integration-tests/tests/singlestore/singlestore-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
vector,
year,
} from 'drizzle-orm/singlestore-core';
import { euclideanDistance, dotProduct } from 'drizzle-orm/singlestore-core/expressions';
import { dotProduct, euclideanDistance } from 'drizzle-orm/singlestore-core/expressions';
import { migrate } from 'drizzle-orm/singlestore/migrator';
import getPort from 'get-port';
import { v4 as uuid } from 'uuid';
Expand Down Expand Up @@ -384,11 +384,19 @@ export function tests(driver?: string) {
\`embedding\` vector(10) not null
)
`,
)
);
await db.insert(vectorSearchTable).values([
{ id: 1, text: "I like dogs", embedding: [0.6119,0.1395,0.2921,0.3664,0.4561,0.7852,0.1997,0.5142,0.5924,0.0465] },
{ id: 2, text: "I like cats", embedding: [0.6075,0.1705,0.0651,0.9489,0.9656,0.8084,0.3046,0.0977,0.6842,0.4402] }
])
{
id: 1,
text: 'I like dogs',
embedding: [0.6119, 0.1395, 0.2921, 0.3664, 0.4561, 0.7852, 0.1997, 0.5142, 0.5924, 0.0465],
},
{
id: 2,
text: 'I like cats',
embedding: [0.6075, 0.1705, 0.0651, 0.9489, 0.9656, 0.8084, 0.3046, 0.0977, 0.6842, 0.4402],
},
]);
}

test('table config: unsigned ints', async () => {
Expand Down Expand Up @@ -2935,20 +2943,32 @@ export function tests(driver?: string) {
test('simple vector search', async (ctx) => {
const { db } = ctx.singlestore;
const table = vectorSearchTable;
const embedding = [0.42,0.93,0.88,0.57,0.32,0.64,0.76,0.52,0.19,0.81]; // ChatGPT's 10 dimension embedding for "dogs are cool"
const embedding = [0.42, 0.93, 0.88, 0.57, 0.32, 0.64, 0.76, 0.52, 0.19, 0.81]; // ChatGPT's 10 dimension embedding for "dogs are cool" not sure how accurate but it works
await setupVectorSearchTest(db);

const withRankEuclidean = db.select({ id: table.id, text: table.text, rank: sql`row_number() over (order by ${euclideanDistance(table.embedding, embedding)})`.as('rank') }).from(table).as('with_rank')
const withRankDotProduct = db.select({ id: table.id, text: table.text, rank: sql`row_number() over (order by ${dotProduct(table.embedding, embedding)})`.as('rank') }).from(table).as('with_rank')
const result1 = await db.select({ id: withRankEuclidean.id, text: withRankEuclidean.text }).from(withRankEuclidean).where(eq(withRankEuclidean.rank, 1));
const result2 = await db.select({ id: withRankDotProduct.id, text: withRankDotProduct.text }).from(withRankDotProduct).where(eq(withRankDotProduct.rank, 1));

expect(result1.length).toEqual(1)
expect(result1[0]).toEqual({ id: 1, text: "I like dogs" });

expect(result2.length).toEqual(1)
expect(result2[0]).toEqual({ id: 1, text: "I like dogs" });
})
const withRankEuclidean = db.select({
id: table.id,
text: table.text,
rank: sql`row_number() over (order by ${euclideanDistance(table.embedding, embedding)})`.as('rank'),
}).from(table).as('with_rank');
const withRankDotProduct = db.select({
id: table.id,
text: table.text,
rank: sql`row_number() over (order by ${dotProduct(table.embedding, embedding)})`.as('rank'),
}).from(table).as('with_rank');
const result1 = await db.select({ id: withRankEuclidean.id, text: withRankEuclidean.text }).from(
withRankEuclidean,
).where(eq(withRankEuclidean.rank, 1));
const result2 = await db.select({ id: withRankDotProduct.id, text: withRankDotProduct.text }).from(
withRankDotProduct,
).where(eq(withRankDotProduct.rank, 1));

expect(result1.length).toEqual(1);
expect(result1[0]).toEqual({ id: 1, text: 'I like dogs' });

expect(result2.length).toEqual(1);
expect(result2[0]).toEqual({ id: 1, text: 'I like dogs' });
});

test('test $onUpdateFn and $onUpdate works as $default', async (ctx) => {
const { db } = ctx.singlestore;
Expand Down

0 comments on commit e8553c7

Please sign in to comment.