Skip to content

Commit

Permalink
support insert null (#382)
Browse files Browse the repository at this point in the history
* avoid using type Function

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

* support insert null

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

* fix build

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

* fix test

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

* update grpc util

Signed-off-by: ryjiang <jiangruiyi@gmail.com>

---------

Signed-off-by: ryjiang <jiangruiyi@gmail.com>
  • Loading branch information
shanghaikid authored Dec 11, 2024
1 parent 0f6fe26 commit 22e613e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 23 deletions.
4 changes: 1 addition & 3 deletions milvus/grpc/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ export class Data extends Collection {
switch (DataTypeMap[field.type]) {
case DataType.BinaryVector:
case DataType.FloatVector:
field.data = (field.data as number[]).concat(
buildFieldData(rowData, field) as number[]
);
field.data = field.data.concat(buildFieldData(rowData, field));
break;
default:
field.data[rowIndex] = buildFieldData(
Expand Down
8 changes: 4 additions & 4 deletions milvus/types/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface ShowCollectionsReq extends GrpcTimeOut {

export type Properties = Record<string, string | number | boolean>;

export type Function = {
export type FunctionObject = {
name: string;
description?: string;
type: FunctionType;
Expand All @@ -119,7 +119,7 @@ export interface BaseCreateCollectionReq extends GrpcTimeOut {
enableDynamicField?: boolean; // optional, alias of enable_dynamic_field
properties?: Properties; // optional, collection properties
db_name?: string; // optional, db name
functions?: Function[]; // optionals, doc-in/doc-out functions
functions?: FunctionObject[]; // optionals, doc-in/doc-out functions
}

export interface CreateCollectionWithFieldsReq extends BaseCreateCollectionReq {
Expand Down Expand Up @@ -203,7 +203,7 @@ export interface CollectionSchema {
enable_dynamic_field: boolean;
autoID: boolean;
fields: FieldSchema[];
functions: Function[];
functions: FunctionObject[];
}

export interface DescribeCollectionResponse extends TimeStamp {
Expand All @@ -222,7 +222,7 @@ export interface DescribeCollectionResponse extends TimeStamp {
shards_num: number;
num_partitions?: string; // int64
db_name: string;
functions: Function[];
functions: FunctionObject[];
}

export interface GetCompactionPlansResponse extends resStatusResponse {
Expand Down
25 changes: 20 additions & 5 deletions milvus/types/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export type FieldData =
| JSON
| Array
| VectorTypes
| null;
| null
| undefined;

// Represents a row of data in Milvus.
export interface RowData {
Expand Down Expand Up @@ -107,14 +108,28 @@ export type InsertTransformers = {
[DataType.Float16Vector]?: (f16: Float16Vector) => Buffer;
};

export interface InsertReq extends collectionNameReq {
// Base properties shared by both variants
interface BaseInsertReq extends collectionNameReq {
partition_name?: string; // partition name
data?: RowData[]; // data to insert
fields_data?: RowData[]; // alias for data
hash_keys?: Number[]; // user can generate hash value depend on primarykey value
hash_keys?: number[]; // user can generate hash value depend on primarykey value
transformers?: InsertTransformers; // provide custom data transformer for specific data type like bf16 or f16 vectors
}

// Variant with data property
interface DataInsertReq extends BaseInsertReq {
data: RowData[]; // data to insert
fields_data?: never; // Ensure fields_data cannot be used
}

// Variant with fields_data property
interface FieldsDataInsertReq extends BaseInsertReq {
fields_data: RowData[]; // alias for data
data?: never; // Ensure data cannot be used
}

// Union type to enforce mutual exclusivity
export type InsertReq = DataInsertReq | FieldsDataInsertReq;

interface BaseDeleteReq extends collectionNameReq {
partition_name?: string; // partition name
consistency_level?:
Expand Down
6 changes: 4 additions & 2 deletions milvus/utils/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,11 @@ export const buildFieldData = (
: Buffer.alloc(0);
case DataType.Array:
const elementField = { ...field, type: elementType! };
return buildFieldData(rowData, elementField, transformers);
return rowData[name] === null
? undefined
: buildFieldData(rowData, elementField, transformers);
default:
return rowData[name];
return rowData[name] === null ? undefined : rowData[name];
}
};

Expand Down
4 changes: 2 additions & 2 deletions milvus/utils/Grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const getRetryInterceptor = ({
} else {
const string = JSON.stringify(savedReceiveMessage);
const msg =
string.length > 2048 ? string.slice(0, 2048) + '...' : string;
string.length > 4096 ? string.slice(0, 4096) + '...' : string;

logger.debug(
`\x1b[32m[Response(${
Expand All @@ -215,7 +215,7 @@ export const getRetryInterceptor = ({
const string = JSON.stringify(message);
// if string is too big, just show 1000 characters
const msg =
string.length > 2048 ? string.slice(0, 2048) + '...' : string;
string.length > 4096 ? string.slice(0, 4096) + '...' : string;
logger.debug(
`\x1b[34m[Request]\x1b[0m${clientId}>${dbname}>\x1b[1m${methodName}(${timeoutInSeconds})\x1b[0m: ${msg}`
);
Expand Down
2 changes: 1 addition & 1 deletion test/grpc/PartialLoad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe(`Partial load API `, () => {
expr: `id > 0`,
output_fields: ['*'],
});
console.dir(query, { depth: null });
// console.dir(query, { depth: null });
expect(query.status.error_code).toEqual(ErrorCode.SUCCESS);
// result should not contain 'array' field
const keys = Object.keys(query.data[0]);
Expand Down
4 changes: 2 additions & 2 deletions test/tools/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import {
DataType,
ConsistencyLevelEnum,
Function,
FunctionObject,
} from '../../milvus';
import { GENERATE_VECTOR_NAME } from './';

Expand Down Expand Up @@ -51,7 +51,7 @@ export const genCollectionParams = (data: {
enableDynamic?: boolean;
maxCapacity?: number;
idType?: DataType;
functions?: Function[];
functions?: FunctionObject[];
}) => {
const {
collectionName,
Expand Down
6 changes: 5 additions & 1 deletion test/tools/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ export const generateInsertData = (
// get data type
const data_type = convertToDataType(field.data_type);

if ((field.nullable || field.default_value) && Math.random() < 0.5) {
if (field.nullable && Math.random() < 0.5) {
value[field.name] = null;
continue;
}
if (field.default_value && Math.random() < 0.5) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions test/utils/Format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ describe('utils/format', () => {
});

it('should return an object with keys from data and fieldsDataMap', () => {
const data = { key1: 'value1', key2: 'value2' };
const data = { key1: 'value1', key2: null };
const fieldsDataMap = new Map([
[
'key1',
Expand All @@ -577,7 +577,7 @@ describe('utils/format', () => {
{
name: 'key2',
type: 'VarChar',
data: [{ key2: 'value2' }],
data: [{ key2: null }],
} as _Field,
],
]);
Expand All @@ -586,7 +586,7 @@ describe('utils/format', () => {
expect(result).toEqual({
[dynamicField]: {},
key1: 'value1',
key2: 'value2',
key2: null,
});
});

Expand Down

0 comments on commit 22e613e

Please sign in to comment.