fix(spp_cel_domain): handle relational fields and SQL JOINs in CEL#41
fix(spp_cel_domain): handle relational fields and SQL JOINs in CEL#41
Conversation
Summary of ChangesHello @jeremi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Common Expression Language (CEL) domain functionality by improving its ability to handle relational fields and generate accurate SQL queries. It allows 'one2many' and 'many2many' fields to be used intuitively as bare predicates, simplifying complex expressions. The changes also ensure that SQL queries involving related fields are correctly constructed with necessary JOINs, preventing data retrieval issues. Furthermore, the cache invalidation mechanism for CEL variables has been made more robust, guaranteeing that changes to variables are immediately reflected across all CEL components. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces important fixes and features, including correctly handling SQL generation for domains with related fields using query.select() in cel_executor.py and cel_sql_builder.py, enabling the use of relational fields (one2many, many2many) as bare predicates in CEL expressions via cel_translator.py, and enhancing cache invalidation in cel_variable.py. A security audit confirmed no vulnerabilities, highlighting the use of Odoo's SQL objects and SQL.identifier to prevent SQL injection, and robust security measures in the CEL parser against sandbox escape or denial-of-service attacks. The new tests are comprehensive, though one could be made more specific.
| found = False | ||
| for leaf in domain: | ||
| if isinstance(leaf, tuple) and leaf[0] == "program_membership_ids" and leaf[1] == "!=": | ||
| found = True | ||
| break | ||
| self.assertTrue(found, f"Expected '!= False' domain for one2many, got: {domain}") |
There was a problem hiding this comment.
The current test assertion is a bit weak as it only checks for the presence of the != operator but not the full domain leaf ('program_membership_ids', '!=', False). This could lead to a false positive if the generated domain was, for example, ('program_membership_ids', '!=', True).
To make the test more robust and specific, I suggest checking for the exact expected tuple within the domain. This can be done more concisely using any().
expected_leaf = ("program_membership_ids", "!=", False)
# The domain can be complex due to base_domains, so we check if any leaf matches.
found = any(leaf == expected_leaf for leaf in domain if isinstance(leaf, tuple))
self.assertTrue(found, f"Expected '{expected_leaf}' to be in domain, but got: {domain}")
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 19.0 #41 +/- ##
==========================================
- Coverage 71.31% 69.00% -2.32%
==========================================
Files 299 404 +105
Lines 23618 37746 +14128
==========================================
+ Hits 16844 26047 +9203
- Misses 6774 11699 +4925
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Handle one2many/many2many fields as bare CEL predicates - Use query.select() for SQL with JOINs instead of raw field access - Add tests for relational predicate handling
Replace weak operator-only check with an exact tuple match to prevent false positives from incorrect values (e.g. True instead of False).
dbf4c17 to
3b5c0ce
Compare
emjay0921
left a comment
There was a problem hiding this comment.
Review & Testing
Code Review
- Verified
query.select()is the correct Odoo 19 API for generating subqueries with JOINs — replaces hand-builtSELECT id FROM table WHERE ...that missed JOIN clauses for related fields. cel_sql_builder.pyandcel_executor.pychanges are consistent and correctly wrap the result inSQL("(%s)", select_sql)for subquery context.cel_translator.pybare predicate logic flow is clean: boolean → relational (one2many/many2many) → error for other types.- Cache invalidation in
cel_variable.pycorrectly broadened from resolver-only to all CEL caches viaspp.cel.service.invalidate_caches(). Selective invalidation viacache_invalidating_fieldsset is properly maintained.
Unit Tests
- Ran full
spp_cel_domaintest suite via Docker: 0 failed, 0 error(s) of 569 tests (including new tests from this PR).
Functional Testing (JSON-RPC against running instance)
- Bare one2many predicate (
program_membership_ids) — compiles to['program_membership_ids', '!=', False]✓ - Bare many2many predicate (
category_id) — compiles to['category_id', '!=', False]✓ - Boolean bare predicate (
active) — still works as['active', '=', True](regression check) ✓ - Related field with JOINs (
r.gender_id.uri == "...") — compiles to['gender_id.uri', '=', '...']correctly ✓ - Standard comparison (
r.is_group == true) — works as expected (regression check) ✓ - Invalid bare predicate (
name) — correctly rejects char field with clear error message ✓ - Cache invalidation CRUD cycle — create var(42) → compile → update(99) → compile reflects new value → delete → compile shows error ✓
- SQL evaluate path — expression evaluation works end-to-end ✓
Minor Notes (non-blocking)
many2onefields not handled as bare predicates (e.g.gender_idalone) — may be intentional scope limitation.- Method name
_invalidate_resolver_cacheis now slightly misleading since it invalidates all caches, not just resolver.
Summary
query.select()for SQL with JOINs instead of raw field access, fixing incorrect query generationOrigin
Cherry-picked from
openspp-modules-v2branchclaude/global-alliance-policy-basket.Test plan
Note
Medium Risk
Touches core query translation/execution paths (SQL generation and predicate compilation), which could change result sets or performance for existing expressions; mitigated by added regression tests.
Overview
Fixes CEL compilation and SQL fast-path generation for relational lookups.
Domains converted to SQL now use
query.select()(in bothCelExecutor._domain_to_id_sqlandSQLBuilder.select_ids_from_domain) so the generated subquery includes requiredFROM/JOINclauses for related-field domains (e.g.gender_id.uri) and still applies record rules even for empty domains.CEL translation now treats bare
one2many/many2manyfields as truthy predicates by compiling them to[(field, '!=', False)], and variable CRUD now invalidates all CEL caches viaspp.cel.service.invalidate_caches(); adds targeted tests covering relational predicates and cache invalidation behavior.Written by Cursor Bugbot for commit dbf4c17. This will update automatically on new commits. Configure here.