Skip to content

[WIP] Fix prefix addition in where queries and xfiles options#12

Merged
warkanum merged 4 commits intomainfrom
copilot/fix-prefix-event-issue
Dec 30, 2025
Merged

[WIP] Fix prefix addition in where queries and xfiles options#12
warkanum merged 4 commits intomainfrom
copilot/fix-prefix-event-issue

Conversation

Copy link
Contributor

Copilot AI commented Dec 30, 2025

Fix incorrect table prefix addition in WHERE clauses

Problem

The AddTablePrefixToColumns function incorrectly adds table prefixes to SQL literals (like true, false, null) when they appear in complex WHERE clauses wrapped in parentheses, resulting in invalid SQL like "mastertask.true".

Example of the Bug

Input:  "(true AND status = 'active')"
Output: "mastertask.true AND status = 'active'"  ❌ INCORRECT

This causes SQL errors like:

ERROR: column mastertask.true does not exist (SQLSTATE=42703)

Root Cause

When processing "(true AND status = 'active')":

  1. stripOuterParentheses removes parentheses → "true AND status = 'active'"
  2. extractLeftSideOfComparison finds the = operator and extracts everything before it → "true AND status"
  3. The function treats "true AND status" as a column name and adds a prefix

Solution

Modified addPrefixToSingleCondition to:

  1. Strip only one level of outer parentheses at a time (using new stripOneOuterParentheses helper)
  2. After stripping, detect if there are multiple AND-separated conditions
  3. Split and recursively process each condition separately to avoid treating compound expressions as single column names
  4. Handle nested parentheses by recursively processing inner conditions
  5. Extract common parenthesis matching logic into stripOneMatchingOuterParen helper to reduce code duplication

After Fix

Input:  "(true AND status = 'active')"
Output: "(true AND mastertask.status = 'active')"  ✓ CORRECT

Validation Complete

  • Added comprehensive test suite TestAddTablePrefixToColumns_ComplexConditions with 9 test cases
  • All 21 existing common package tests pass ✓
  • All 38 restheadspec package tests pass ✓
  • Linter passes with no warnings ✓
  • Code review feedback addressed (refactored to reduce duplication) ✓
  • CodeQL security scan passes with 0 alerts ✓
  • Verified fix resolves the reported issue ✓

Security Summary

No security vulnerabilities detected by CodeQL analysis.

Impact

  • Fixes: ERROR: column mastertask.true does not exist
  • WHERE clauses with parenthesized conditions now work correctly
  • SQL literals (true, false, null, 1=1, etc.) are properly preserved in all contexts
  • No breaking changes to existing functionality
  • Works for both registered and unregistered models
Original prompt

This section details on the original issue you should resolve

<issue_title>Where queries and xfiles options add prefix event if the column name does not exit on the model.</issue_title>
<issue_description>Where queries and xfiles options add prefix event if the column name does not exit on the model.
common.AddTablePrefixToColumns incorrectly add prefixes. It must only add prefixes for columns found in the table or iets preloads.

ERROR logger/logger.go:131 Error counting records: ERROR: column mastertask.true does not exist (SQLSTATE=42703) {"process_id": 1709050}
github.com/bitechdev/ResolveSpec/pkg/logger.Error
/home/hein/work/git/api-server-utils/vendor/github.com/bitechdev/ResolveSpec/pkg/logger/logger.go:131
github.com/bitechdev/ResolveSpec/pkg/restheadspec.(*Handler).handleRead
/home/hein/work/git/api-server-utils/vendor/github.com/bitechdev/ResolveSpec/pkg/restheadspec/handler.go:578
github.com/bitechdev/ResolveSpec/pkg/restheadspec.(*Handler).Handle
/home/hein/work/git/api-server-utils/vendor/github.com/bitechdev/ResolveSpec/pkg/restheadspec/handler.go:143
github.com/bitechdev/ResolveSpec/pkg/restheadspec.createMuxHandler.func1
/home/hein/work/git/api-server-utils/vendor/github.com/bitechdev/ResolveSpec/pkg/restheadspec/restheadspec.go:173
net/http.HandlerFunc.ServeHTTP</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
@warkanum
Copy link
Member

@copilot If the model is not registered, isValidColumn must fail on all columns. Therefore not modify or add any prefixes. Calls to isValidColumn without a model must be logged and fixed.

Copilot AI and others added 2 commits December 30, 2025 13:31
…rrectly

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
@warkanum warkanum marked this pull request as ready for review December 30, 2025 13:37
@warkanum warkanum merged commit 41e4956 into main Dec 30, 2025
5 of 6 checks passed
@warkanum warkanum deleted the copilot/fix-prefix-event-issue branch December 30, 2025 13:38
Copilot AI restored the copilot/fix-prefix-event-issue branch December 30, 2025 13:38
@warkanum warkanum deleted the copilot/fix-prefix-event-issue branch December 30, 2025 13:38
Copilot AI requested a review from warkanum December 30, 2025 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Where queries and xfiles options add prefix event if the column name does not exit on the model.

2 participants