Skip to content

Commit

Permalink
fix: when insert do not add function output field to dynamic field (m…
Browse files Browse the repository at this point in the history
…ilvus-io#2382)

Signed-off-by: Buqian Zheng <[email protected]>
  • Loading branch information
zhengbuqian authored Nov 26, 2024
1 parent dd629dd commit 73d0394
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
34 changes: 26 additions & 8 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ def _is_input_field(field: Dict, is_upsert: bool):
"is_function_output", False
)

@staticmethod
def _function_output_field_names(fields_info: List[Dict]):
return [field["name"] for field in fields_info if field.get("is_function_output", False)]

@staticmethod
def _num_input_fields(fields_info: List[Dict], is_upsert: bool):
return len([field for field in fields_info if Prepare._is_input_field(field, is_upsert)])
Expand All @@ -407,6 +411,7 @@ def _parse_row_request(
input_fields_info = [
field for field in fields_info if Prepare._is_input_field(field, is_upsert=False)
]
function_output_field_names = Prepare._function_output_field_names(fields_info)
fields_data = {
field["name"]: schema_types.FieldData(field_name=field["name"], type=field["type"])
for field in input_fields_info
Expand All @@ -426,10 +431,16 @@ def _parse_row_request(
msg = f"expected Dict, got '{type(entity).__name__}'"
raise TypeError(msg)
for k, v in entity.items():
if k not in fields_data and not enable_dynamic:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedField % k
)
if k not in fields_data:
if k in function_output_field_names:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedFunctionOutputField % k
)

if not enable_dynamic:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedField % k
)

if k in fields_data:
field_info, field_data = field_info_map[k], fields_data[k]
Expand Down Expand Up @@ -482,6 +493,7 @@ def _parse_upsert_row_request(
input_fields_info = [
field for field in fields_info if Prepare._is_input_field(field, is_upsert=True)
]
function_output_field_names = Prepare._function_output_field_names(fields_info)
fields_data = {
field["name"]: schema_types.FieldData(field_name=field["name"], type=field["type"])
for field in input_fields_info
Expand All @@ -501,10 +513,16 @@ def _parse_upsert_row_request(
msg = f"expected Dict, got '{type(entity).__name__}'"
raise TypeError(msg)
for k, v in entity.items():
if k not in fields_data and not enable_dynamic:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedField % k
)
if k not in fields_data:
if k in function_output_field_names:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedFunctionOutputField % k
)

if not enable_dynamic:
raise DataNotMatchException(
message=ExceptionsMessage.InsertUnexpectedField % k
)

if k in fields_data:
field_info, field_data = field_info_map[k], fields_data[k]
Expand Down
3 changes: 3 additions & 0 deletions pymilvus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ class ExceptionsMessage:
InsertUnexpectedField = (
"Attempt to insert an unexpected field `%s` to collection without enabling dynamic field"
)
InsertUnexpectedFunctionOutputField = (
"Attempt to insert an unexpected function output field `%s` to collection"
)
InsertMissedField = (
"Insert missed an field `%s` to collection without set nullable==true or set default_value"
)
Expand Down

0 comments on commit 73d0394

Please sign in to comment.