Skip to content

dolthub/doltgresql#2373 fix VALUES subquery unwrap from dropping clauses#2402

Merged
Hydrocharged merged 3 commits intodolthub:mainfrom
codeaucafe:codeaucafe/2373-fix-subquery-values-projection-aggregate-distinct
Mar 4, 2026
Merged

dolthub/doltgresql#2373 fix VALUES subquery unwrap from dropping clauses#2402
Hydrocharged merged 3 commits intodolthub:mainfrom
codeaucafe:codeaucafe/2373-fix-subquery-values-projection-aggregate-distinct

Conversation

@codeaucafe
Copy link
Contributor

Summary

  • Replace overly permissive len(inSelect.From) == 1 guard in nodeAliasedTableExpr with isTrivialSelectStar, which only unwraps bare SELECT * FROM (VALUES ...)
    subqueries with no additional clauses (projections, LIMIT, ORDER BY, DISTINCT, WHERE, GROUP BY, HAVING)
  • Add *plan.Offset guard to TypeSanitizer to prevent OFFSET literals inside subqueries from being converted to Doltgres types, which caused GMS validateOffsetAndLimit
    to reject them with "invalid type: bigint"
  • Un-skip mixed-type VALUES-in-subquery tests and add mixed-type variants plus OFFSET-specific assertions

Closes: #2373

nodeAliasedTableExpr unconditionally unwrapped any subquery over
a VALUES clause when the inner SELECT had a single FROM item.
This discarded projections, LIMIT, ORDER BY, DISTINCT, WHERE,
GROUP BY, and HAVING because the guard only checked
len(inSelect.From) == 1.

Replace that check with isTrivialSelectStar, which only permits
unwrapping when the inner SELECT is a bare SELECT * with no
additional clauses. Non-trivial subqueries now correctly fall
through to the vitess.Subquery path, preserving all semantics.

Additionally, add a *plan.Offset guard to the TypeSanitizer so
OFFSET literals inside subqueries are kept as GMS types, matching
the existing *plan.Limit guard. Without this, OFFSET triggered
"invalid type: bigint" because GMS validateOffsetAndLimit only
recognizes its own type singletons.

Tests:
- Un-skip three mixed-type VALUES-in-subquery assertions
(projection, LIMIT, ORDER BY)
- Add mixed-type (int/decimal) variants to all integer-only
subquery test groups (DISTINCT, WHERE, GROUP BY, HAVING,
column aliasing, combined clauses, JOIN)
- Add OFFSET-only and LIMIT+OFFSET subquery assertions for
both integer and mixed-type VALUES

Refs: dolthub#2373
@codeaucafe codeaucafe force-pushed the codeaucafe/2373-fix-subquery-values-projection-aggregate-distinct branch from 078265c to d7b3469 Compare March 2, 2026 05:31
@codeaucafe codeaucafe marked this pull request as ready for review March 2, 2026 05:34
@codeaucafe
Copy link
Contributor Author

@Hydrocharged sorry to ping ya, but since this kinda corresponds to my last PR you assisted me on maybe you could review this or at least determine if this "fix" is on the right path by handling in analyzer and ast. Thanks in advance.

Inline isSelectStarOnly into isTrivialSelectStar. And, revert
TypeSanitizer comment to match the original, and instead updated
to mention both limit and offset literals.

Refs: dolthub#2373
@codeaucafe codeaucafe requested a review from Hydrocharged March 3, 2026 00:39
Copy link
Collaborator

@Hydrocharged Hydrocharged left a comment

Choose a reason for hiding this comment

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

LGTM!

@Hydrocharged Hydrocharged merged commit 06696a1 into dolthub:main Mar 4, 2026
19 of 20 checks passed
codeaucafe added a commit to codeaucafe/codeaucafe that referenced this pull request Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: VALUES inside subquery ignores projections, LIMIT, and ORDER BY

3 participants