From b32b69c993839bd56995bf98704aafa919899c12 Mon Sep 17 00:00:00 2001 From: QianZhu Date: Thu, 29 Feb 2024 13:32:01 -0800 Subject: [PATCH] Add create scalar index to sdk (#1033) --- node/src/index.ts | 8 +++++-- node/src/remote/index.ts | 23 ++++++++++++++++--- python/python/lancedb/remote/table.py | 32 +++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/node/src/index.ts b/node/src/index.ts index 8dd48788..69aea280 100644 --- a/node/src/index.ts +++ b/node/src/index.ts @@ -341,6 +341,7 @@ export interface Table { * * @param column The column to index * @param replace If false, fail if an index already exists on the column + * it is always set to true for remote connections * * Scalar indices, like vector indices, can be used to speed up scans. A scalar * index can speed up scans that contain filter expressions on the indexed column. @@ -384,7 +385,7 @@ export interface Table { * await table.createScalarIndex('my_col') * ``` */ - createScalarIndex: (column: string, replace: boolean) => Promise + createScalarIndex: (column: string, replace?: boolean) => Promise /** * Returns the number of rows in this table. @@ -914,7 +915,10 @@ export class LocalTable implements Table { }) } - async createScalarIndex (column: string, replace: boolean): Promise { + async createScalarIndex (column: string, replace?: boolean): Promise { + if (replace === undefined) { + replace = true + } return tableCreateScalarIndex.call(this._tbl, column, replace) } diff --git a/node/src/remote/index.ts b/node/src/remote/index.ts index 56c3181d..6e6e590a 100644 --- a/node/src/remote/index.ts +++ b/node/src/remote/index.ts @@ -397,7 +397,7 @@ export class RemoteTable implements Table { } const column = indexParams.column ?? 'vector' - const indexType = 'vector' // only vector index is supported for remote connections + const indexType = 'vector' const metricType = indexParams.metric_type ?? 'L2' const indexCacheSize = indexParams.index_cache_size ?? null @@ -420,8 +420,25 @@ export class RemoteTable implements Table { } } - async createScalarIndex (column: string, replace: boolean): Promise { - throw new Error('Not implemented') + async createScalarIndex (column: string): Promise { + const indexType = 'scalar' + + const data = { + column, + index_type: indexType, + replace: true + } + const res = await this._client.post( + `/v1/table/${this._name}/create_scalar_index/`, + data + ) + if (res.status !== 200) { + throw new Error( + `Server Error, status: ${res.status}, ` + + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `message: ${res.statusText}: ${res.data}` + ) + } } async countRows (): Promise { diff --git a/python/python/lancedb/remote/table.py b/python/python/lancedb/remote/table.py index dafa2e2c..bbae9921 100644 --- a/python/python/lancedb/remote/table.py +++ b/python/python/lancedb/remote/table.py @@ -66,12 +66,36 @@ class RemoteTable(Table): """to_pandas() is not yet supported on LanceDB cloud.""" return NotImplementedError("to_pandas() is not yet supported on LanceDB cloud.") - def create_scalar_index(self, *args, **kwargs): - """Creates a scalar index""" - return NotImplementedError( - "create_scalar_index() is not yet supported on LanceDB cloud." + def list_indices(self): + """List all the indices on the table""" + print(self._name) + resp = self._conn._client.post(f"/v1/table/{self._name}/index/list/") + return resp + + def create_scalar_index( + self, + column: str, + ): + """Creates a scalar index + Parameters + ---------- + column : str + The column to be indexed. Must be a boolean, integer, float, + or string column. + """ + index_type = "scalar" + + data = { + "column": column, + "index_type": index_type, + "replace": True, + } + resp = self._conn._client.post( + f"/v1/table/{self._name}/create_scalar_index/", data=data ) + return resp + def create_index( self, metric="L2",