Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate expr by pk type when delete entities by ids #242

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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