Skip to content
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
27 changes: 27 additions & 0 deletions src/impl/types/StructFieldSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ StructFieldSchema::WithNullable(bool nullable) {
return *this;
}

const std::map<std::string, std::string>&
StructFieldSchema::TypeParams() const {
return type_params_;
}

void
StructFieldSchema::SetTypeParams(const std::map<std::string, std::string>& params) {
// it is a merge operation, keep old pairs
type_params_.insert(params.begin(), params.end());
}

void
StructFieldSchema::SetTypeParams(std::map<std::string, std::string>&& params) {
if (type_params_.empty()) {
type_params_ = std::move(params);
} else {
// it is a merge operation, keep old pairs
type_params_.insert(params.begin(), params.end());
}
}

StructFieldSchema&
StructFieldSchema::AddTypeParam(const std::string& key, const std::string& val) {
type_params_.insert(std::make_pair(key, val));
return *this;
}

const std::vector<FieldSchema>&
StructFieldSchema::Fields() const {
return fields_;
Expand Down
13 changes: 13 additions & 0 deletions src/impl/utils/TypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ ConvertStructFieldSchema(const proto::schema::StructArrayFieldSchema& proto_sche
field_schema.SetDescription(proto_schema.description());
field_schema.SetNullable(proto_schema.nullable());

std::map<std::string, std::string> params;
for (int k = 0; k < proto_schema.type_params_size(); ++k) {
auto& kv = proto_schema.type_params(k);
params.emplace(kv.key(), kv.value());
}
field_schema.SetTypeParams(std::move(params));

for (auto i = 0; i < proto_schema.fields_size(); i++) {
const auto& proto_field = proto_schema.fields(i);

Expand Down Expand Up @@ -715,6 +722,12 @@ ConvertStructFieldSchema(const StructFieldSchema& schema, proto::schema::StructA
proto_schema.set_description(schema.Description());
proto_schema.set_nullable(schema.IsNullable());

for (const auto& pair : schema.TypeParams()) {
auto kv = proto_schema.add_type_params();
kv->set_key(pair.first);
kv->set_value(pair.second);
}

for (auto& field : schema.Fields()) {
auto proto_field = proto_schema.add_fields();
ConvertFieldSchema(field, *proto_field);
Expand Down
35 changes: 35 additions & 0 deletions src/include/milvus/types/StructFieldSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <map>
#include <vector>

#include "FieldSchema.h"
Expand Down Expand Up @@ -127,6 +128,38 @@ class MILVUS_SDK_API StructFieldSchema {
StructFieldSchema&
WithNullable(bool nullable);

/**
* @brief Extra key-value pair setting for this struct field.
* @return the type params.
*/
const std::map<std::string, std::string>&
TypeParams() const;

/**
* @brief Set extra key-value pair setting for this struct field.
* Note: this is a merge operation, existing keys are kept and not overwritten.
* @param [in] params the params.
*/
void
SetTypeParams(const std::map<std::string, std::string>& params);
Comment thread
yhmo marked this conversation as resolved.

/**
* @brief Set extra key-value pair setting for this struct field.
* Note: this is a merge operation, existing keys are kept and not overwritten.
* @param [in] params the params.
*/
void
SetTypeParams(std::map<std::string, std::string>&& params);

/**
* @brief Add an extra key-value pair setting for this struct field.
* Note: if the key already exists, the new value is ignored.
* @param [in] key the key.
* @param [in] val the val.
*/
StructFieldSchema&
AddTypeParam(const std::string& key, const std::string& val);

/**
* @brief Get sub fields of the struct field.
* @return the fields.
Expand Down Expand Up @@ -154,6 +187,8 @@ class MILVUS_SDK_API StructFieldSchema {
int64_t capacity_{0};
bool is_nullable_{false};

std::map<std::string, std::string> type_params_;

std::vector<FieldSchema> fields_;
};
} // namespace milvus
36 changes: 36 additions & 0 deletions test/ut/types/TestStructFieldSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,51 @@ TEST_F(StructFieldSchemaTest, AddFieldRvalue) {
EXPECT_EQ(&ref, &schema);
}

TEST_F(StructFieldSchemaTest, SetNullable) {
milvus::StructFieldSchema schema;
schema.SetNullable(true);
EXPECT_TRUE(schema.IsNullable());
}

TEST_F(StructFieldSchemaTest, TypeParams) {
milvus::StructFieldSchema schema;
EXPECT_TRUE(schema.TypeParams().empty());

schema.SetTypeParams({{"mmap.enabled", "true"}});
schema.AddTypeParam("warmup", "{\"policy\":\"async\"}");
EXPECT_EQ(schema.TypeParams().at("mmap.enabled"), "true");
EXPECT_EQ(schema.TypeParams().at("warmup"), "{\"policy\":\"async\"}");
}

TEST_F(StructFieldSchemaTest, SetTypeParamsRvalue) {
milvus::StructFieldSchema schema;
schema.SetTypeParams(std::map<std::string, std::string>{{"mmap.enabled", "true"}});
schema.SetTypeParams(std::map<std::string, std::string>{{"warmup", "sync"}});
EXPECT_EQ(schema.TypeParams().size(), 2);
EXPECT_EQ(schema.TypeParams().at("mmap.enabled"), "true");
EXPECT_EQ(schema.TypeParams().at("warmup"), "sync");
}

TEST_F(StructFieldSchemaTest, AddTypeParamFluent) {
milvus::StructFieldSchema schema;
auto& ref = schema.AddTypeParam("mmap.enabled", "false");
EXPECT_EQ(schema.TypeParams().at("mmap.enabled"), "false");
EXPECT_EQ(&ref, &schema);
}

TEST_F(StructFieldSchemaTest, ChainingMethods) {
milvus::StructFieldSchema schema;
schema.WithName("s1")
.WithDescription("desc")
.WithMaxCapacity(50)
.WithNullable(true)
.AddTypeParam("mmap.enabled", "true")
.AddField(milvus::FieldSchema("f1", milvus::DataType::VARCHAR))
.AddField(milvus::FieldSchema("f2", milvus::DataType::INT32));
EXPECT_EQ(schema.Name(), "s1");
EXPECT_EQ(schema.Description(), "desc");
EXPECT_EQ(schema.MaxCapacity(), 50);
EXPECT_TRUE(schema.IsNullable());
EXPECT_EQ(schema.TypeParams().at("mmap.enabled"), "true");
EXPECT_EQ(schema.Fields().size(), 2);
}
13 changes: 12 additions & 1 deletion test/ut/utils/TestTypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,12 @@ TEST_F(TypeUtilsTest, ConvertStructFieldSchemaFromProto) {
milvus::proto::schema::StructArrayFieldSchema proto_schema;
proto_schema.set_name("my_struct");
proto_schema.set_description("test struct");
auto* kv1 = proto_schema.add_type_params();
kv1->set_key("mmap.enabled");
kv1->set_value("true");
auto* kv2 = proto_schema.add_type_params();
kv2->set_key("warmup");
kv2->set_value("{\"policy\":\"async\"}");
auto* f1 = proto_schema.add_fields();
f1->set_name("sub_int32");
f1->set_data_type(milvus::proto::schema::DataType::Int32);
Expand All @@ -930,6 +936,8 @@ TEST_F(TypeUtilsTest, ConvertStructFieldSchemaFromProto) {
milvus::StructFieldSchema sdk_schema;
milvus::ConvertStructFieldSchema(proto_schema, sdk_schema);
EXPECT_EQ(sdk_schema.Name(), "my_struct");
EXPECT_EQ(sdk_schema.TypeParams().at("mmap.enabled"), "true");
EXPECT_EQ(sdk_schema.TypeParams().at("warmup"), "{\"policy\":\"async\"}");
EXPECT_EQ(sdk_schema.Fields().size(), 2);
}

Expand All @@ -940,14 +948,17 @@ TEST_F(TypeUtilsTest, ConvertStructFieldSchemaViaCollectionSchema) {
schema.AddField(milvus::FieldSchema("vec", milvus::DataType::FLOAT_VECTOR).WithDimension(4));

milvus::StructFieldSchema struct_schema;
struct_schema.WithName("my_struct").WithMaxCapacity(5);
struct_schema.WithName("my_struct").WithMaxCapacity(5).AddTypeParam("mmap.enabled", "true");
struct_schema.AddField(milvus::FieldSchema("sub_int", milvus::DataType::INT32));
schema.AddStructField(std::move(struct_schema));

milvus::proto::schema::CollectionSchema proto_schema;
milvus::ConvertCollectionSchema(schema, proto_schema);
EXPECT_EQ(proto_schema.struct_array_fields_size(), 1);
EXPECT_EQ(proto_schema.struct_array_fields(0).name(), "my_struct");
EXPECT_EQ(proto_schema.struct_array_fields(0).type_params_size(), 1);
EXPECT_EQ(proto_schema.struct_array_fields(0).type_params(0).key(), "mmap.enabled");
EXPECT_EQ(proto_schema.struct_array_fields(0).type_params(0).value(), "true");
}

TEST_F(TypeUtilsTest, ConvertResourceGroupConfigRoundtrip) {
Expand Down
Loading