Repository navigation
Implement PERIOD_ADD and PERIOD_DIFF functions - #4294
Open
backendArchitect wants to merge 1 commit into
Open
backendArchitect wants to merge 1 commit into
backendArchitect wants to merge 1 commit into
Conversation
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.
Fixes: dolthub/dolt#12792
PERIOD_ADD(P, N)andPERIOD_DIFF(P1, P2)were missing, so queries likeSELECT PERIOD_DIFF(202612, 202601)failed withfunction: 'period_diff' not found. The reporter hit this in a WordPress plugin's retention report.This adds both functions in
sql/expression/function/period.go, next toMAKEDATE, following the MySQL 8.4 docs:Pis a period inYYMMorYYYYMMformat (a number, not a date). Two-digit years below 70 map to 20xx and the rest to 19xx, through the existingtypes.TwoDigitYear.PERIOD_ADDreturns aYYYYMMperiod, andPERIOD_ADD(0, N)returns 0.PERIOD_DIFFreturns the number of months between the two periods (it can be negative).evalInt64).Incorrect arguments to period_add. I added this assql.ErrWrongArguments, mapped to the MySQL error code, because no existing error kind matched it.PERIOD_ADDalso returns this error when the result would fall before year 0.Tests:
period_test.go: table-driven unit tests for both functions: the examples from the issue and the docs, year rollover, negative months, YYMM input, the 69/70 two-digit year boundary, 0, NULL, string arguments, and invalid months.function_queries.go: engine tests with the same values, plus aPERIOD_DIFF(DATE_FORMAT(...), DATE_FORMAT(...))query over derived tables (the shape from the report) and the two error cases.Without the
registry.goentries, the new engine tests fail withfunction: 'period_add' not found. With them,go test ./...passes.I don't have a MySQL server here, so the invalid-period handling comes from the docs and from how I understand MySQL's
valid_periodcheck. I haven't compared it against a live server. That covers erroring on month 0 or 13 and onPERIOD_DIFF(0, x), and returning 0 forPERIOD_ADD(0, N). I'm happy to change it if the team wants a more lenient behavior.