imbajin commented on code in PR #422:
URL: https://github.com/apache/hugegraph-ai/pull/422#discussion_r4536024379
##########
hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/commit_to_hugegraph.py:
##########
@@ -162,15 +198,35 @@ def load_into_graph(self, vertices, edges, schema): #
pylint: disable=too-many-
else:
result =
self._handle_graph_creation(self.client.graph().add_vertex, input_label,
input_properties)
if result is None:
- raise ValueError(f"Failed to create vertex '{input_label}'
with properties {input_properties}")
+ log.error(
+ "Failed to create vertex '%s' with properties %s, skip it
& need check it again",
+ input_label,
+ input_properties,
+ )
+ skipped_vertices += 1
+ skip_reports.append(f"{mapping_id or input_label}: server
rejected the vertex")
+ if mapping_id:
+ skipped_ids.add(mapping_id)
+ continue
Review Comment:
⚠️ Partial failures need to reach the caller. If one vertex is created and
another is rejected by the server, this now continues and `run()` returns the
original data, still containing the rejected vertex; the import flow then
returns successfully without identifying the skipped vertex or its dependent
edges. Issue #373 explicitly asks for this summary in the import result. Please
propagate skipped IDs and reasons through the flow/API result, with a test
covering one successful and one rejected vertex.
##########
hugegraph-llm/src/hugegraph_llm/operators/hugegraph_op/commit_to_hugegraph.py:
##########
@@ -307,14 +394,80 @@ def _check_single_data_type(self, data_type: str, value)
-> bool:
PropertyDataType.INT.value,
PropertyDataType.LONG.value,
):
- return isinstance(value, int)
+ # bool is an int subclass in Python, but True is not a valid
number here
+ return isinstance(value, int) and not isinstance(value, bool)
if data_type in (PropertyDataType.FLOAT.value,
PropertyDataType.DOUBLE.value):
return isinstance(value, float)
- if data_type in (PropertyDataType.TEXT.value,
PropertyDataType.UUID.value):
+ if data_type in (
+ PropertyDataType.TEXT.value,
+ PropertyDataType.UUID.value,
+ PropertyDataType.BLOB.value,
+ ):
+ # BLOB is created as TEXT (see _set_property_data_type), so it
validates as text
return isinstance(value, str)
# TODO: check ok below
if data_type == PropertyDataType.DATE.value: # the format should be
"yyyy-MM-dd"
import re
- return isinstance(value, str) and re.match(r"^\d{4}-\d{2}-\d{2}$",
value)
+ return isinstance(value, str) and
bool(re.match(r"^\d{4}-\d{2}-\d{2}$", value))
raise ValueError(f"Unknown/Unsupported data type: {data_type}")
+
+ def _coerce_property_value(self, data_type: str, cardinality: str, value)
-> Tuple[Any, bool]:
+ """Losslessly coerce a property value to its declared type and
cardinality.
+
+ Returns ``(coerced_value, True)`` when the value is already valid or
convertible
+ without losing information (numeric strings to numbers, numbers to
text, scalars
+ to single-element collections), otherwise ``(None, False)`` so the
caller can
+ report the value instead of silently dropping it.
+ """
+ if cardinality in (PropertyCardinality.LIST.value,
PropertyCardinality.SET.value):
+ items = value if isinstance(value, list) else [value]
+ coerced_items = []
+ for item in items:
+ coerced, ok = self._coerce_single_value(data_type, item)
+ if not ok:
+ return None, False
+ coerced_items.append(coerced)
+ return coerced_items, True
+ return self._coerce_single_value(data_type, value)
+
+ def _coerce_single_value(self, data_type: str, value) -> Tuple[Any, bool]:
+ try:
+ if self._check_property_data_type(data_type,
PropertyCardinality.SINGLE.value, value):
+ return value, True
+ candidates = self._lossless_candidates(data_type, value)
+ for candidate in candidates:
+ if self._check_property_data_type(data_type,
PropertyCardinality.SINGLE.value, candidate):
+ return candidate, True
+ except ValueError:
+ return None, False # unknown data type: the caller reports the
value
+ return None, False
+
+ @staticmethod
+ def _lossless_candidates(data_type: str, value) -> List[Any]:
+ """Values that carry the same information as ``value`` in the declared
type."""
+ if isinstance(value, bool):
+ return []
+ if data_type in (PropertyDataType.TEXT.value,
PropertyDataType.UUID.value, PropertyDataType.BLOB.value):
+ return [str(value)] if isinstance(value, (int, float)) else []
+ if data_type in (
+ PropertyDataType.BYTE.value,
+ PropertyDataType.INT.value,
+ PropertyDataType.LONG.value,
+ ):
+ if isinstance(value, float):
+ return [int(value)] if value.is_integer() else []
+ if isinstance(value, str):
+ try:
+ return [int(value)]
+ except ValueError:
+ return []
+ return []
+ if data_type in (PropertyDataType.FLOAT.value,
PropertyDataType.DOUBLE.value):
+ if isinstance(value, (int, float, str)):
+ try:
+ return [float(value)]
Review Comment:
⚠️ This conversion can silently change the property value. For a DOUBLE
property, both `9007199254740993` and its string form become
`9007199254740992.0` here and are accepted as valid, so the graph write
receives a different value. Please reject conversions that lose information and
cover large integers and numeric strings with a regression test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]