Repository navigation
Commit 2a2e9af
authored
Python: fix(sqlserver): handle None and boolean expressions in SqlServerCollection lambda parser (#14687)
### Summary
Closes #14683
`SqlServerCollection._lambda_parser` rendered `None` comparisons as
`[field] = ?` or `[field] <> ?` binding the string literal `'None'`.
Because SQL Server evaluates equality with `NULL` under `ANSI_NULLS ON`
as UNKNOWN, search filters targeting `None` could never match `NULL`
rows. In addition:
- `ast.Is` and `ast.IsNot` raised `NotImplementedError: Unsupported
operator: <class 'ast.Is'>` on `x.tenant is None` and `x.tenant is not
None`.
- Booleans were bound as string `'True'` / `'False'` instead of `'1'` /
`'0'` (which is expected by SQL Server `BIT` columns and is already used
by `_cast_value`).
- Omitted search arguments in `default_dynamic_filter_function`
generating `lambda x: x.tenant == None` bound `'None'` as a query
parameter.
### Changes
1. **`SqlServerCollection._lambda_parser`**:
- Inspect comparison operators `(ast.Eq, ast.NotEq, ast.Is, ast.IsNot)`.
- Normalize operands so if `left_node` is `ast.Constant` with `value is
None`, it swaps `left_node` and `right_node`.
- When comparing against `None`:
- If both sides are scalar constants, evaluate to T-SQL `(1 = 1)` or `(1
= 0)`.
- Otherwise, render `[column] IS NULL` (for `ast.Eq`, `ast.Is`) or
`[column] IS NOT NULL` (for `ast.NotEq`, `ast.IsNot`) without appending
any parameter to `command.parameters`.
- Raise `NotImplementedError` if `ast.Is` or `ast.IsNot` is used with a
non-`None` operand.
- In `case ast.Constant():`, bind booleans as `'1'` if `True` else `'0'`
to match `_cast_value` and SQL Server `BIT` semantics.
- Avoid double-parsing `right_node` across comparison operator branches.
2. **Unit Tests**:
- Added parameterized `test_null_filter_parser` in
`tests/unit/connectors/memory/test_sql_server.py` covering 28 cases
mirroring Postgres null filter parser tests (`== None`, `!= None`, `is
None`, `is not None`, reversed comparisons, constant truth evaluation,
compound boolean expressions, boolean bindings `'1'`/`'0'`, and string
`'None'`).
- Added `test_null_filter_query_parameter_order` verifying SELECT query
parameter alignment when combining null filters with vector and range
filters.
- Added `test_dynamic_null_filter` verifying
`default_dynamic_filter_function(tenant=None)` generates `[tenant] IS
NULL` without parameters.
- Added `test_unsupported_is_operator` verifying unsupported `is`
comparisons raise `NotImplementedError`.
### Verification
- `uv run pytest tests/unit/connectors/memory/test_sql_server.py` (73
passed in 0.16s).
- `uv run ruff check tests/unit/connectors/memory/test_sql_server.py
semantic_kernel/connectors/sql_server.py` (all checks passed).
- `uv run ruff format --check
tests/unit/connectors/memory/test_sql_server.py
semantic_kernel/connectors/sql_server.py` (all formatted).
Signed-off-by: Wasim <wasim143mr@gmail.com>1 parent 8cda8fe commit 2a2e9af
2 files changed
Lines changed: 138 additions & 14 deletions
File tree
- python
- semantic_kernel/connectors
- tests/unit/connectors/memory
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
574 | | - | |
| 574 | + | |
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
581 | 581 | | |
582 | | - | |
583 | 582 | | |
584 | 583 | | |
585 | 584 | | |
| |||
599 | 598 | | |
600 | 599 | | |
601 | 600 | | |
602 | | - | |
| 601 | + | |
603 | 602 | | |
604 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
605 | 622 | | |
606 | 623 | | |
607 | | - | |
608 | 624 | | |
609 | 625 | | |
610 | | - | |
611 | 626 | | |
612 | 627 | | |
613 | | - | |
614 | 628 | | |
615 | 629 | | |
616 | | - | |
617 | 630 | | |
618 | 631 | | |
619 | | - | |
620 | 632 | | |
621 | 633 | | |
622 | | - | |
623 | 634 | | |
624 | 635 | | |
625 | | - | |
626 | 636 | | |
627 | 637 | | |
628 | | - | |
629 | 638 | | |
630 | 639 | | |
631 | 640 | | |
| |||
659 | 668 | | |
660 | 669 | | |
661 | 670 | | |
662 | | - | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
663 | 675 | | |
664 | 676 | | |
665 | 677 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
25 | 32 | | |
26 | 33 | | |
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
| 37 | + | |
30 | 38 | | |
31 | 39 | | |
32 | 40 | | |
| |||
626 | 634 | | |
627 | 635 | | |
628 | 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 | + | |
0 commit comments