forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add vector data type, operator experessions
- Loading branch information
1 parent
21529bb
commit 2783479
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ColumnBaseConfig } from "~/column"; | ||
import { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from "~/column-builder"; | ||
import { entityKind } from "~/entity"; | ||
import { SingleStoreColumn, SingleStoreColumnBuilder } from "./common"; | ||
import { AnySingleStoreTable } from "~/singlestore-core/table"; | ||
|
||
export type SingleStoreVectorBuilderInitial<TName extends string> = SingleStoreVectorBuilder<{ | ||
name: TName; | ||
dataType: "array"; | ||
columnType: "SingleStoreVector"; | ||
data: Array<number>; | ||
driverParam: Array<number>; | ||
enumValues: undefined; | ||
generated: undefined; | ||
}> | ||
|
||
export class SingleStoreVectorBuilder<T extends ColumnBuilderBaseConfig<"array", "SingleStoreVector">> extends SingleStoreColumnBuilder<T, SingleStoreVectorConfig> { | ||
static readonly [entityKind]: string = "SingleStoreVectorBuilder" | ||
|
||
constructor(name: T["name"], config: SingleStoreVectorConfig) { | ||
super(name, "array", "SingleStoreVector") | ||
this.config.dimensions = config.dimensions | ||
this.config.elementType = config.elementType | ||
} | ||
|
||
/** @internal */ | ||
override build<TTableName extends string>(table: AnySingleStoreTable<{name: TTableName}>): SingleStoreVector<MakeColumnConfig<T, TTableName>> { | ||
return new SingleStoreVector(table, this.config as ColumnBuilderRuntimeConfig<any, any>); | ||
} | ||
} | ||
|
||
export class SingleStoreVector<T extends ColumnBaseConfig<"array", "SingleStoreVector">> extends SingleStoreColumn<T> { | ||
static readonly [entityKind]: string = "SingleStoreVector" | ||
|
||
readonly dimensions: number; | ||
readonly elementType: ElementType | undefined; | ||
|
||
constructor(table: AnySingleStoreTable<{name: T["tableName"]}>, config: SingleStoreVectorBuilder<T>["config"]) { | ||
super(table, config) | ||
this.dimensions = config.dimensions | ||
this.elementType = config.elementType | ||
} | ||
|
||
getSQLType(): string { | ||
const et = this.elementType === undefined ? "" : `, ${this.elementType}`; | ||
return `vector(${this.dimensions}${et})` | ||
} | ||
|
||
override mapToDriverValue(value: Array<number>) { | ||
return JSON.stringify(value) | ||
} | ||
|
||
override mapFromDriverValue(value: string): Array<number> { | ||
return JSON.parse(value) | ||
} | ||
} | ||
|
||
type ElementType = "I8" | "I16" | "I32" | "I64" | "F32" | "F64"; | ||
|
||
export interface SingleStoreVectorConfig { | ||
dimensions: number; | ||
elementType: ElementType; | ||
} | ||
|
||
export function vector<TName extends string>(name: TName, config: SingleStoreVectorConfig): SingleStoreVectorBuilderInitial<TName> { | ||
return new SingleStoreVectorBuilder(name, config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters