imbajin commented on code in PR #422:
URL: https://github.com/apache/hugegraph-ai/pull/422#discussion_r4536024365
##########
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:
**⚠️ Preserve the value during DOUBLE coercion**
Both an integer and its string form can be accepted even when `float()`
changes the value:
```python
9007199254740993 -> 9007199254740992.0
"9007199254740993" -> 9007199254740992.0
```
**Impact:** the graph write receives a different value while the conversion
is reported as valid.
**Suggested fix:** reject conversions that lose information, and add
regression coverage for large integers and numeric strings.
##########
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:
**⚠️ Return partial-import failures to the caller**
**Example:** the server creates one vertex but rejects another.
**Actual result:** this branch continues, and `run()` returns the original
data, including the rejected vertex. The import flow returns successfully
without identifying the skipped vertex or its dependent edges.
**Expected:** return skipped IDs and reasons through the flow/API result, as
requested in [#373](https://github.com/apache/hugegraph-ai/issues/373). Cover
the case with one successful and one rejected vertex in a flow-level 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]