mirror of
https://github.com/lancedb/lancedb.git
synced 2026-05-21 05:50:41 +00:00
merge insert
This commit is contained in:
@@ -451,6 +451,11 @@ export interface Table<T = number[]> {
|
||||
indexStats: (indexUuid: string) => Promise<IndexStats>
|
||||
|
||||
filter(value: string): Query<T>
|
||||
|
||||
/**
|
||||
* TODO comment
|
||||
*/
|
||||
mergeInsert: () => MergeInsertBuilder
|
||||
|
||||
schema: Promise<Schema>
|
||||
}
|
||||
@@ -900,6 +905,15 @@ export class LocalTable<T = number[]> implements Table<T> {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
mergeInsert: () => MergeInsertBuilder = () => {
|
||||
return new MergeInsertBuilder(async (args: {
|
||||
params: MergeInsertParams
|
||||
data: Array<Record<string, unknown>> | ArrowTable
|
||||
}) => {
|
||||
throw new Error('Not implemented')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export interface CleanupStats {
|
||||
@@ -1076,3 +1090,56 @@ export enum MetricType {
|
||||
*/
|
||||
Dot = 'dot',
|
||||
}
|
||||
|
||||
export interface MergeInsertParams {
|
||||
whenMatchedUpdateAll: boolean
|
||||
whenNotMatchedInsertAll: boolean
|
||||
whenNotMatchedBySourceDelete: boolean
|
||||
whenNotMatchedBySourceCondition: boolean
|
||||
}
|
||||
|
||||
type MergeInsertCallback = (args: {
|
||||
params: MergeInsertParams
|
||||
data: Array<Record<string, unknown>> | ArrowTable
|
||||
}) => Promise<void>
|
||||
|
||||
export class MergeInsertBuilder {
|
||||
readonly #callback: MergeInsertCallback
|
||||
readonly #params: MergeInsertParams
|
||||
|
||||
constructor (callback: MergeInsertCallback) {
|
||||
this.#callback = callback
|
||||
this.#params = {
|
||||
whenMatchedUpdateAll: false,
|
||||
whenNotMatchedInsertAll: false,
|
||||
whenNotMatchedBySourceDelete: false,
|
||||
whenNotMatchedBySourceCondition: false
|
||||
}
|
||||
}
|
||||
|
||||
whenMatchedUpdateAll (): MergeInsertBuilder {
|
||||
this.#params.whenMatchedUpdateAll = true
|
||||
return this
|
||||
}
|
||||
|
||||
whenNotMatchedInsertAll (): MergeInsertBuilder {
|
||||
this.#params.whenNotMatchedInsertAll = true
|
||||
return this
|
||||
}
|
||||
|
||||
whenNotMatchedBySourceDelete (): MergeInsertBuilder {
|
||||
this.#params.whenNotMatchedBySourceDelete = true
|
||||
return this
|
||||
}
|
||||
|
||||
whenNotMatchedBySourceCondition (): MergeInsertBuilder {
|
||||
this.#params.whenNotMatchedBySourceCondition = true
|
||||
return this
|
||||
}
|
||||
|
||||
async execute ({ data }: {
|
||||
data: Array<Record<string, unknown>> | ArrowTable
|
||||
}): Promise<void> {
|
||||
await this.#callback({ params: this.#params, data })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ export class HttpLancedbClient {
|
||||
public async post (
|
||||
path: string,
|
||||
data?: any,
|
||||
params?: Record<string, string | number>,
|
||||
params?: Record<string, string | number | boolean>,
|
||||
content?: string | undefined
|
||||
): Promise<AxiosResponse> {
|
||||
const response = await axios.post(
|
||||
|
||||
@@ -24,6 +24,8 @@ import {
|
||||
type IndexStats,
|
||||
type UpdateArgs,
|
||||
type UpdateSqlArgs,
|
||||
type MergeInsertParams,
|
||||
MergeInsertBuilder,
|
||||
makeArrowTable
|
||||
} from '../index'
|
||||
import { Query } from '../query'
|
||||
@@ -424,4 +426,36 @@ export class RemoteTable<T = number[]> implements Table<T> {
|
||||
numUnindexedRows: results.data.num_unindexed_rows
|
||||
}
|
||||
}
|
||||
|
||||
mergeInsert: () => MergeInsertBuilder = () => {
|
||||
return new MergeInsertBuilder(async ({ data, params }: {
|
||||
params: MergeInsertParams
|
||||
data: Array<Record<string, unknown>> | ArrowTable
|
||||
}) => {
|
||||
// TODO -- uncomment this this
|
||||
// let tbl: ArrowTable
|
||||
// if (data instanceof ArrowTable) {
|
||||
// tbl = data
|
||||
// } else {
|
||||
// tbl = makeArrowTable(data, await this.schema)
|
||||
// }
|
||||
const tbl = data as ArrowTable
|
||||
|
||||
const buffer = await fromTableToStreamBuffer(tbl, this._embeddings)
|
||||
|
||||
console.log({ buffer })
|
||||
|
||||
await this._client.post(
|
||||
`/v1/table/${this._name}/merge_insert/`,
|
||||
buffer,
|
||||
{
|
||||
when_matched_update_all: params.whenMatchedUpdateAll,
|
||||
when_not_matched_insert_all: params.whenNotMatchedInsertAll,
|
||||
when_not_matched_by_source_delete: params.whenNotMatchedBySourceDelete,
|
||||
when_not_matched_by_source_condition: params.whenNotMatchedBySourceCondition
|
||||
},
|
||||
'application/vnd.apache.arrow.stream'
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user