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

[Cherry-Pick]Support rename collection #1302

Merged
merged 1 commit into from
Feb 15, 2023
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
24 changes: 22 additions & 2 deletions examples/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
default_float_vec_field_name = "float_vector"
default_binary_vec_field_name = "binary_vector"


all_index_types = [
"FLAT",
"IVF_FLAT",
Expand Down Expand Up @@ -55,7 +54,6 @@
{"nlist": 128}
]


default_index = {"index_type": "IVF_FLAT", "params": {"nlist": 128}, "metric_type": "L2"}
default_binary_index = {"index_type": "BIN_FLAT", "params": {"nlist": 1024}, "metric_type": "JACCARD"}

Expand Down Expand Up @@ -300,6 +298,26 @@ def alias_cases():
alias_cases()


def test_rename_collection():
connections.connect(alias="default")
schema = CollectionSchema(fields=[
FieldSchema("int64", DataType.INT64, description="int64", is_primary=True),
FieldSchema("float_vector", DataType.FLOAT_VECTOR, is_primary=False, dim=128),
])

old_collection = "old_collection"
new_collection = "new_collection"
Collection(old_collection, schema=schema)

print("\nlist collections:")
print(utility.list_collections())
assert utility.has_collection(old_collection)

utility.rename_collection(old_collection, new_collection)
assert utility.has_collection(new_collection)
assert not utility.has_collection(old_collection)


if __name__ == "__main__":
print("test collection and get an existing collection")
name = test_create_collection()
Expand All @@ -317,4 +335,6 @@ def alias_cases():
test_specify_primary_key()
print("test alias")
test_alias()
print("test rename collection")
test_rename_collection()
print("test end")
11 changes: 11 additions & 0 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ def list_collections(self, timeout=None):

raise MilvusException(status.error_code, status.reason)

@retry_on_rpc_failure()
def rename_collections(self, old_name=None, new_name=None, timeout=None):
check_pass_param(collection_name=new_name)
check_pass_param(collection_name=old_name)
request = Prepare().rename_collections_request(old_name, new_name)
rf = self._stub.RenameCollection.future(request, timeout=timeout)
response = rf.result()

if response.error_code != 0:
raise MilvusException(response.error_code, response.reason)

@retry_on_rpc_failure()
def create_partition(self, collection_name, partition_name, timeout=None):
check_pass_param(collection_name=collection_name, partition_name=partition_name)
Expand Down
4 changes: 4 additions & 0 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def show_collections_request(cls, collection_names=None):
req.type = milvus_types.ShowType.InMemory
return req

@classmethod
def rename_collections_request(cls, old_name=None, new_name=None):
return milvus_types.RenameCollectionRequest(oldName=old_name, newName=new_name)

@classmethod
def create_partition_request(cls, collection_name, partition_name):
return milvus_types.CreatePartitionRequest(collection_name=collection_name, partition_name=partition_name)
Expand Down
37 changes: 19 additions & 18 deletions pymilvus/grpc_gen/common_pb2.py

Large diffs are not rendered by default.

36 changes: 23 additions & 13 deletions pymilvus/grpc_gen/milvus_pb2.py

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions pymilvus/grpc_gen/milvus_pb2_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ def __init__(self, channel):
request_serializer=milvus__pb2.DescribeResourceGroupRequest.SerializeToString,
response_deserializer=milvus__pb2.DescribeResourceGroupResponse.FromString,
)
self.RenameCollection = channel.unary_unary(
'/milvus.proto.milvus.MilvusService/RenameCollection',
request_serializer=milvus__pb2.RenameCollectionRequest.SerializeToString,
response_deserializer=common__pb2.Status.FromString,
)


class MilvusServiceServicer(object):
Expand Down Expand Up @@ -753,6 +758,12 @@ def DescribeResourceGroup(self, request, context):
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')

def RenameCollection(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_MilvusServiceServicer_to_server(servicer, server):
rpc_method_handlers = {
Expand Down Expand Up @@ -1086,6 +1097,11 @@ def add_MilvusServiceServicer_to_server(servicer, server):
request_deserializer=milvus__pb2.DescribeResourceGroupRequest.FromString,
response_serializer=milvus__pb2.DescribeResourceGroupResponse.SerializeToString,
),
'RenameCollection': grpc.unary_unary_rpc_method_handler(
servicer.RenameCollection,
request_deserializer=milvus__pb2.RenameCollectionRequest.FromString,
response_serializer=common__pb2.Status.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'milvus.proto.milvus.MilvusService', rpc_method_handlers)
Expand Down Expand Up @@ -2218,6 +2234,23 @@ def DescribeResourceGroup(request,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

@staticmethod
def RenameCollection(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/milvus.proto.milvus.MilvusService/RenameCollection',
milvus__pb2.RenameCollectionRequest.SerializeToString,
common__pb2.Status.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)


class ProxyServiceStub(object):
"""Missing associated documentation comment in .proto file."""
Expand Down
30 changes: 30 additions & 0 deletions pymilvus/orm/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,36 @@ def drop_collection(collection_name, timeout=None, using="default"):
return _get_connection(using).drop_collection(collection_name, timeout=timeout)


def rename_collection(old_collection_name, new_collection_name, timeout=None, using="default"):
"""
Rename a collection to new collection name
:param old_collection_name: A string representing old name of the renamed collection
:type old_collection_name: str
:param new_collection_name: A string representing new name of the renamed collection
:type new_collection_name: str
:param timeout: An optional duration of time in seconds to allow for the RPC. When timeout
is set to None, client waits until server response or error occur.
:type timeout: float
:example:
>>> from pymilvus import Collection, FieldSchema, CollectionSchema, DataType, connections, utility
>>> connections.connect(alias="default")
>>> schema = CollectionSchema(fields=[
... FieldSchema("int64", DataType.INT64, description="int64", is_primary=True),
... FieldSchema("float_vector", DataType.FLOAT_VECTOR, is_primary=False, dim=128),
... ])
>>> collection = Collection(name="old_collection", schema=schema)
>>> utility.rename_collection("old_collection", "new_collection")
>>> True
>>> utility.drop_collection("new_collection")
>>> utility.has_collection("new_collection")
>>> False
"""
return _get_connection(using).rename_collections(old_collection_name, new_collection_name, timeout=timeout)

def list_collections(timeout=None, using="default") -> list:
"""
Returns a list of all collection names.
Expand Down