Skip to content

demo: complex ORM/non-ORM query showcase (CTE, UNION, LEFT JOIN)#7

Open
eitamring wants to merge 3 commits intomainfrom
demo/complex-query-patterns
Open

demo: complex ORM/non-ORM query showcase (CTE, UNION, LEFT JOIN)#7
eitamring wants to merge 3 commits intomainfrom
demo/complex-query-patterns

Conversation

@eitamring
Copy link
Copy Markdown
Contributor

@eitamring eitamring commented Mar 5, 2026

Purpose

Add a realistic complex-query intentional-failure showcase for both ORM and non-ORM paths, so contributors can validate parser/extractor behavior beyond simple statements.

Included Query Shapes

  • CTE (WITH ...)
  • UNION ALL
  • LEFT JOIN

Files

  • demo/complex-patterns/sql/schema.sql
  • demo/complex-patterns/sql/complex_queries.sql
  • demo/complex-patterns/python/orm_complex.py
  • demo/complex-patterns/python/raw_complex.py
  • demo/complex-patterns/README.md

Expected Findings (Intentional)

This folder is expected to produce findings, not pass cleanly:

  • VG004 (unbounded select): expected in CTE/UNION patterns without final limit
  • VG106 (unknown filter column): expected on LEFT JOIN filters using orders.ghost_status

Verified on this branch:

  • 10 total findings
  • 8x VG004
  • 2x VG106

ORM vs Non-ORM Coverage

  • ORM: SQLAlchemy query-builder forms (select(...), .query(...).union_all(...), .outerjoin(...))
  • Non-ORM: session.execute(text(...)) raw SQL with equivalent complex shapes

Extra Included

This branch also includes the already-proven CI compatibility commits:

  • reviewdog conversion JSON-shape compatibility
  • pinned VALK_GUARD_INSTALL_REF for reproducible CI behavior

Comment on lines +11 to +13
select(User.id.label("id"), User.email.label("email"))
.where(User.active.is_(True))
.cte("active_users")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
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

)

# Intentionally no LIMIT on outer query -> VG004 expected.
stmt = select(active_users.c.id, active_users.c.email).order_by(active_users.c.id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Origin: SQLAlchemy query builder | Query: SELECT "active_users"."c"."id", "active_users"."c"."email" FROM "active_users"



def orm_union_all_unbounded(session: Session):
q_active = session.query(User.id, User.email).filter(User.active.is_(True))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
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


def orm_union_all_unbounded(session: Session):
q_active = session.query(User.id, User.email).filter(User.active.is_(True))
q_inactive = session.query(User.id, User.email).filter(User.active.is_(False))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
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

Comment on lines +8 to +21
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Query: WITH active_users AS (

Comment on lines +25 to +37
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Query: SELECT users.id, users.email

Comment on lines +41 to +52
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()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚫 [valk-guard] reported by reviewdog 🐶
VG106: filter predicate column "ghost_status" not found in table "orders" schema; check predicate/group/order columns in schema/model mappings | Query: SELECT users.id, users.email, orders.status

Comment on lines +2 to +9
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Query: WITH active_users AS (

Comment on lines +12 to +18
SELECT users.id, users.email
FROM users
WHERE users.active = true
UNION ALL
SELECT users.id, users.email
FROM users
WHERE users.active = false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ [valk-guard] reported by reviewdog 🐶
VG004: SELECT without LIMIT may return unbounded rows; add LIMIT or FETCH FIRST | Query: SELECT users.id, users.email

Comment on lines +21 to +26
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚫 [valk-guard] reported by reviewdog 🐶
VG106: filter predicate column "ghost_status" not found in table "orders" schema; check predicate/group/order columns in schema/model mappings | Query: SELECT users.id, users.email, orders.status

@eitamring eitamring force-pushed the demo/complex-query-patterns branch from b56b54a to d24b782 Compare March 6, 2026 18:05
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.

1 participant