graflo.filter¶
Filter expression system for database queries.
This package provides a flexible system for creating and evaluating filter expressions that can be translated into different database query languages (AQL, Cypher, SQL, Python).
Key Components
- LogicalOperator: Logical operations (AND, OR, NOT, IMPLICATION / IF_THEN)
- ComparisonOperator: Comparison operations (==, !=, >, <, etc.)
- FilterExpression: Filter expression (leaf or composite logical formulae)
- parse_filter_expression: Unified YAML/JSON loader (Bindings, SelectSpec, graph DB)
- BoundParams: Values a rendered filter names by placeholder, for the driver to bind
SQL notes
- IF_THEN (IMPLICATION) renders as
(NOT antecedent OR consequent). - Use ExpressionFlavor.PYTHON for in-memory implication evaluation on documents.
Example
from graflo.filter import FilterExpression, parse_filter_expression expr = parse_filter_expression({ ... "AND": [ ... {"field": "age", "cmp_operator": ">=", "value": 18}, ... {"field": "status", "cmp_operator": "==", "value": "active"}, ... ] ... })
Modules:
| Name | Description |
|---|---|
onto |
Filter expression system for database queries. |
select |
Declarative SQL-like view specifications. |
sql |
SQL filter helpers built on top of FilterExpression. |
Attributes¶
ALL_BASE_COLUMNS = 'all_base'
module-attribute
¶
__all__ = ['ALL_BASE_COLUMNS', 'BoundParams', 'ComparisonOperator', 'FilterExpression', 'JoinClause', 'LogicalOperator', 'SelectSpec', 'parse_filter_expression', 'render_conjunct']
module-attribute
¶
Classes¶
BoundParams
¶
Values a rendered filter names by placeholder, for the driver to bind.
Pass one instance per query as params= to every filter rendered into
it, and the query's :attr:values to the driver: placeholder names run on
across filters. Only flavors whose drivers bind parameters take one; nGQL
and GSQL filters are written as escaped literals.
Source code in graflo/filter/onto.py
ComparisonOperator
¶
Bases: BaseEnum
Comparison operators for field comparisons.
Attributes:
| Name | Type | Description |
|---|---|---|
NEQ |
Not equal (!=) |
|
EQ |
Equal (==) |
|
GE |
Greater than or equal (>=) |
|
LE |
Less than or equal (<=) |
|
GT |
Greater than (>) |
|
LT |
Less than (<) |
|
IN |
Membership test (IN) |
|
IS_NULL |
Null check (IS NULL) |
|
IS_NOT_NULL |
Non-null check (IS NOT NULL) |
Source code in graflo/filter/onto.py
Attributes¶
EQ = '=='
class-attribute
instance-attribute
¶
GE = '>='
class-attribute
instance-attribute
¶
GT = '>'
class-attribute
instance-attribute
¶
IN = 'IN'
class-attribute
instance-attribute
¶
IS_NOT_NULL = 'IS_NOT_NULL'
class-attribute
instance-attribute
¶
IS_NULL = 'IS_NULL'
class-attribute
instance-attribute
¶
LE = '<='
class-attribute
instance-attribute
¶
LT = '<'
class-attribute
instance-attribute
¶
NEQ = '!='
class-attribute
instance-attribute
¶
FilterExpression
¶
Bases: ConfigBaseModel
Unified filter expression (discriminated: leaf or composite).
- kind="leaf": single field comparison (field, cmp_operator, value, optional unary_op).
- kind="composite": logical combination (operator AND/OR/NOT/IF_THEN, deps).
Source code in graflo/filter/onto.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 | |
Attributes¶
cmp_operator = None
class-attribute
instance-attribute
¶
deps = Field(default_factory=list)
class-attribute
instance-attribute
¶
field = None
class-attribute
instance-attribute
¶
kind
instance-attribute
¶
operator = None
class-attribute
instance-attribute
¶
unary_op = None
class-attribute
instance-attribute
¶
value = Field(default_factory=list)
class-attribute
instance-attribute
¶
Methods:¶
__call__(doc_name='doc', kind=ExpressionFlavor.AQL, **kwargs)
¶
Render or evaluate the expression in the target language.
With params= a :class:BoundParams, values are written as its
placeholders; without it, as literals.
Source code in graflo/filter/onto.py
check_discriminated_shape()
¶
Enforce exactly one shape per kind and normalise null-check operators.
Source code in graflo/filter/onto.py
from_dict(data)
classmethod
¶
Create a filter expression from a dictionary or list.
Returns FilterExpression (leaf or composite). LSP-compliant: return type is Self.
Source code in graflo/filter/onto.py
from_list(current)
classmethod
¶
Build a leaf expression from list form [cmp_operator, value, field?, unary_op?].
Source code in graflo/filter/onto.py
leaf_operator_to_unary_op(data)
classmethod
¶
Map leaf 'operator' (YAML/kwargs) to unary_op; infer kind and cmp_operator.
Source code in graflo/filter/onto.py
matches(doc)
¶
Whether doc satisfies the expression, evaluated in Python.
Prefer this to expr(kind=ExpressionFlavor.PYTHON, **doc), which
cannot evaluate a document with a field named kind or doc_name.
Source code in graflo/filter/onto.py
parse_deps(v)
classmethod
¶
Parse dict/list items into FilterExpression instances.
Source code in graflo/filter/onto.py
rename_fields(renames)
¶
This expression with every leaf field rewritten by renames.
Pure: returns a new expression and leaves this one untouched. Fields absent from renames keep their name.
Source code in graflo/filter/onto.py
value_to_list(v)
classmethod
¶
Convert single value to list if necessary. Explicit None becomes [None] for null comparison.
Source code in graflo/filter/onto.py
JoinClause
¶
Bases: ConfigBaseModel
Specification for a SQL JOIN operation.
Used by TableConnector to describe multi-table queries. Each JoinClause
adds one JOIN to the generated SQL. The base row uses TableConnector.base_alias
(default base), not a hard-coded name.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
str
|
Table name to join (e.g. "all_classes"). |
schema_name |
str | None
|
Optional schema override for the joined table. |
alias |
str | None
|
SQL alias for the joined table (e.g. "s", "t"). Required when the same table is joined more than once. |
on_self |
str
|
Column on the base (left) table used in the ON condition. |
on_other |
str
|
Column on the joined (right) table used in the ON condition. |
join_type |
str
|
Type of join -- LEFT, INNER, etc. Defaults to LEFT. |
select_fields |
list[str] | None
|
Explicit list of columns to SELECT from this join. When None every column of the joined table is included (aliased with the join alias prefix). |
Source code in graflo/filter/select.py
Attributes¶
alias = Field(default=None, description='SQL alias for the joined table.')
class-attribute
instance-attribute
¶
join_type = Field(default='LEFT', description='JOIN type (LEFT, INNER, etc.).')
class-attribute
instance-attribute
¶
on_other = Field(..., description='Column on the joined table for the ON condition.')
class-attribute
instance-attribute
¶
on_self = Field(..., description='Column on the base table for the ON condition.')
class-attribute
instance-attribute
¶
schema_name = Field(default=None, description='Schema override for the joined table.')
class-attribute
instance-attribute
¶
select_fields = Field(default=None, description='Columns to SELECT from this join (None = all columns).')
class-attribute
instance-attribute
¶
table = Field(..., description='Table name to join.')
class-attribute
instance-attribute
¶
LogicalOperator
¶
Bases: BaseEnum
Logical operators for combining filter conditions.
Attributes:
| Name | Type | Description |
|---|---|---|
AND |
Logical AND operation |
|
OR |
Logical OR operation |
|
NOT |
Logical NOT operation |
|
IMPLICATION |
Logical IF-THEN operation |
Source code in graflo/filter/onto.py
SelectSpec
¶
Bases: ConfigBaseModel
Declarative view specification emulating SQL SELECT structure.
Alternative to TableConnector's table_name + joins + filters. Supports from_dict() for YAML/JSON loading (like FilterExpression).
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['select', 'type_lookup']
|
"select" for full spec, "type_lookup" for shorthand |
from_ |
str | None
|
Base table (used when kind="select") |
joins |
list[JoinClause | dict[str, Any]]
|
JOIN clauses (used when kind="select") |
select |
list[str | dict[str, Any]]
|
SELECT list (used when kind="select"). Each entry may be:
- A string: |
where |
FilterExpression | dict[str, Any] | None
|
WHERE clause, reuses FilterExpression |
table |
str | None
|
Lookup table (type_lookup only) |
identity |
str | None
|
Identity column in lookup table (type_lookup only) |
type_column |
str | None
|
Type discriminator column (type_lookup only) |
source |
str | None
|
FK column on base table for source (type_lookup only) |
target |
str | None
|
FK column on base table for target (type_lookup only) |
relation |
str | None
|
Relation column in base table (type_lookup only, optional) |
source_table |
str | None
|
Lookup table for the source side (defaults to table). |
target_table |
str | None
|
Lookup table for the target side (defaults to table). |
source_identity |
str | None
|
Join column on the source lookup (defaults to identity). |
target_identity |
str | None
|
Join column on the target lookup (defaults to identity). |
source_type_column |
str | None
|
Type discriminator on source lookup (defaults to type_column). |
target_type_column |
str | None
|
Type discriminator on target lookup (defaults to type_column). |
base_alias |
str
|
SQL alias for the base table row when joins are present
( |
Source code in graflo/filter/select.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
Attributes¶
base_alias = Field(default='base', description='SQL table alias for the base row (when joins are generated).')
class-attribute
instance-attribute
¶
from_ = Field(default=None, validation_alias='from')
class-attribute
instance-attribute
¶
identity = None
class-attribute
instance-attribute
¶
joins = Field(default_factory=list)
class-attribute
instance-attribute
¶
kind = 'select'
class-attribute
instance-attribute
¶
relation = None
class-attribute
instance-attribute
¶
select = Field(default_factory=_default_select_columns, description='Default all_base expands to base.* when joins exist, else SELECT *.')
class-attribute
instance-attribute
¶
source = None
class-attribute
instance-attribute
¶
source_identity = None
class-attribute
instance-attribute
¶
source_table = None
class-attribute
instance-attribute
¶
source_type_column = None
class-attribute
instance-attribute
¶
table = None
class-attribute
instance-attribute
¶
target = None
class-attribute
instance-attribute
¶
target_identity = None
class-attribute
instance-attribute
¶
target_table = None
class-attribute
instance-attribute
¶
target_type_column = None
class-attribute
instance-attribute
¶
type_column = None
class-attribute
instance-attribute
¶
where = None
class-attribute
instance-attribute
¶
Methods:¶
build_sql(schema, base_table, extra_conditions=())
¶
Build SQL SELECT query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str
|
Schema name (e.g. "public") |
required |
base_table
|
str
|
Base table name (from TableConnector.table_name) |
required |
extra_conditions
|
Sequence[str]
|
Rendered conditions to AND onto the view's own
|
()
|
Returns:
| Type | Description |
|---|---|
str
|
Complete SQL query string |
Source code in graflo/filter/select.py
concat_select_parts(head, *tail)
classmethod
¶
Merge kind="select" join/select fragments into one spec.
head may set from_ and where. Each tail fragment must omit
from_ and where so the logical base table and filters stay on
head. joins and select are concatenated in order.
Omitting from on the merged spec is fine: :meth:build_sql uses
TableConnector.table_name as the base when from_ is unset.
This is the structured equivalent of listing two join/select blocks in
one YAML view: select, described in
docs/concepts/connectors/table_views.md.
Source code in graflo/filter/select.py
effective_base_alias()
¶
The alias the generated query gives the base table, or None.
A type_lookup view always joins, so it always aliases. A select
view aliases the base table only when it has joins; conditions added to
a join-less view must name columns unqualified.
Source code in graflo/filter/select.py
from_dict(data)
classmethod
¶
Create SelectSpec from dictionary (YAML/JSON friendly).
Supports:
- kind="type_lookup": table, identity, type_column, source, target, relation,
optional per-side source_* / target_* lookup overrides
- kind="select": from, joins, select (all_base, strings, base/from_join
dicts, or legacy expr/alias dicts), where, base_alias
Source code in graflo/filter/select.py
where_expression()
¶
The where clause as a filter expression, or None.
Functions:¶
parse_filter_expression(raw)
¶
Parse YAML/JSON/dict/list/filter model into a :class:FilterExpression.
Uses :meth:FilterExpression.from_dict for logical shorthand (OR, AND,
NOT, IF_THEN keys) and leaf shorthand; uses model_validate for
discriminated kind / operator + deps payloads.
Source code in graflo/filter/onto.py
render_conjunct(expr, *, kind, doc_name='doc', **kwargs)
¶
Render expr as one operand of an AND of conditions.
Callers that collect several conditions and join them with AND must
render each through this function. Every target language binds AND
tighter than OR, so a bare a OR b beside c would mean
a OR (b AND c); an OR condition is therefore parenthesised. AND
and NOT conditions and single comparisons keep their meaning unwrapped,
and IF_THEN renders its own parentheses.