fix(salesforce): add Id and LastModifiedDate to SOQL rules unless they are selected #4880
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.48) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 507 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.373
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #4880"
general
weight: 0.35
ShivamSharma43
Relates to: no issue
This is a bug I found while reading the Salesforce client.
Before running a SOQL advanced sync rule,
SalesforceClientaddsIdandLastModifiedDateto theSELECTclause, becauseSalesforceDocMapper.map_salesforce_objectsbuilds_id,urland_timestampfrom them. Both helpers decide whether the field is already selected by searching the whole query:_add_last_modified_datechecks"lastmodifieddate" not in query.lower()_add_idchecksre.search(r"\bid\b", query.lower())So the field is skipped when it only appears outside the select list, or inside another field name. Some rules this affects, on
main:| Rule query | Query sent to Salesforce |
| --- | --- |
|
SELECT Id, Name FROM Account WHERE LastModifiedDate > LAST_N_DAYS:7| unchanged, noLastModifiedDateselected ||
SELECT Id, LastModifiedDate__c FROM Account| unchanged, noLastModifiedDateselected ||
SELECT Name, Owner.Id FROM Account|SELECT Name, Owner.Id, LastModifiedDate FROM Account, noIdselected ||
SELECT Name, (SELECT Id FROM Contacts) FROM Account| noIdselected ||
SELECT Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact)| noIdselected |Salesforce only returns the fields that are selected, so for these rules every document gets
_id: Noneand aurlending in/None, or a_timestampset to the sync time instead of the record's last modification. With a sync-time_timestamp, unchanged records are never skipped on later syncs.The fix adds a small
_split_select_clausehelper that walks the outerSELECTlist, skipping commas andFROMinside parentheses (subqueries and functions), and returns the top-level fields and the position of the outerFROM._add_idand_add_last_modified_datenow share_add_selected_field, which adds the field only when it is not one of those top-level fields (case-insensitive, as SOQL field names are), and inserts it before the outerFROM. Inserting there also stops the field from landing in aWHERE ... IN (SELECT ... FROM ...)subquery, which the old greedySELECT (.*) FROMsubstitution did. TheFIELDS(ALL)/FIELDS(STANDARD)checks are unchanged.Checklists
Pre-Review Checklist
config.yml.example)v7.13.2,v7.14.0,v8.0.0)Tests
In
tests/sources/test_salesforce.py, new cases for the existing parametrized tests:test_add_last_modified_date:LastModifiedDateonly inWHERE, aLastModifiedDate__cfield, andOwner.LastModifiedDatenow getLastModifiedDateadded; a lowercaselastmodifieddatein the select list andFIELDS(ALL)are left as they are.test_add_id:Owner.Id, anIdin a child subquery, and anIdonly in aWHERE ... IN (SELECT ...)semi-join now getIdadded before the outerFROM; a lowercaseidin the select list is left as it is.The 6 new add-the-field cases fail on
mainand pass with the fix. The rest oftest_salesforce.pyhas the same results before and after the change (on my Windows machine, three content-document tests fail onmaintoo because of the localbase64command).ruff checkandruff format --checkare clean, apart from the repo-wide CPY001 notice.🤖 Generated with Claude Code