Skip to content

feat(schema)!: restrict grouped select to single-value expressions - #59

Merged
kudima03 merged 2 commits into
mainfrom
feat/grouped-select-single-value
Sep 24, 2026
Merged

kudima03 merged 2 commits into
mainfrom
feat/grouped-select-single-value

Conversation

@kudima03

@kudima03 kudima03 commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Summary

Refs #52. Covers the groupBy side: the schema now rejects a field that isn't grouped when it appears in select of a grouped query.

JSON Schema can't compare the fields in select with the fields in groupBy. So instead of trying to check that, this PR changes the query shape so the invalid case can't be written at all:

  • Schema: root dependentSchemas: when groupBy is present, every select item must be singleValueReturning. This works like dependentRequired from fix(schema): require groupBy when having is present #58 and needs no if/then. alias is still checked by the root selectExpression, because dependentSchemas adds to the root schema and doesn't replace it.
  • Semantics: the groupBy fields are output automatically as the first result columns, in groupBy order and named after the field. Next come the select entries. This matches LINQ, where .GroupBy(k).Select(g => ...) gives you g.Key plus aggregates over g.
  • Samples: removed the group-key fields from select in 07, 08, 12 and 18. Sample 12 loses its aliases on the group keys (customer_id, customer_name, membership_tier).
  • Docs: README, CLAUDE.md, and a breaking-change entry in CHANGELOG.md under [Unreleased].

Verification

All 21 samples validate. Scenario checks, using 08_having.json as the base:

Scenario Result
grouped, aggregates only valid
grouped, arithmetic over aggregate (multiply(sum, 0.05)) valid
grouped, scalar literal valid
grouped, group-key field repeated in select invalid
grouped, field that isn't grouped (status) invalid
grouped, eachMultiply column invalid
ungrouped, field + aggregate valid (broadcast semantics, to be documented separately)

Breaking change

Grouped queries that list any field or each* expression in select no longer validate. To migrate, delete the group-key fields from select; they now appear in the result automatically. The release needs a major bump.

Not in this PR

🤖 Generated with Claude Code

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>
@kudima03
kudima03 added this pull request to stack #62 September 24, 2026 14:54
@kudima03
kudima03 merged commit 67c8a88 into main Sep 24, 2026
1 check passed
@kudima03
kudima03 deleted the feat/grouped-select-single-value branch September 24, 2026 14:55
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