SQL Rewrite Tests [[suite_sql_rewrite_tests: TestSuite]]TestSuite
- locationtests/sql/rewrite/
- formatNNN-name.json with input, workspace, expected, expected_result
- test_count25
- implementations[sql_rewrite.rs]
- is_data_driventrue
- testsRun SQL Query
Description [[description: text]]textLSP SQL Integration Tests TestSuite 80%SQL Workspace Tests TestSuite 75%
Tests for SQL AST rewrite — automatic injection of __workspace filters into SQL queries.
Use SQL rewrite tests when:
- Verifying that SQL is correctly rewritten (AST rewrite)
- Testing complex SQL constructs (JOIN, CTE, subqueries, window functions)
- Checking that rewrite preserves semantics (DISTINCT, LEFT JOIN, aggregates)
- Validating that rewritten SQL executes correctly
Do not use for workspace isolation checks (use SQL workspace tests), LSP integration (use LSP SQL integration tests), or QMDC parsing (use parser microtests).
Testing Logic
Two levels of verification
-
String comparison — checks that SQL is correctly rewritten. Compares
expectedSQL with the rewrite result. -
Execution validation — checks that rewritten SQL works. If
expected_resultis specified, the SQL is executed against a test database populated frommulti-workspace-isolation/.
Test database
Uses parse_all_workspaces() to load data from:
tests/sql/multi-workspace-isolation/workspace1/(ws1: 1 Feature)tests/sql/multi-workspace-isolation/workspace2/(ws2: 2 Features)
Test Format
{
"input": "SELECT COUNT(*) FROM objects",
"workspace": "ws1",
"expected": "SELECT COUNT(*) FROM objects o WHERE o.__workspace = 'ws1'",
"expected_result": {
"columns": ["cnt"],
"rows": [[1]]
}
}
input— original SQL queryworkspace— workspace ID for filteringexpected— expected rewritten SQLexpected_result(optional) — expected execution result
Critical Checks
Semantics preservation
Rewrite must NOT change: DISTINCT in aggregates, aggregate type, GROUP BY logic, LEFT JOIN semantics (RLS in ON, not WHERE).
RLS rules
- All
objectsandedgestables get aalias.__workspace = 'ws1'filter - Aliases are mandatory
LEFT JOIN→ RLS inONclauseINNER JOIN→ RLS inONclause
Supported constructs
SELECT, UNION, EXCEPT, INTERSECT, subqueries (recursive), CTE (WITH, WITH RECURSIVE), JOIN (INNER, LEFT/RIGHT/FULL OUTER), window functions (OVER), CASE, IN, EXISTS, BETWEEN, LIKE.
How to Add a Test
- Create
NNN-name.jsonintests/sql/rewrite/ - Specify
input,workspace,expected - Optionally add
expected_resultfor execution validation - Run:
cd qmdc-rs && cargo test --test sql_rewrite