Skip to content

Commit

Permalink
feat: generate expr by pk type when delete entities by ids
Browse files Browse the repository at this point in the history
Signed-off-by: Yicai Yu <yuyicai@hotmail.com>
  • Loading branch information
yuyicai committed Sep 14, 2023
1 parent e614e8d commit 2faf56b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ docs
api
coverage
.DS_Store
yarn-error.log
yarn-error.log
.idea
48 changes: 46 additions & 2 deletions milvus/grpc/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
formatCollectionSchema,
formatDescribedCol,
validatePartitionNumbers,
METADATA,
METADATA, DataTypeMap, DataType,
} from '../';

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ export class Collection extends Database {
* | :-- | :-- | :-- |
* | collection_name | String | Collection name |
* | timeout? | number | An optional duration of time in millisecond to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined |
*
*
* @returns
* | Property | Description |
* | :-- | :-- |
Expand Down Expand Up @@ -985,4 +985,48 @@ export class Collection extends Database {

return pkField;
}

/**
* Get the primary key field type
*
* @param data
* | Property | Type | Description |
* | :-- | :-- | :-- |
* | collection_name | string | the name of the collection |
* | timeout? | number | An optional duration of time in milliseconds to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or an error occurs. Default is undefined |
*
* @returns
* | Property | Description |
* | :-- | :-- |
* | pkFieldType | the primary key field type |
*
* @throws {Error} if `collection_name` property is not present in `data`
*
* #### Example
*
* ```
* new milvusClient(MILUVS_ADDRESS).getPkFieldType({
* collection_name: 'my_collection',
* });
* ```
*/
async getPkFieldType(data: DescribeCollectionReq): Promise<keyof typeof DataType> {
// get collection info
const collectionInfo = await this.describeCollection(data);

// pk field type
let pkFieldType: keyof typeof DataType = 'None';
// extract key information
for (let i = 0; i < collectionInfo.schema.fields.length; i++) {
const f = collectionInfo.schema.fields[i];

// get pk field type info
if (f.is_primary_key) {
pkFieldType = f.data_type;
break;
}
}

return pkFieldType;
}
}
18 changes: 13 additions & 5 deletions milvus/grpc/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Data extends Collection {
* scalar_field: 1
* }]
* });
* ```
* ```
*/
private async _insert(
data: InsertReq,
Expand Down Expand Up @@ -332,10 +332,11 @@ export class Data extends Collection {
}

const pkField = await this.getPkFieldName(data);
const pkFieldType = await this.getPkFieldType(data);

// generate expr by different type of ids
// generate expr by different type of pk
const expr =
typeof data.ids[0] === 'string'
DataTypeMap[pkFieldType] === DataType.VarChar
? `${pkField} in ["${data.ids.join('","')}"]`
: `${pkField} in [${data.ids.join(',')}]`;
const req = { ...data, expr };
Expand Down Expand Up @@ -809,9 +810,16 @@ export class Data extends Collection {
throw new Error(ERROR_REASONS.IDS_REQUIRED);
}

// build query req
const req = { ...data, expr: `${pkField} in [${data.ids.join(',')}]` };
const pkFieldType = await this.getPkFieldType(data);

// generate expr by different type of pk
const expr =
DataTypeMap[pkFieldType] === DataType.VarChar
? `${pkField} in ["${data.ids.join('","')}"]`
: `${pkField} in [${data.ids.join(',')}]`;

// build query req
const req = { ...data, expr };
return this.query(req);
}

Expand Down

0 comments on commit 2faf56b

Please sign in to comment.