feat(schema)!: restrict grouped select to single-value expressions - #59
Merged
Merged
Conversation
When groupBy is present, select items must be singleValueReturning, enforced via a root dependentSchemas rule and a new singleValueSelectExpression definition. Group keys are emitted automatically as the leading result columns, so fields that aren't grouped and per-row each* columns can no longer appear next to aggregates in a grouped query. Remove group-key fields from select in samples 07, 08, 12 and 18, and document the rule in README, CLAUDE.md and CHANGELOG. BREAKING CHANGE: grouped queries that list groupBy fields (or any other field or each* expression) in select no longer validate. Refs #52 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dependentSchemas applies on top of the root select schema, so alias is already validated by selectExpression. The grouped select rule can reference singleValueReturning directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refs #52. Covers the
groupByside: the schema now rejects a field that isn't grouped when it appears inselectof a grouped query.JSON Schema can't compare the fields in
selectwith the fields ingroupBy. So instead of trying to check that, this PR changes the query shape so the invalid case can't be written at all:dependentSchemas: whengroupByis present, everyselectitem must besingleValueReturning. This works likedependentRequiredfrom fix(schema): require groupBy when having is present #58 and needs noif/then.aliasis still checked by the rootselectExpression, becausedependentSchemasadds to the root schema and doesn't replace it.groupByfields are output automatically as the first result columns, ingroupByorder and named after the field. Next come theselectentries. This matches LINQ, where.GroupBy(k).Select(g => ...)gives youg.Keyplus aggregates overg.selectin 07, 08, 12 and 18. Sample 12 loses its aliases on the group keys (customer_id,customer_name,membership_tier).CHANGELOG.mdunder[Unreleased].Verification
All 21 samples validate. Scenario checks, using
08_having.jsonas the base:multiply(sum, 0.05))selectstatus)eachMultiplycolumnBreaking change
Grouped queries that list any field or
each*expression inselectno longer validate. To migrate, delete the group-key fields fromselect; they now appear in the result automatically. The release needs a major bump.Not in this PR
selectwithoutgroupBy(the other half of Prevent mixing aggregate and non-aggregate select without groupBy at schema level #52). That's documentation only and will come in a follow-up.🤖 Generated with Claude Code