Compile a condition and extract usable index conditions. Looks for variable-constant comparisons in the `condition` string involving the indexed columns whose variable names appear in `indexedcols`. The part of `condition` having usable indexes is returned as a compiled condition i
(
condition: str, typemap: dict[str, type], indexedcols: frozenset[str]
)
| 449 | |
| 450 | |
| 451 | def compile_condition( |
| 452 | condition: str, typemap: dict[str, type], indexedcols: frozenset[str] |
| 453 | ) -> CompiledCondition: |
| 454 | """Compile a condition and extract usable index conditions. |
| 455 | |
| 456 | Looks for variable-constant comparisons in the `condition` string |
| 457 | involving the indexed columns whose variable names appear in |
| 458 | `indexedcols`. The part of `condition` having usable indexes is |
| 459 | returned as a compiled condition in a `CompiledCondition` container. |
| 460 | |
| 461 | Expressions such as '0 < c1 <= 1' do not work as expected. The |
| 462 | Numexpr types of *all* variables must be given in the `typemap` |
| 463 | mapping. The ``function`` of the resulting `CompiledCondition` |
| 464 | instance is a Numexpr function object, and the ``parameters`` list |
| 465 | indicates the order of its parameters. |
| 466 | |
| 467 | """ |
| 468 | # Get the expression tree and extract index conditions. |
| 469 | expr = ne.necompiler.stringToExpression(condition, typemap, {}) |
| 470 | if expr.astKind != "bool": |
| 471 | raise TypeError( |
| 472 | "condition ``%s`` does not have a boolean type" % condition |
| 473 | ) |
| 474 | idxexprs = _get_idx_expr(expr, indexedcols) |
| 475 | # Post-process the answer |
| 476 | if isinstance(idxexprs, list): |
| 477 | # Simple expression |
| 478 | strexpr = ["e0"] |
| 479 | else: |
| 480 | # Complex expression |
| 481 | idxexprs, strexpr = idxexprs |
| 482 | # Get rid of the unnecessary list wrapper for strexpr |
| 483 | strexpr = strexpr[0] |
| 484 | |
| 485 | # Get the variable names used in the condition. |
| 486 | # At the same time, build its signature. |
| 487 | varnames = _get_variable_names(expr) |
| 488 | signature = [(var, typemap[var]) for var in varnames] |
| 489 | try: |
| 490 | # See the comments in `numexpr.evaluate()` for the |
| 491 | # reasons of inserting copy operators for unaligned, |
| 492 | # *unidimensional* arrays. |
| 493 | func = ne.necompiler.NumExpr(expr, signature) |
| 494 | except NotImplementedError as nie: |
| 495 | # Try to make this Numexpr error less cryptic. |
| 496 | raise _unsupported_operation_error(nie) |
| 497 | |
| 498 | _, ex_uses_vml = ne.necompiler.getExprNames(condition, {}) |
| 499 | kwargs = {"ex_uses_vml": ex_uses_vml} |
| 500 | |
| 501 | params = varnames |
| 502 | # This is more comfortable to handle about than a tuple. |
| 503 | return CompiledCondition(func, params, idxexprs, strexpr, **kwargs) |
| 504 | |
| 505 | |
| 506 | def call_on_recarr( |
no test coverage detected