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 exponentiation operator**: `^` now follows TCK power precedence, returns float values on pandas, cuDF, and Polars, and promotes the six numeric-precedence scenarios from expression debt.
- **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_power_operator_tck.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
11 changes: 7 additions & 4 deletions graphistry/compute/gfql/cypher/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,13 @@
| additive "+" multiplicative -> add_op
| additive MINUS multiplicative -> sub_op

?multiplicative: unary
| multiplicative "*" unary -> mul_op
| multiplicative "/" unary -> div_op
| multiplicative "%" unary -> mod_op
?multiplicative: power_expr
| multiplicative "*" power_expr -> mul_op
| multiplicative "/" power_expr -> div_op
| multiplicative "%" power_expr -> mod_op

?power_expr: unary
| power_expr "^" unary -> pow_op

?unary: "+" unary -> uplus
| MINUS unary -> uminus
Expand Down
14 changes: 10 additions & 4 deletions graphistry/compute/gfql/expr_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ def __init__(self, message: str, *, line: Optional[int] = None, column: Optional
| additive "+" multiplicative -> add_op
| additive MINUS multiplicative -> sub_op

?multiplicative: unary
| multiplicative "*" unary -> mul_op
| multiplicative "/" unary -> div_op
| multiplicative "%" unary -> mod_op
?multiplicative: power_expr
| multiplicative "*" power_expr -> mul_op
| multiplicative "/" power_expr -> div_op
| multiplicative "%" power_expr -> mod_op

?power_expr: unary
| power_expr "^" unary -> pow_op

?unary: "+" unary -> uplus
| MINUS unary -> uminus
Expand Down Expand Up @@ -733,6 +736,9 @@ def div_op(self, items: Sequence[Any]) -> BinaryOp:
def mod_op(self, items: Sequence[Any]) -> BinaryOp:
return self._bin("%", items)

def pow_op(self, items: Sequence[Any]) -> BinaryOp: # hygiene-ok: explicit-any -- Lark transformer callback, file-wide convention
return self._bin("^", items)

def cmp_op(self, items: Sequence[Any]) -> BinaryOp:
op = ""
for item in items:
Expand Down
2 changes: 1 addition & 1 deletion graphistry/compute/gfql/language_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
GFQL_INEQUALITY_EQUALITY_COMPARISON_BINARY_OPS: Final[frozenset[str]] = frozenset({"!=", "<>"})
GFQL_ORDERED_COMPARISON_BINARY_OPS: Final[frozenset[str]] = frozenset({"<", "<=", ">", ">="})
GFQL_STRING_PREDICATE_OPS: Final[frozenset[str]] = frozenset({"contains", "starts_with", "ends_with", "regex"})
GFQL_ARITHMETIC_BINARY_OPS: Final[frozenset[str]] = frozenset({"+", "-", "*", "/", "%"})
GFQL_ARITHMETIC_BINARY_OPS: Final[frozenset[str]] = frozenset({"+", "-", "*", "/", "%", "^"})
GFQL_ALLOWED_BINARY_OPS: Final[frozenset[str]] = (
GFQL_BOOLEAN_BINARY_OPS
| GFQL_COMPARISON_BINARY_OP_NAMES
Expand Down
9 changes: 9 additions & 0 deletions graphistry/compute/gfql/lazy/engine/polars/row_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,15 @@ def lower_expr(node: ExprNode, columns: Sequence[str]) -> Optional[pl.Expr]:
# ('PT6M'), so +/- would become string concatenation (silent wrong answer); pandas handles it.
if node.op in ("+", "-") and (_is_iso_duration_literal(node.left) or _is_iso_duration_literal(node.right)):
return None
if node.op == "^":
lhs = lower_expr(node.left, columns)
rhs = lower_expr(node.right, columns)
if lhs is None or rhs is None:
return None
ldt, rdt = _expr_output_dtype(lhs), _expr_output_dtype(rhs)
if ldt == pl.Boolean or rdt == pl.Boolean or _is_cross_type(ldt, rdt):
return None
return lhs.cast(pl.Float64) ** rhs.cast(pl.Float64)
# decline (NIE): ORDERING two ISO temporal constructor-string literals = lexicographic
# (wrong across timezones). Only literal-vs-literal ordering declines: =/<> are
# lexicographically correct, and literal-vs-real-string-column must NOT decline.
Expand Down
24 changes: 24 additions & 0 deletions graphistry/compute/gfql/row/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,30 @@ def _is_arith_node(operand_node: Any) -> bool: # hygiene-ok: explicit-any -- AS
if left is None or right is None:
return True, None
return True, left * right
if op == "^":
if left is None or right is None:
return True, None
if (hasattr(left, "astype") and RowPipelineMixin._gfql_series_bool_like(left)) or (
hasattr(right, "astype") and RowPipelineMixin._gfql_series_bool_like(right)
):
return False, None
if isinstance(left, bool) or isinstance(right, bool):
return False, None
left_kind = RowPipelineMixin._gfql_cypher_numeric_kind(left)
right_kind = RowPipelineMixin._gfql_cypher_numeric_kind(right)
if left_kind is None or right_kind is None:
return False, None
if hasattr(left, "astype"):
right_series = (
right
if hasattr(right, "astype")
else self._gfql_broadcast_scalar(table_df, right)
)
return True, left.astype(float) ** right_series.astype(float)
if hasattr(right, "astype"):
return True, self._gfql_broadcast_scalar(table_df, float(left)) ** right.astype(float)
scalar_power = pd.Series([float(left)], dtype="float64") ** float(right)
return True, float(scalar_power.iloc[0])
if op == "/":
if left is None or right is None:
return True, None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"MATCH (n) WHERE n.x = 1 AND (n.y = 2 AND n.z = 3) RETURN n",
"MATCH (n) WHERE n.x IN [1, 2, 3] RETURN n",
"MATCH (n) WHERE n.x + 1 > n.y * 2 RETURN n",
"MATCH (n) WHERE n.x ^ 2 + n.y * 3 > 8 RETURN n",
"MATCH (n) WHERE NOT (n)-[:BLOCKED]->() RETURN n",
# RETURN / projection
"MATCH (n) RETURN DISTINCT n.city AS city",
Expand Down
98 changes: 98 additions & 0 deletions graphistry/tests/compute/gfql/test_power_operator_tck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os

import pandas as pd
import pytest

import graphistry
from graphistry.compute.exceptions import GFQLValidationError


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

try:
import polars # noqa: F401
_HAS_POLARS = True
except Exception: # pragma: no cover
_HAS_POLARS = False

_ENGINES = ["pandas"]
if _HAS_CUDF and os.environ.get("TEST_CUDF") == "1":
_ENGINES.append("cudf")
if _HAS_POLARS:
_ENGINES.append("polars")
if _HAS_CUDF and os.environ.get("TEST_POLARS_GPU") == "1":
_ENGINES.append("polars-gpu")


@pytest.fixture(params=_ENGINES)
def engine(request):
if request.param == "cudf":
pytest.importorskip("cudf")
elif request.param == "polars-gpu":
pytest.importorskip("cudf_polars")
return request.param


def _graph(engine):
nodes = pd.DataFrame({"id": [0], "base": [2.0], "exp": [3.0], "nullable": [None], "flag": [True]})
edges = pd.DataFrame({"s": [0], "d": [1], "eid": [0]})
if engine in {"polars", "polars-gpu"}:
import polars as pl

nodes, edges = pl.from_pandas(nodes), pl.from_pandas(edges)
elif engine == "cudf":
import cudf

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


@pytest.mark.parametrize(
("query", "expected"),
[
("RETURN 4 ^ 3 * 2 ^ 3 AS a, (4 ^ 3) * (2 ^ 3) AS b, 4 ^ (3 * 2) ^ 3 AS c", [512.0, 512.0, 68719476736.0]),
("RETURN 4 ^ 3 / 2 ^ 3 AS a, (4 ^ 3) / (2 ^ 3) AS b, 4 ^ (3 / 2) ^ 3 AS c", [8.0, 8.0, 64.0]),
("RETURN 4 ^ 3 % 2 ^ 3 AS a, (4 ^ 3) % (2 ^ 3) AS b, 4 ^ (3 % 2) ^ 3 AS c", [0.0, 0.0, 64.0]),
("RETURN 4 ^ 3 + 2 ^ 3 AS a, (4 ^ 3) + (2 ^ 3) AS b, 4 ^ (3 + 2) ^ 3 AS c", [72.0, 72.0, 1073741824.0]),
("RETURN 4 ^ 3 - 2 ^ 3 AS a, (4 ^ 3) - (2 ^ 3) AS b, 4 ^ (3 - 2) ^ 3 AS c", [56.0, 56.0, 64.0]),
("RETURN -3 ^ 2 AS a, (-3) ^ 2 AS b, -(3 ^ 2) AS c", [9.0, 9.0, -9.0]),
],
)
def test_power_precedence_matches_tck(engine, query, expected):
row = _graph(engine).gfql(query, engine=engine)._nodes
if hasattr(row, "to_pandas"):
row = row.to_pandas()
values = row.iloc[0].tolist()
assert values == expected


def test_power_series_scalar_null_and_bool_paths(engine):
graph = _graph(engine)
if engine == "polars":
with pytest.raises((NotImplementedError, GFQLValidationError)):
graph.gfql("MATCH (n) RETURN n.flag ^ n.exp AS v", engine=engine)
return

result = graph.gfql("MATCH (n) RETURN n.base ^ n.exp AS a, 2 ^ n.exp AS b, n.base ^ 2 AS c", engine=engine)._nodes
if hasattr(result, "to_pandas"):
result = result.to_pandas()
assert result.iloc[0].tolist() == [8.0, 8.0, 4.0]

with pytest.raises((GFQLValidationError, NotImplementedError)):
graph.gfql("MATCH (n) RETURN n.flag ^ n.exp", engine=engine)


@pytest.mark.parametrize("engine", ["pandas", *(["polars"] if _HAS_POLARS else [])])
def test_power_scalar_float_edges_match_series(engine):
result = _graph(engine).gfql(
"RETURN 0.0 ^ -1.0 AS zero, (-2.0) ^ 0.5 AS negative",
engine=engine,
)._nodes
if hasattr(result, "to_pandas"):
result = result.to_pandas()
assert result.iloc[0]["zero"] == float("inf")
assert pd.isna(result.iloc[0]["negative"])
Loading