-
Notifications
You must be signed in to change notification settings - Fork 218
feat: add collections validation and GFQL support #874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lmeyerov
wants to merge
20
commits into
master
Choose a base branch
from
feat/collections-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+890
−3
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6e9e6f3
chore: clarify plan location in template
lmeyerov 209b2a3
feat: add collections validation and gfql support
lmeyerov 673eed8
fix: satisfy mypy in collections validation
lmeyerov e1d0526
feat: add collections helper constructors
lmeyerov 858ff91
feat: wrap collection set expr to gfql chain
lmeyerov 6b5370c
Refine collections types and helpers
lmeyerov 04dd228
Fix collections typing for mypy
lmeyerov 322d152
Validate collections settings inputs
lmeyerov b170076
Simplify collections typing
lmeyerov 9064aee
Reuse gfql chain normalization in collections helpers
lmeyerov a7b2a2d
Refine collections gfql normalization for mypy
lmeyerov 8389026
Normalize collections GFQL via Chain and reject Let
lmeyerov 1144977
Avoid Chain.from_json in collections normalization
lmeyerov 4e889b2
Allow Let in collections normalization
lmeyerov 59ba9a3
Simplify collections gfql wrapping
lmeyerov 2988311
Slim collections validation helpers
lmeyerov 2f426d0
Simplify collections input parsing
lmeyerov 924f234
fix: canonicalize collections validation and encoding
lmeyerov 41a33c4
refactor: simplify collections normalization
lmeyerov 0de6944
chore: move collections notes to development
lmeyerov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| from typing import Dict, List, Optional, Sequence, TypeVar | ||
|
|
||
| from graphistry.models.collections import ( | ||
| CollectionIntersection, | ||
| CollectionExprInput, | ||
| CollectionSet, | ||
| ) | ||
| from graphistry.utils.json import JSONVal | ||
|
|
||
| CollectionDict = TypeVar("CollectionDict", CollectionSet, CollectionIntersection) | ||
|
|
||
|
|
||
| def _apply_collection_metadata(collection: CollectionDict, **metadata: Optional[str]) -> CollectionDict: | ||
| value = metadata.get("id") | ||
| if value is not None: | ||
| collection["id"] = value | ||
| value = metadata.get("name") | ||
| if value is not None: | ||
| collection["name"] = value | ||
| value = metadata.get("description") | ||
| if value is not None: | ||
| collection["description"] = value | ||
| value = metadata.get("node_color") | ||
| if value is not None: | ||
| collection["node_color"] = value | ||
| value = metadata.get("edge_color") | ||
| if value is not None: | ||
| collection["edge_color"] = value | ||
| return collection | ||
|
|
||
|
|
||
| def _wrap_gfql_expr(expr: CollectionExprInput) -> Dict[str, JSONVal]: | ||
|
|
||
| from graphistry.compute.ast import ASTObject, from_json as ast_from_json | ||
| from graphistry.compute.chain import Chain | ||
|
|
||
| def _normalize_op(op: object) -> Dict[str, JSONVal]: | ||
| if isinstance(op, ASTObject): | ||
| return op.to_json() | ||
| if isinstance(op, dict): | ||
| return ast_from_json(op, validate=True).to_json() | ||
| raise TypeError("Collection GFQL operations must be AST objects or dictionaries") | ||
|
|
||
| def _normalize_ops(raw: object) -> List[Dict[str, JSONVal]]: | ||
| if isinstance(raw, Chain): | ||
| return _normalize_ops(raw.to_json().get("chain", [])) | ||
| if isinstance(raw, ASTObject): | ||
| return [raw.to_json()] | ||
| if isinstance(raw, list): | ||
| if len(raw) == 0: | ||
| raise ValueError("Collection GFQL operations list cannot be empty") | ||
| return [_normalize_op(op) for op in raw] | ||
| if isinstance(raw, dict): | ||
| if raw.get("type") == "Chain" and "chain" in raw: | ||
| return _normalize_ops(raw.get("chain")) | ||
| if raw.get("type") == "gfql_chain" and "gfql" in raw: | ||
| return _normalize_ops(raw.get("gfql")) | ||
| if "chain" in raw: | ||
| return _normalize_ops(raw.get("chain")) | ||
| if "gfql" in raw: | ||
| return _normalize_ops(raw.get("gfql")) | ||
| return [_normalize_op(raw)] | ||
| raise TypeError("Collection expr must be an AST object, chain, list, or dict") | ||
|
|
||
| return {"type": "gfql_chain", "gfql": _normalize_ops(expr)} | ||
|
|
||
|
|
||
| def collection_set( | ||
| *, | ||
| expr: CollectionExprInput, | ||
| id: Optional[str] = None, | ||
| name: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| node_color: Optional[str] = None, | ||
| edge_color: Optional[str] = None, | ||
| ) -> CollectionSet: | ||
| """Build a collection dict for a GFQL-defined set.""" | ||
| collection: CollectionSet = {"type": "set", "expr": _wrap_gfql_expr(expr)} | ||
| return _apply_collection_metadata( | ||
| collection, | ||
| id=id, | ||
| name=name, | ||
| description=description, | ||
| node_color=node_color, | ||
| edge_color=edge_color, | ||
| ) | ||
|
|
||
|
|
||
| def collection_intersection( | ||
| *, | ||
| sets: Sequence[str], | ||
| id: Optional[str] = None, | ||
| name: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| node_color: Optional[str] = None, | ||
| edge_color: Optional[str] = None, | ||
| ) -> CollectionIntersection: | ||
| """Build a collection dict for an intersection of set IDs.""" | ||
| collection: CollectionIntersection = { | ||
| "type": "intersection", | ||
| "expr": { | ||
| "type": "intersection", | ||
| "sets": list(sets), | ||
| }, | ||
| } | ||
| return _apply_collection_metadata( | ||
| collection, | ||
| id=id, | ||
| name=name, | ||
| description=description, | ||
| node_color=node_color, | ||
| edge_color=edge_color, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict, List, TYPE_CHECKING, Union | ||
| from typing_extensions import Literal, NotRequired, Required, TypedDict | ||
|
|
||
| from graphistry.utils.json import JSONVal | ||
|
|
||
| if TYPE_CHECKING: | ||
| from graphistry.compute.ast import ASTObject | ||
| from graphistry.compute.chain import Chain | ||
|
|
||
|
|
||
| CollectionExprInput = Union[ | ||
| "Chain", | ||
| "ASTObject", | ||
| List["ASTObject"], | ||
| Dict[str, JSONVal], | ||
| List[Dict[str, JSONVal]], | ||
| ] | ||
|
|
||
|
|
||
| class IntersectionExpr(TypedDict): | ||
| type: Literal["intersection"] | ||
| sets: List[str] | ||
|
|
||
|
|
||
| class CollectionBase(TypedDict, total=False): | ||
| id: str | ||
| name: str | ||
| description: str | ||
| node_color: str | ||
| edge_color: str | ||
|
|
||
|
|
||
| class CollectionSet(CollectionBase): | ||
| type: NotRequired[Literal["set"]] | ||
| expr: Required[CollectionExprInput] | ||
|
|
||
|
|
||
| class CollectionIntersection(CollectionBase): | ||
| type: NotRequired[Literal["intersection"]] | ||
| expr: Required[IntersectionExpr] | ||
|
|
||
|
|
||
| Collection = Union[CollectionSet, CollectionIntersection] | ||
| CollectionsInput = Union[str, Collection, List[Collection]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.