Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- **GFQL mixed-type CASE branches**: pandas preserves per-row mixed Cypher values, while Polars and cuDF decline incompatible boolean/string/number branches with structured E110 instead of silently coercing (#2330). Numeric int/float branches retain common-type behavior.
- **GFQL nullable entity keys on cuDF** (#2312): `keys(n)` now returns empty lists for rows with no non-null properties alongside populated rows. Native empty key lists use the string child type expected by the grouped result, and key lists remain aligned with their original entities after the native grouping join.
- **GFQL Cypher empty property maps**: an explicit node `{}` no longer replaces labels, so `MATCH (n:Foo {})` still selects `Foo` nodes. An explicit relationship `{}` no longer replaces relationship types with an empty tuple, so `MATCH ()-[r:R {}]->()` matches only typed `R` relationships on every supported engine, while untyped `n {}` and `r {}` controls remain untyped (#2318).

Expand Down
1 change: 1 addition & 0 deletions bin/test-polars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ POLARS_TEST_FILES=(
graphistry/tests/compute/gfql/test_engine_polars_binding_rows.py
# engine-parametrized (pandas/polars/cudf/polars-gpu); the pandas params also run in
# test-gfql-core, but only this lane has polars installed
graphistry/tests/compute/gfql/test_case_mixed_dtype.py
graphistry/tests/compute/gfql/test_varlen_bounded_engine_parity_1787.py
graphistry/tests/compute/gfql/test_engine_polars_with_match_reentry.py
# engine-parametrized: its pandas params run in test-gfql-core, but the polars params
Expand Down
26 changes: 26 additions & 0 deletions graphistry/compute/gfql/lazy/engine/polars/row_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,21 @@ def _value_category(v: Any) -> Optional[str]:
return None


def _case_branch_value_kind(dt: Optional[pl.DataType]) -> Optional[str]:
"""Coarse Cypher value family for CASE branches; unknown families stay fail-open."""
import polars as pl

if dt is None or dt == pl.Null:
return None
if dt == pl.Boolean:
return "bool"
if _dtype_is_int(dt) or _dtype_is_float(dt):
return "number"
if _dtype_is_stringlike(dt):
return "str"
return None


def _lower_list_literal(items: Sequence[ExprNode], columns: Sequence[str]) -> Optional[pl.Expr]:
"""Lower ``[e0, e1, ...]`` to a per-row list via ``pl.concat_list``, or None to defer.

Expand Down Expand Up @@ -713,6 +728,17 @@ def lower_expr(node: ExprNode, columns: Sequence[str]) -> Optional[pl.Expr]:
wf = lower_expr(node.when_false, columns)
if cond is None or wt is None or wf is None:
return None
true_kind = _case_branch_value_kind(_expr_output_dtype(wt))
false_kind = _case_branch_value_kind(_expr_output_dtype(wf))
if true_kind is not None and false_kind is not None and true_kind != false_kind:
from graphistry.compute.exceptions import ErrorCode, GFQLUnsupportedError
raise GFQLUnsupportedError(
ErrorCode.E110,
"CASE branches return incompatible Cypher value types",
field="expression",
value=sorted([true_kind, false_kind]),
suggestion="Use an explicit cast so both branches have the same value type.",
)
# cast cond to Boolean: a Null-dtype/3-valued WHEN takes the ELSE branch (Cypher,
# matching pandas); no-op on a real Boolean.
return pl.when(cond.cast(pl.Boolean)).then(wt).otherwise(wf)
Expand Down
60 changes: 59 additions & 1 deletion graphistry/compute/gfql/row/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
s_to_numeric,
safe_map_series,
)
from graphistry.compute.exceptions import ErrorCode, GFQLTypeError, GFQLValidationError
from graphistry.compute.exceptions import (
ErrorCode,
GFQLTypeError,
GFQLUnsupportedError,
GFQLValidationError,
)
from graphistry.compute.dataframe_utils import (
concat_frames,
domain_diff,
Expand Down Expand Up @@ -2256,6 +2261,22 @@ def _floor_series(s: Any) -> Any:
cond_mask = self._gfql_bool_mask(table_df, cond_value)
cond_null = self._gfql_null_mask(table_df, cond_value)
cond_true = cond_mask & ~cond_null
true_kind = self._gfql_case_branch_value_kind(true_value)
false_kind = self._gfql_case_branch_value_kind(false_value)
is_pandas = resolve_engine(EngineAbstract.AUTO, table_df) == Engine.PANDAS
if (
not is_pandas
and true_kind is not None
and false_kind is not None
and true_kind != false_kind
):
raise GFQLUnsupportedError(
ErrorCode.E110,
"CASE branches return incompatible Cypher value types",
field="expression",
value=sorted([true_kind, false_kind]),
suggestion="Use an explicit cast so both branches have the same value type.",
)
try:
return True, true_value.where(cond_true, false_value)
except TypeError: # pragma: no cover - cuDF-only (.where mixed-dtype); no cuDF lane in the coverage gate (validated on dgx)
Expand Down Expand Up @@ -2988,6 +3009,43 @@ def _gfql_series_numeric_non_bool_like(series: Any) -> bool:
values = RowPipelineMixin._gfql_series_to_pylist(sample)
return len(values) > 0 and all(RowPipelineMixin._gfql_scalar_numeric_non_bool(v) for v in values)

@staticmethod
def _gfql_case_branch_value_kind(series: SeriesT) -> Optional[str]:
"""Classify CASE branch values by Cypher family; all-null/unknown is not a family."""
if not hasattr(series, "astype"):
if isinstance(series, bool):
return "bool"
if RowPipelineMixin._gfql_scalar_numeric_non_bool(series):
return "number"
return "str" if isinstance(series, str) else None
try:
null_mask = series.isna()
non_null = ~null_mask
if not hasattr(non_null, "any") or not bool(non_null.any()):
return None
dtype = series.dtype
if pd.api.types.is_bool_dtype(dtype):
return "bool"
if pd.api.types.is_numeric_dtype(dtype):
return "number"
if pd.api.types.is_object_dtype(dtype) and isinstance(series, pd.Series):
sample = series.dropna().head(128)
values = RowPipelineMixin._gfql_series_to_pylist(sample)
if not values:
return None
if all(isinstance(value, bool) for value in values):
return "bool"
if all(RowPipelineMixin._gfql_scalar_numeric_non_bool(value) for value in values):
return "number"
if all(isinstance(value, str) for value in values):
return "str"
return "mixed"
if pd.api.types.is_string_dtype(dtype):
return "str"
return str(dtype)
except (TypeError, ValueError):
return None

@staticmethod
def _gfql_format_cypher_scalar_literal(value: Any) -> str:
if value is None or is_null_scalar(value):
Expand Down
94 changes: 84 additions & 10 deletions graphistry/tests/compute/gfql/test_case_mixed_dtype.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
"""Regression: CASE with branches of different dtypes must be engine-consistent.

pandas coerces the two CASE branches to a common type, but cuDF's `.where` raises
`TypeError: cudf does not support mixed types`, which surfaced as a hard
`GFQLTypeError` (e.g. `CASE WHEN path IS NULL THEN -1 ELSE length(path) END` over an
UNREACHABLE shortestPath: the hops branch is an object/null column, the other is int
-1). The evaluator now unifies the branch dtypes and retries, so cuDF returns the same
value pandas does instead of raising. This general repro uses an all-null object column
(same mechanism, no shortestPath dependency).
"""Regression: CASE must never silently coerce incompatible Cypher value families.

pandas can preserve mixed branch values in an object column. Columnar engines with
homogeneous columns do not guess a Cypher type: they decline incompatible families.
"""
import os

import pandas as pd
import pytest

import graphistry
from graphistry.compute.exceptions import ErrorCode, GFQLUnsupportedError
from graphistry.compute.gfql.row.pipeline import RowPipelineMixin

try:
import cudf # noqa: F401
_HAS_CUDF = True
except Exception: # pragma: no cover
_HAS_CUDF = False

_ENGINES = ["pandas"] + (["cudf"] if _HAS_CUDF else [])
try:
import polars # noqa: F401
_HAS_POLARS = True
except Exception: # pragma: no cover
_HAS_POLARS = False

_HAS_CUDF_POLARS = _HAS_CUDF
_ENGINES = ["pandas"] + (
["cudf"] if _HAS_CUDF and os.environ.get("TEST_CUDF") == "1" else []
)
FOUR_ENGINES = _ENGINES + [
*(["polars"] if _HAS_POLARS else []),
*(
["polars-gpu"]
if _HAS_CUDF_POLARS and os.environ.get("TEST_POLARS_GPU") == "1"
else []
),
]

_N = pd.DataFrame({"id": [0, 1, 2], "x": pd.Series([None, None, None], dtype="object")})
_E = pd.DataFrame({"s": [0], "d": [1], "eid": [0]})


def _graph(engine):
nodes = _N
edges = _E
if engine == "cudf":
import cudf

nodes, edges = cudf.from_pandas(nodes), cudf.from_pandas(edges)
elif engine in {"polars", "polars-gpu"}:
import polars as pl

nodes, edges = pl.from_pandas(nodes), pl.from_pandas(edges)
return graphistry.nodes(nodes, "id").edges(edges, "s", "d").bind(edge="eid")


def test_case_branch_family_classifier_pins_null_scalar_and_object_paths():
classify = RowPipelineMixin._gfql_case_branch_value_kind

assert classify(1) == "number"
assert classify(True) == "bool"
assert classify("x") == "str"
assert classify(None) is None
assert classify(pd.Series([None, None], dtype="object")) is None
assert classify(pd.Series([True, False])) == "bool"
assert classify(pd.Series([1, 2.5])) == "number"
assert classify(pd.Series(["x", None], dtype="object")) == "str"
assert classify(pd.Series([True, 1], dtype="object")) == "mixed"


@pytest.mark.parametrize("engine", _ENGINES)
def test_case_mixed_dtype_branches_do_not_raise(engine):
# int THEN branch vs all-null object ELSE branch: cuDF `.where` used to raise
Expand All @@ -39,3 +83,33 @@ def test_case_mixed_dtype_branches_do_not_raise(engine):
# value-equal across engines (cuDF coerces to float -1.0; pandas keeps int -1)
assert [rec["id"] for rec in r.to_dict("records")] == [0, 1, 2]
assert all(float(rec["r"]) == -1.0 for rec in r.to_dict("records"))


@pytest.mark.parametrize("engine", FOUR_ENGINES)
@pytest.mark.parametrize(
("query", "expected"),
[
("CASE WHEN m.id = 1 THEN true ELSE 5 END", [5, True, 5]),
("CASE WHEN m.id = 1 THEN 5 ELSE 'x' END", ["x", 5, "x"]),
],
)
def test_incompatible_case_value_families_never_coerce(engine, query, expected):
if engine in {"cudf", "polars", "polars-gpu"}:
with pytest.raises(GFQLUnsupportedError) as exc:
_graph(engine).gfql(f"MATCH (m) RETURN {query} AS r", engine=engine)
assert exc.value.code == ErrorCode.E110
assert sorted(exc.value.context["value"]) in [["bool", "number"], ["number", "str"]]
return

r = _graph(engine).gfql(f"MATCH (m) RETURN {query} AS r", engine=engine)._nodes
assert r["r"].tolist() == expected


@pytest.mark.parametrize("engine", FOUR_ENGINES)
def test_numeric_case_families_still_conform(engine):
r = _graph(engine).gfql(
"MATCH (m) RETURN CASE WHEN m.id = 1 THEN 1 ELSE 2.5 END AS r", engine=engine
)._nodes
if hasattr(r, "to_pandas"):
r = r.to_pandas()
assert r["r"].tolist() == [2.5, 1.0, 2.5]
Loading