linrrzqqq commented on code in PR #56945:
URL: https://github.com/apache/doris/pull/56945#discussion_r2464196564
##########
be/src/vec/functions/function_date_or_datetime_computation.h:
##########
@@ -1378,5 +1379,94 @@ class FunctionGetFormat : public IFunction {
static constexpr auto TIME_NAME = "TIME";
};
+class PeriodHelper {
+public:
+ // For two digit year, 70-99 -> 1970-1999, 00-69 -> 2000-2069
+ // this rule is same as MySQL
+ static constexpr int YY_PART_YEAR = 70;
+ static Status valid_period(int64_t period) {
+ if (period <= 0 || (period % 100) == 0 || (period % 100) > 12) {
+ return Status::InvalidArgument("Period function got invalid
period: {}", period);
+ }
+ return Status::OK();
+ }
+
+ static int64_t check_and_convert_period_to_month(uint64_t period) {
+ THROW_IF_ERROR(valid_period(period));
+ uint64_t year = period / 100;
+ if (year < 100) {
+ year += (year >= YY_PART_YEAR) ? 1900 : 2000;
+ }
+ return year * 12LL + (period % 100) - 1;
Review Comment:
The -1 prevents December from causing the year to incorrectly overflow when
converting back, because month % 12 returns 0 to 11. The +1 converts the
internal 0-11 to readable 1-12.
example:
without +1 -1:
```text
// period -> month
202412 -> 2024 * 12 + 12 == 24300
//month -> period
24300 -> 24300/12 == 2025, 24300 % 12 ==0 -> 202500
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]