Skip to content

feat(agg): BIT/BOOL aggregates + FIRST_VALUE/LAST_VALUE/NTH_VALUE window helpers - #167

Merged
productdevbook merged 1 commit into
mainfrom
feat/bit-bool-window-value-builders
May 19, 2026
Merged

feat(agg): BIT/BOOL aggregates + FIRST_VALUE/LAST_VALUE/NTH_VALUE window helpers#167
productdevbook merged 1 commit into
mainfrom
feat/bit-bool-window-value-builders

Conversation

@productdevbook

Copy link
Copy Markdown
Owner

Summary

Adds eight typed builders for SQL aggregate and window functions that were missing from the public surface:

  • BIT aggregatesbitAnd, bitOr, bitXor (PG/MySQL/SQLite 3.44+ for AND/OR; MySQL-only for XOR)
  • BOOL aggregatesboolAnd, boolOr (PG and SQLite 3.45+; MySQL/MSSQL refuse — workaround is MIN/MAX(CAST(b AS int)))
  • Window-value helpersfirstValue, lastValue, nthValue (window-only — already in WINDOW_ONLY_FUNCTIONS; previously callers had to reach for sqlFn)

Per-dialect gating goes through four new feature flags (BIT_AGGREGATES, BIT_XOR_AGG, BOOL_AGGREGATES, NTH_VALUE_FN). Printer overrides surface the matrix at compile time so unsupported combos throw UnsupportedDialectFeatureError instead of leaking through to a runtime "no such function" parse error.

PG: also gates BIT_XOR (PG 14+ does ship it natively, but older PG versions surface a UDF-lookup error — PG 14+ users can bypass via sqlFn("BIT_XOR", expr)).

Files

  • src/builder/aggregate.ts — 8 new builders, all JSDoc-annotated with the dialect matrix and the LAST_VALUE frame-default footgun
  • src/dialect/features.ts — 4 new feature entries
  • src/printer/function-tables.ts — adds BIT_/BOOL_ to STANDARD_FUNCTIONS
  • src/printer/{pg,mysql,sqlite,mssql}.ts — gating overrides
  • src/builder/eb.ts, src/index.ts, src/ns/win.ts — re-exports (window helpers also mirrored into the win namespace alongside lag/lead)
  • docs/recipes.md — new subsections for bitwise/boolean aggregates and window-value functions

Test plan

  • pnpm fmt && pnpm lint && pnpm typecheck && pnpm vitest run — 2458 passed
  • test/builder/bit-bool-aggregates.test.ts — SQL output across all four dialects, refusal checks, PGlite roundtrip verifying the actual fold result
  • test/builder/window-value-fns.test.ts — SQL output, OVER-required check, MSSQL NTH_VALUE refusal, win namespace mirror, PGlite roundtrip for opening/closing/Nth values

Tradeoffs

  • BIT_XOR_AGG lists only MySQL even though PG 14+ has the function. sumak has no per-version gating today; the safe default is "refuse" with an sqlFn escape hatch documented in the JSDoc. Following the same pattern as the trigonometry feature flag.
  • BOOL_AND / BOOL_OR could have a portable rewrite to MIN(CAST(b AS int)) on MySQL/MSSQL but the result type and NULL semantics differ subtly (boolean vs int). Refuse + document the workaround keeps the builder surface honest.
  • firstValue/lastValue/nthValue are documented as window-only and the printer already enforces it via the existing WINDOW_ONLY_FUNCTIONS allowlist — no new gating needed for the OVER-required check.

🤖 Generated with Claude Code

…dow helpers

Adds five aggregate builders (bitAnd, bitOr, bitXor, boolAnd, boolOr) and
three window-value builders (firstValue, lastValue, nthValue) with
per-dialect gating via new feature flags BIT_AGGREGATES, BIT_XOR_AGG,
BOOL_AGGREGATES, NTH_VALUE_FN. Printer overrides refuse the unsupported
combos (MSSQL has no BIT_*/BOOL_*/NTH_VALUE; MySQL has no BOOL_*; SQLite
has no BIT_XOR; PG matrix excludes BIT_XOR pending per-version gating).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@productdevbook
productdevbook force-pushed the feat/bit-bool-window-value-builders branch from 88e966e to 4453944 Compare May 19, 2026 11:33
@productdevbook
productdevbook merged commit 3978c4f into main May 19, 2026
1 check passed
@productdevbook
productdevbook deleted the feat/bit-bool-window-value-builders branch May 19, 2026 11:35
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