This commit is contained in:
Robert Schulze 2023-09-10 12:55:15 +00:00
parent 1ce84774f0
commit caadfe393b
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
7 changed files with 58 additions and 42 deletions

View File

@ -722,7 +722,7 @@ SELECT toDate('2016-12-27') AS date, toYearWeek(date) AS yearWeek0, toYearWeek(d
## toDaysSinceYearZero
Returns for a given date, the number of days passed since the [year 0](https://en.wikipedia.org/wiki/Year_zero).
Returns for a given date, the number of days passed since [1 January 0000](https://en.wikipedia.org/wiki/Year_zero) in the [proleptic Gregorian calendar defined by ISO 8601](https://en.wikipedia.org/wiki/Gregorian_calendar#Proleptic_Gregorian_calendar). The calculation is the same as in MySQL's [`TO_DAYS()`](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_to-days) function.
**Syntax**
@ -734,7 +734,7 @@ Aliases: `TO_DAYS`
**Arguments**
- `date` — The date to calculate the number of days passed since year zero from. [Date](../../sql-reference/data-types/date.md) or [Date64](../../sql-reference/data-types/date64.md).
- `date` — The date to calculate the number of days passed since year zero from. [Date](../../sql-reference/data-types/date.md) or [Date32](../../sql-reference/data-types/date32.md).
**Returned value**
@ -752,7 +752,7 @@ Result:
``` text
┌─toDaysSinceYearZero(toDate('2023-09-08')))─┐
│ 738772
│ 713569
└────────────────────────────────────────────┘
```

View File

@ -927,6 +927,12 @@ struct ToDayOfYearImpl
struct ToDaysSinceYearZeroImpl
{
private:
/// Constant calculated from MySQL's TO_DAYS() implementation.
/// https://github.com/mysql/mysql-server/blob/ea1efa9822d81044b726aab20c857d5e1b7e046a/mysys/my_time.cc#L1042
static constexpr auto DAYS_BETWEEN_YEARS_0_AND_1900 = 693'961; /// 01 January, each
public:
static constexpr auto name = "toDaysSinceYearZero";
static UInt32 execute(Int64, const DateLUTImpl &)
@ -939,11 +945,11 @@ struct ToDaysSinceYearZeroImpl
}
static UInt32 execute(Int32 d, const DateLUTImpl &)
{
return /* days between 0000-01-01 and 1970-01-01 */ 719'164 + d;
return DAYS_BETWEEN_YEARS_0_AND_1900 + d;
}
static UInt32 execute(UInt16 d, const DateLUTImpl &)
{
return /* days between 0000-01-01 and 1970-01-01 */ 719'164 + d;
return DAYS_BETWEEN_YEARS_0_AND_1900 + d;
}
static constexpr bool hasPreimage() { return false; }

View File

@ -19,7 +19,7 @@ namespace ErrorCodes
namespace
{
/** Returns number of days passed since 0001-01-01 */
/** Returns number of days passed since 0000-01-01 */
class FunctionToDaysSinceYearZero : public IFunction
{
using ResultType = DataTypeUInt32;
@ -65,7 +65,17 @@ public:
REGISTER_FUNCTION(ToDaysSinceYearZero)
{
factory.registerFunction<FunctionToDaysSinceYearZero>();
factory.registerFunction<FunctionToDaysSinceYearZero>(
FunctionDocumentation{
.description=R"(
Returns for a given date, the number of days passed since 1 January 0000 in the proleptic Gregorian calendar defined by ISO 8601.
The calculation is the same as in MySQL's TO_DAYS() function.
)",
.examples{
{"typical", "SELECT toDaysSinceYearZero(toDate('2023-09-08'))", "713569"}},
.categories{"Dates and Times"}
});
/// MySQL compatibility alias.
factory.registerAlias("TO_DAYS", FunctionToDaysSinceYearZero::name, FunctionFactory::CaseInsensitive);
}

View File

@ -1,13 +0,0 @@
Reject invalid parameters
Const argument
719164
738772
693597
738772
\N
Non-const argument
738772
738772
MySQL alias
738772
738772

View File

@ -1,22 +0,0 @@
SELECT 'Reject invalid parameters';
SELECT daysSinceYearZero(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
SELECT daysSinceYearZero(toDate('2023-09-08'), toDate('2023-09-08')); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
SELECT daysSinceYearZero('str'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT daysSinceYearZero(42); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT daysSinceYearZero(toDateTime('2023-09-08 11:11:11')); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT daysSinceYearZero(toDateTime64('2023-09-08 11:11:11', 3)); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT 'Const argument';
SELECT daysSinceYearZero(toDate('1970-01-01'));
SELECT daysSinceYearZero(toDate('2023-09-08'));
SELECT daysSinceYearZero(toDate32('1900-01-01'));
SELECT daysSinceYearZero(toDate32('2023-09-08'));
SELECT daysSinceYearZero(NULL);
SELECT 'Non-const argument';
SELECT daysSinceYearZero(materialize(toDate('2023-09-08')));
SELECT daysSinceYearZero(materialize(toDate32('2023-09-08')));
SELECT 'MySQL alias';
SELECT to_days(toDate('2023-09-08'));
SELECT TO_DAYS(toDate('2023-09-08'));

View File

@ -0,0 +1,13 @@
Invalid parameters
Const argument
693961
713569
668394
713569
\N
Non-const argument
713569
713569
MySQL alias
713569
713569

View File

@ -0,0 +1,22 @@
SELECT 'Invalid parameters';
SELECT toDaysSinceYearZero(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
SELECT toDaysSinceYearZero(toDate('2023-09-08'), toDate('2023-09-08')); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH }
SELECT toDaysSinceYearZero('str'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT toDaysSinceYearZero(42); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT toDaysSinceYearZero(toDateTime('2023-09-08 11:11:11')); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT toDaysSinceYearZero(toDateTime64('2023-09-08 11:11:11', 3)); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT }
SELECT 'Const argument';
SELECT toDaysSinceYearZero(toDate('1970-01-01'));
SELECT toDaysSinceYearZero(toDate('2023-09-08'));
SELECT toDaysSinceYearZero(toDate32('1900-01-01'));
SELECT toDaysSinceYearZero(toDate32('2023-09-08'));
SELECT toDaysSinceYearZero(NULL);
SELECT 'Non-const argument';
SELECT toDaysSinceYearZero(materialize(toDate('2023-09-08')));
SELECT toDaysSinceYearZero(materialize(toDate32('2023-09-08')));
SELECT 'MySQL alias';
SELECT to_days(toDate('2023-09-08'));
SELECT TO_DAYS(toDate('2023-09-08'));