Skip to content

fix(backend): insert project_id filter before ORDER BY/GROUP BY/LIMIT#280

Merged
chiragSharmaD11 merged 6 commits intomainfrom
fix/query-builder-fix
Mar 9, 2026
Merged

fix(backend): insert project_id filter before ORDER BY/GROUP BY/LIMIT#280
chiragSharmaD11 merged 6 commits intomainfrom
fix/query-builder-fix

Conversation

@chiragSharmaD11
Copy link
Collaborator

Summary

Fix appendProjectId in QueryServiceImpl to insert the project_id filter before trailing SQL clauses (ORDER BY, GROUP BY, LIMIT) instead of blindly appending at the query end.

Context / Motivation

Queries with ORDER BY, GROUP BY, or LIMIT clauses were failing with Athena parse errors because AND project_id = '...' was appended after these clauses, producing invalid SQL like:

SELECT * FROM ... WHERE ... ORDER BY "timestamp" DESC AND project_id = 'fancode';

Error: mismatched input 'AND'. Expecting: ',', 'FETCH', 'LIMIT', 'NULLS', 'OFFSET', <EOF>

What Changed

  • Backend:
    • Refactored appendProjectId in QueryServiceImpl to find the earliest trailing clause position (ORDER BY, GROUP BY, LIMIT) and insert the project_id filter before it
    • Added findTrailingClausePosition helper with precompiled regex patterns
    • Added 5 new test cases in a @Nested AppendProjectId class covering ORDER BY, GROUP BY, LIMIT, combined clauses, and the no-trailing-clause fallback

Screenshots / Recordings (UI only)

  • N/A

appendProjectId was blindly appending AND project_id at the query end,
which produced invalid SQL when ORDER BY, GROUP BY, or LIMIT was present.

Made-with: Cursor
@github-actions github-actions bot added the backend Involves backend code changes label Mar 9, 2026
Copy link

@orca-security-in orca-security-in bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

Use WHERE instead of AND when the query has no existing WHERE clause,
preventing invalid SQL like "SELECT ... FROM table AND project_id = ..."

Made-with: Cursor
Call appendProjectId directly instead of going through submitQuery,
removing coupling to SqlQueryValidator and mock boilerplate.

Made-with: Cursor
chiragSharmaD11 and others added 3 commits March 10, 2026 00:23
…tion insertion

Extend TRAILING_CLAUSE_PATTERNS in both QueryServiceImpl.appendProjectId
and QueryTimestampEnricher.addWhereClause to cover HAVING, OFFSET, UNION,
INTERSECT, and EXCEPT — preventing filters from being appended after
these clauses and producing invalid SQL.

Made-with: Cursor
@chiragSharmaD11 chiragSharmaD11 merged commit f45d178 into main Mar 9, 2026
11 of 12 checks passed
@chiragSharmaD11 chiragSharmaD11 deleted the fix/query-builder-fix branch March 9, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Involves backend code changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants