repeat_row -- disallow on WITH ORDINALITY; add repeat_row_non_negative#36174
Open
ggevay wants to merge 3 commits intoMaterializeInc:mainfrom
Open
repeat_row -- disallow on WITH ORDINALITY; add repeat_row_non_negative#36174ggevay wants to merge 3 commits intoMaterializeInc:mainfrom
repeat_row -- disallow on WITH ORDINALITY; add repeat_row_non_negative#36174ggevay wants to merge 3 commits intoMaterializeInc:mainfrom
Conversation
Also disallow it in constructs that are implemented by WITH ORDINALITY under the hood: - ROWS FROM - implicit ROWS FROM, i.e., multiple table functions in a SELECT clause
3ede8a8 to
b441fcb
Compare
def-
reviewed
Apr 21, 2026
Contributor
def-
left a comment
There was a problem hiding this comment.
SELECT * FROM repeat_row_non_negative(9223372036854775807) WITH ORDINALITY LIMIT 1;Returns wrong result:
ordinality
------------
(0 rows)
Should be 1.
Contributor
Author
|
@def-, good catch! The bug is in It's coming from inside |
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.
This PR kicks off the
repeat_rowproductionization work stream. Resolves SQL-160. Resolves SQL-161.I suggest reviewing commit by commit:
repeat_row_non_negativeas a new SQL function, which errors out on a negative input (instead of producing a negative number of rows). It's less powerful thanrepeat_row, but its advantage is that it doesn't have the restrictions and dangers thatrepeat_rowhas. The immediate motivation for this was to be able to keep using this in tests ofWITH ORDINALITYwhenrepeat_rowis disallowed, but it might also be useful for users for other purposes, so users could be allowed to freely use it. (For now, it's guarded by a feature flag which defaults to off, and I haven't documented it yet, but if we think this would be indeed a useful function for users, then I can add docs and turn it on in a follow-up PR.)repeat_rowinWITH ORDINALITYand in some other constructs that are implemented usingWITH ORDINALITY. We need to make this change for productionizingrepeat_row, becauseWITH ORDINALITYis not well-defined for negative diffs, and the implementation panics on a negative diff. (Note that this is not violating backwards compatibility requirements, becauserepeat_rowwas not officially supported so far, and has not been turned on for most users.)Note: An alternative to disallowing
repeat_rowwithWITH ORDINALITYwould be to makeWithOrdinality::evalnot panic on negative diffs. However, this seems to be not easily possible without a performance hit or complicating the code: We'd need to eitherevalthe table function twice, only checking the diffs at the first evaluation, costing CPU;evalresult in aVec, costing memory;flat_mapclosure somehow fallible, for which I don't see an easy way. (E.g., we could make the iterator's item typeResult, but then would need to touch every caller ofTableFunc::evaland every arm of the big match inTableFunc::eval, and might slow things down a bit. Or we could report the error in anRc<Cell<...>>, which also feels convoluted, and potentially costing CPU.)We could reconsider making it possible generally to return errors in the middle of table function iterators, if we ever find a need for this in some other table function. But
repeat_rowtogether withWITH ORDINALITYfeels so rare, that it seems ok to just disallow this combination for now. Edit: https://github.com/MaterializeInc/database-issues/issues/11324 would be one more reason to implement error returns inside this iterator, but that issue seems also quite low priority.