-
Notifications
You must be signed in to change notification settings - Fork 0
demo: complex ORM/non-ORM query showcase (CTE, UNION, LEFT JOIN) #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Complex Query Patterns (Intentional Violations) | ||
|
|
||
| This demo intentionally introduces issues in higher-complexity query shapes across both ORM and non-ORM paths. | ||
|
|
||
| Patterns covered: | ||
|
|
||
| - CTE (`WITH ...`) | ||
| - `UNION ALL` | ||
| - `LEFT JOIN` | ||
|
|
||
| Expected rule families: | ||
|
|
||
| - `VG004` (unbounded selects / missing limit) | ||
| - `VG106` (unknown filter column on joined table) | ||
|
|
||
| Files: | ||
|
|
||
| - `sql/complex_queries.sql` | ||
| - `python/orm_complex.py` | ||
| - `python/raw_complex.py` | ||
|
|
||
| Local verification on this branch currently yields: | ||
|
|
||
| - 8 findings of `VG004` | ||
| - 2 findings of `VG106` | ||
|
|
||
| Use this folder as a stress-case fixture for parser/extractor behavior and complex-query rule coverage. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| """Complex SQLAlchemy ORM query patterns with intentional violations.""" | ||
|
|
||
| from sqlalchemy import select, text | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from models import Order, User | ||
|
|
||
|
|
||
| def orm_cte_unbounded(session: Session): | ||
| active_users = ( | ||
| select(User.id.label("id"), User.email.label("email")) | ||
| .where(User.active.is_(True)) | ||
| .cte("active_users") | ||
| ) | ||
|
|
||
| # Intentionally no LIMIT on outer query -> VG004 expected. | ||
| stmt = select(active_users.c.id, active_users.c.email).order_by(active_users.c.id) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return session.execute(stmt).all() | ||
|
|
||
|
|
||
| def orm_union_all_unbounded(session: Session): | ||
| q_active = session.query(User.id, User.email).filter(User.active.is_(True)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| q_inactive = session.query(User.id, User.email).filter(User.active.is_(False)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| # Intentionally no LIMIT on union result -> VG004 expected. | ||
| return q_active.union_all(q_inactive).all() | ||
|
|
||
|
|
||
| def orm_left_join_unknown_filter(session: Session): | ||
| return ( | ||
| session.query(User.id, User.email, Order.status) | ||
| .outerjoin(Order, User.id == Order.user_id) | ||
| .filter(text("orders.ghost_status = 'pending'")) | ||
| .order_by(User.id) | ||
| .limit(100) | ||
| .all() | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """Complex raw SQL execution patterns with intentional violations.""" | ||
|
|
||
| from sqlalchemy import text | ||
| from sqlalchemy.orm import Session | ||
|
|
||
|
|
||
| def raw_cte_select_star_unbounded(session: Session): | ||
| return session.execute( | ||
| text( | ||
| """ | ||
| WITH active_users AS ( | ||
| SELECT * | ||
| FROM users | ||
| WHERE users.active = true | ||
| ) | ||
| SELECT active_users.id, active_users.email | ||
| FROM active_users | ||
| ORDER BY active_users.id | ||
| """ | ||
| ) | ||
| ).all() | ||
|
Comment on lines
+8
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def raw_union_all_unbounded(session: Session): | ||
| return session.execute( | ||
| text( | ||
| """ | ||
| SELECT users.id, users.email | ||
| FROM users | ||
| WHERE users.active = true | ||
| UNION ALL | ||
| SELECT users.id, users.email | ||
| FROM users | ||
| WHERE users.active = false | ||
| """ | ||
| ) | ||
| ).all() | ||
|
Comment on lines
+25
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
|
|
||
| def raw_left_join_unknown_filter(session: Session): | ||
| return session.execute( | ||
| text( | ||
| """ | ||
| SELECT users.id, users.email, orders.status | ||
| FROM users | ||
| LEFT JOIN orders ON users.id = orders.user_id | ||
| WHERE orders.ghost_status = 'pending' | ||
| ORDER BY users.id | ||
| LIMIT 100 | ||
| """ | ||
| ) | ||
| ).all() | ||
|
Comment on lines
+41
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| -- CTE: SELECT * + unbounded outer query (intentional violations). | ||
| WITH active_users AS ( | ||
| SELECT * | ||
| FROM users | ||
| WHERE users.active = true | ||
| ) | ||
| SELECT active_users.id, active_users.email | ||
| FROM active_users | ||
| ORDER BY active_users.id; | ||
|
Comment on lines
+2
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| -- UNION ALL: unbounded final result (intentional violation). | ||
| SELECT users.id, users.email | ||
| FROM users | ||
| WHERE users.active = true | ||
| UNION ALL | ||
| SELECT users.id, users.email | ||
| FROM users | ||
| WHERE users.active = false; | ||
|
Comment on lines
+12
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| -- LEFT JOIN: unknown filter column on joined table (intentional violation). | ||
| SELECT users.id, users.email, orders.status | ||
| FROM users | ||
| LEFT JOIN orders ON users.id = orders.user_id | ||
| WHERE orders.ghost_status = 'pending' | ||
| ORDER BY users.id | ||
| LIMIT 100; | ||
|
Comment on lines
+21
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [valk-guard] reported by reviewdog 🐶 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| CREATE TABLE users ( | ||
| id INTEGER PRIMARY KEY, | ||
| email TEXT NOT NULL, | ||
| active BOOLEAN NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE orders ( | ||
| id INTEGER PRIMARY KEY, | ||
| user_id INTEGER NOT NULL REFERENCES users(id), | ||
| status TEXT NOT NULL | ||
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Origin: SQLAlchemy query builder | Query:
SELECT "User"."id", "User"."email" FROM "User" WHERE 1=1