feat($slice): array slicing operator, both arities#6
Merged
Conversation
Adds $slice to the expression ops table. Both Mongo forms compile to a jsonb_array_elements ... WITH ORDINALITY subquery re-aggregated with jsonb_agg(e order by ord) so element order is preserved: - [arr, n]: ord <= n for positive n; for negative n, last |n| via ord > jsonb_array_length(arr) + n. - [arr, skip, n]: start = greatest(case when skip < 0 then len + skip else skip end, 0), take ord in (start, start + n]. Negative skip counts from the end and clamps at 0, matching Mongo. Tests: 2 unit (SQL shape: ordinality window, ordered agg, skip/take bounds, params) + 2 integration in $project and $addFields covering first-n, last-n, n past length, zero n, skip+take, negative position, position past start. README expression list updated. 88 pass / 0 fail. tsc clean. Closes #2 Co-Authored-By: Claude Fable 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
Implements MongoDB's
$sliceexpression operator (#2), both forms:{ $slice: ["$arr", n] }— firstnelements, or last|n|whennis negative{ $slice: ["$arr", skip, n] }—nelements starting atskip; negativeskipcounts from the end and clamps at the start, matching Mongo semanticsBoth compile to a
jsonb_array_elements ... WITH ORDINALITYsubquery re-aggregated withjsonb_agg(e ORDER BY ord)so element order is preserved. All user-supplied values stay parameterized.Tests
$slicein$projectand$addFieldsagainst real Postgres: first-n, last-n, n past length, zero n, skip+take, negative position, position past starttsc --noEmitcleanREADME expression list updated.
Closes #2
🤖 Generated with Claude Code