Skip to content

Commit 602496b

Browse files
[Backport 3.0] fix: fallback add field for external collections (#3727) (#3728)
Backport of #3727 to `3.0`. Signed-off-by: sijie-ni-0214 <[email protected]> Signed-off-by: pymilvus-bot <[email protected]> Co-authored-by: sijie-ni-0214 <[email protected]>
1 parent 0fa3eac commit 602496b

5 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎pymilvus/client/async_grpc_handler.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
check_status,
7070
get_server_type,
7171
immutable_message_to_dict,
72+
is_external_collection_schema_alter_unsupported,
7273
is_successful,
7374
len_of,
7475
replicate_checkpoint_to_dict,
@@ -1596,14 +1597,21 @@ async def add_collection_field(
15961597
collection_name=collection_name,
15971598
field_schema=field_schema,
15981599
)
1600+
fallback_to_legacy = False
15991601
try:
16001602
response = await self._async_stub.AlterCollectionSchema(
16011603
request, timeout=timeout, metadata=_api_level_md(context)
16021604
)
1603-
check_status(response.alter_status)
1605+
if is_external_collection_schema_alter_unsupported(response.alter_status):
1606+
fallback_to_legacy = True
1607+
else:
1608+
check_status(response.alter_status)
16041609
except grpc.RpcError as e:
16051610
if e.code() != grpc.StatusCode.UNIMPLEMENTED:
16061611
raise
1612+
fallback_to_legacy = True
1613+
1614+
if fallback_to_legacy:
16071615
legacy_request = Prepare.add_collection_field_request(collection_name, field_schema)
16081616
status = await self._async_stub.AddCollectionField(
16091617
legacy_request, timeout=timeout, metadata=_api_level_md(context)

‎pymilvus/client/grpc_handler.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
check_status,
9292
get_server_type,
9393
immutable_message_to_dict,
94+
is_external_collection_schema_alter_unsupported,
9495
is_successful,
9596
len_of,
9697
replicate_checkpoint_to_dict,
@@ -551,14 +552,21 @@ def add_collection_field(
551552
collection_name=collection_name,
552553
field_schema=field_schema,
553554
)
555+
fallback_to_legacy = False
554556
try:
555557
response = self._stub.AlterCollectionSchema(
556558
request, timeout=timeout, metadata=_api_level_md(context)
557559
)
558-
check_status(response.alter_status)
560+
if is_external_collection_schema_alter_unsupported(response.alter_status):
561+
fallback_to_legacy = True
562+
else:
563+
check_status(response.alter_status)
559564
except grpc.RpcError as e:
560565
if e.code() != grpc.StatusCode.UNIMPLEMENTED:
561566
raise
567+
fallback_to_legacy = True
568+
569+
if fallback_to_legacy:
562570
legacy_request = Prepare.add_collection_field_request(collection_name, field_schema)
563571
status = self._stub.AddCollectionField(
564572
legacy_request, timeout=timeout, metadata=_api_level_md(context)

‎pymilvus/client/utils.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ def check_status(status: common_pb2.Status):
7777
raise MilvusException.from_status(status)
7878

7979

80+
def is_external_collection_schema_alter_unsupported(status: common_pb2.Status) -> bool:
81+
is_parameter_invalid = status.code == 1100 or status.error_code == common_pb2.IllegalArgument
82+
return is_parameter_invalid and (
83+
"alter collection schema operation is not supported for external collection"
84+
in status.reason
85+
)
86+
87+
8088
def is_successful(status: common_pb2.Status):
8189
return status.code == 0 and status.error_code == 0
8290

‎tests/unit/async_grpc_handler/test_async_collection.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,47 @@ async def test_add_collection_field_falls_back_for_older_server(self) -> None:
691691
assert GlobalCache.schema.get(handler.server_address, "", "test_coll") is None
692692
GlobalCache._reset_for_testing()
693693

694+
@pytest.mark.asyncio
695+
async def test_add_collection_field_falls_back_for_external_collection(self) -> None:
696+
mock_channel = MagicMock()
697+
mock_channel._unary_unary_interceptors = []
698+
handler = AsyncGrpcHandler(channel=mock_channel)
699+
handler._is_channel_ready = True
700+
handler.ensure_channel_ready = AsyncMock()
701+
702+
mock_stub = AsyncMock()
703+
mock_response = MagicMock()
704+
mock_response.alter_status = common_pb2.Status(
705+
code=1100,
706+
error_code=common_pb2.IllegalArgument,
707+
reason=(
708+
"alter collection schema operation is not supported for external collection "
709+
"test_coll: invalid parameter"
710+
),
711+
)
712+
mock_stub.AlterCollectionSchema = AsyncMock(return_value=mock_response)
713+
legacy_status = MagicMock(code=0, error_code=0, reason="")
714+
mock_stub.AddCollectionField = AsyncMock(return_value=legacy_status)
715+
handler._async_stub = mock_stub
716+
717+
mock_field_schema = MagicMock()
718+
legacy_request = MagicMock()
719+
with patch("pymilvus.client.async_grpc_handler.Prepare") as mock_prepare, patch(
720+
"pymilvus.client.async_grpc_handler.check_pass_param"
721+
), patch("pymilvus.client.async_grpc_handler.check_status") as mock_check_status:
722+
mock_prepare.alter_collection_schema_request.return_value = MagicMock()
723+
mock_prepare.add_collection_field_request.return_value = legacy_request
724+
725+
await handler.add_collection_field("test_coll", mock_field_schema)
726+
727+
mock_stub.AlterCollectionSchema.assert_awaited_once()
728+
mock_prepare.add_collection_field_request.assert_called_once_with(
729+
"test_coll", mock_field_schema
730+
)
731+
mock_stub.AddCollectionField.assert_awaited_once()
732+
assert mock_stub.AddCollectionField.call_args.args[0] is legacy_request
733+
mock_check_status.assert_called_once_with(legacy_status)
734+
694735
@pytest.mark.asyncio
695736
async def test_add_collection_struct_field(self) -> None:
696737
"""Test add_collection_struct_field async API"""

‎tests/unit/grpc_handler/test_collection.py‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pymilvus import FieldSchema, Function, FunctionType, StructFieldSchema
99
from pymilvus.client.cache import GlobalCache
1010
from pymilvus.client.types import DataType
11-
from pymilvus.exceptions import DescribeCollectionException
11+
from pymilvus.exceptions import DescribeCollectionException, MilvusException
1212
from pymilvus.grpc_gen import common_pb2, schema_pb2
1313

1414
from .conftest import (
@@ -243,6 +243,50 @@ def test_add_collection_field_falls_back_for_older_server(self, handler):
243243
assert GlobalCache.schema.get(handler.server_address, "", "coll") is None
244244
GlobalCache._reset_for_testing()
245245

246+
def test_add_collection_field_falls_back_for_external_collection(self, handler):
247+
response = MagicMock()
248+
response.alter_status = common_pb2.Status(
249+
code=1100,
250+
error_code=common_pb2.IllegalArgument,
251+
reason=(
252+
"alter collection schema operation is not supported for external collection coll: "
253+
"invalid parameter"
254+
),
255+
)
256+
handler._stub.AlterCollectionSchema.return_value = response
257+
handler._stub.AddCollectionField.return_value = make_status()
258+
field = FieldSchema(
259+
name="new_field",
260+
dtype=DataType.VARCHAR,
261+
max_length=256,
262+
external_field="source_field",
263+
)
264+
265+
handler.add_collection_field("coll", field)
266+
267+
handler._stub.AlterCollectionSchema.assert_called_once()
268+
handler._stub.AddCollectionField.assert_called_once()
269+
request = handler._stub.AddCollectionField.call_args.args[0]
270+
field_schema = schema_pb2.FieldSchema()
271+
field_schema.ParseFromString(request.schema)
272+
assert field_schema.name == "new_field"
273+
assert field_schema.external_field == "source_field"
274+
275+
def test_add_collection_field_does_not_fallback_for_other_business_errors(self, handler):
276+
response = MagicMock()
277+
response.alter_status = common_pb2.Status(
278+
code=1100,
279+
error_code=common_pb2.IllegalArgument,
280+
reason="invalid add field request",
281+
)
282+
handler._stub.AlterCollectionSchema.return_value = response
283+
field = FieldSchema(name="new_field", dtype=DataType.VARCHAR, max_length=256)
284+
285+
with pytest.raises(MilvusException, match="invalid add field request"):
286+
handler.add_collection_field("coll", field)
287+
288+
handler._stub.AddCollectionField.assert_not_called()
289+
246290
def test_add_collection_struct_field(self, handler):
247291
handler._stub.AddCollectionStructField.return_value = make_status()
248292
struct_field = StructFieldSchema(nullable=True)

0 commit comments

Comments
 (0)