Skip to content

Commit

Permalink
Merge pull request #60644 from ClickHouse/revert-60281-54746_to_milli…
Browse files Browse the repository at this point in the history
…second_function

Revert "Add `toMillisecond` function"
  • Loading branch information
tavplubix committed Mar 1, 2024
2 parents 8607e74 + 5225fc6 commit 1b042be
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 177 deletions.
60 changes: 14 additions & 46 deletions docs/en/sql-reference/functions/date-time-functions.md
Expand Up @@ -394,7 +394,8 @@ Result:

## toYear

Returns the year component (AD) of a date or date with time.
Converts a date or date with time to the year number (AD) as `UInt16` value.


**Syntax**

Expand Down Expand Up @@ -430,7 +431,7 @@ Result:

## toQuarter

Returns the quarter (1-4) of a date or date with time.
Converts a date or date with time to the quarter number (1-4) as `UInt8` value.

**Syntax**

Expand Down Expand Up @@ -464,9 +465,10 @@ Result:
└──────────────────────────────────────────────┘
```


## toMonth

Returns the month component (1-12) of a date or date with time.
Converts a date or date with time to the month number (1-12) as `UInt8` value.

**Syntax**

Expand Down Expand Up @@ -502,7 +504,7 @@ Result:

## toDayOfYear

Returns the number of the day within the year (1-366) of a date or date with time.
Converts a date or date with time to the number of the day of the year (1-366) as `UInt16` value.

**Syntax**

Expand Down Expand Up @@ -538,7 +540,7 @@ Result:

## toDayOfMonth

Returns the number of the day within the month (1-31) of a date or date with time.
Converts a date or date with time to the number of the day in the month (1-31) as `UInt8` value.

**Syntax**

Expand Down Expand Up @@ -574,7 +576,7 @@ Result:

## toDayOfWeek

Returns the number of the day within the week of a date or date with time.
Converts a date or date with time to the number of the day in the week as `UInt8` value.

The two-argument form of `toDayOfWeek()` enables you to specify whether the week starts on Monday or Sunday, and whether the return value should be in the range from 0 to 6 or 1 to 7. If the mode argument is omitted, the default mode is 0. The time zone of the date can be specified as the third argument.

Expand Down Expand Up @@ -625,7 +627,7 @@ Result:

## toHour

Returns the hour component (0-24) of a date with time.
Converts a date with time to the number of the hour in 24-hour time (0-23) as `UInt8` value.

Assumes that if clocks are moved ahead, it is by one hour and occurs at 2 a.m., and if clocks are moved back, it is by one hour and occurs at 3 a.m. (which is not always exactly when it occurs - it depends on the timezone).

Expand All @@ -639,7 +641,7 @@ Alias: `HOUR`

**Arguments**

- `value` - a [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)
- `value` - a [Date](../data-types/date.md), [Date32](../data-types/date32.md), [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)

**Returned value**

Expand All @@ -663,7 +665,7 @@ Result:

## toMinute

Returns the minute component (0-59) a date with time.
Converts a date with time to the number of the minute of the hour (0-59) as `UInt8` value.

**Syntax**

Expand All @@ -675,7 +677,7 @@ Alias: `MINUTE`

**Arguments**

- `value` - a [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)
- `value` - a [Date](../data-types/date.md), [Date32](../data-types/date32.md), [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)

**Returned value**

Expand All @@ -699,7 +701,7 @@ Result:

## toSecond

Returns the second component (0-59) of a date with time. Leap seconds are not considered.
Converts a date with time to the second in the minute (0-59) as `UInt8` value. Leap seconds are not considered.

**Syntax**

Expand All @@ -711,7 +713,7 @@ Alias: `SECOND`

**Arguments**

- `value` - a [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)
- `value` - a [Date](../data-types/date.md), [Date32](../data-types/date32.md), [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)

**Returned value**

Expand All @@ -733,40 +735,6 @@ Result:
└─────────────────────────────────────────────┘
```

## toMillisecond

Returns the millisecond component (0-999) of a date with time.

**Syntax**

```sql
toMillisecond(value)
```

*Arguments**

- `value` - [DateTime](../data-types/datetime.md) or [DateTime64](../data-types/datetime64.md)

Alias: `MILLISECOND`

```sql
SELECT toMillisecond(toDateTime64('2023-04-21 10:20:30.456', 3))
```

Result:

```response
┌──toMillisecond(toDateTime64('2023-04-21 10:20:30.456', 3))─┐
│ 456 │
└────────────────────────────────────────────────────────────┘
```

**Returned value**

- The millisecond in the minute (0 - 59) of the given date/time

Type: `UInt16`

## toUnixTimestamp

Converts a string, a date or a date with time to the [Unix Timestamp](https://en.wikipedia.org/wiki/Unix_time) in `UInt32` representation.
Expand Down
40 changes: 8 additions & 32 deletions src/Common/DateLUTImpl.h
Expand Up @@ -3,13 +3,13 @@
#include <base/DayNum.h>
#include <base/defines.h>
#include <base/types.h>
#include <Core/DecimalFunctions.h>

#include <ctime>
#include <cassert>
#include <string>
#include <type_traits>


#define DATE_SECONDS_PER_DAY 86400 /// Number of seconds in a day, 60 * 60 * 24

#define DATE_LUT_MIN_YEAR 1900 /// 1900 since majority of financial organizations consider 1900 as an initial year.
Expand Down Expand Up @@ -280,9 +280,9 @@ class DateLUTImpl
static_assert(std::is_integral_v<DateOrTime> && std::is_integral_v<Divisor>);
assert(divisor > 0);

if (offset_is_whole_number_of_hours_during_epoch) [[likely]]
if (likely(offset_is_whole_number_of_hours_during_epoch))
{
if (x >= 0) [[likely]]
if (likely(x >= 0))
return static_cast<DateOrTime>(x / divisor * divisor);

/// Integer division for negative numbers rounds them towards zero (up).
Expand Down Expand Up @@ -576,10 +576,10 @@ class DateLUTImpl

unsigned toSecond(Time t) const
{
if (offset_is_whole_number_of_minutes_during_epoch) [[likely]]
if (likely(offset_is_whole_number_of_minutes_during_epoch))
{
Time res = t % 60;
if (res >= 0) [[likely]]
if (likely(res >= 0))
return static_cast<unsigned>(res);
return static_cast<unsigned>(res) + 60;
}
Expand All @@ -593,30 +593,6 @@ class DateLUTImpl
return time % 60;
}

template <typename DateOrTime>
unsigned toMillisecond(const DateOrTime & datetime, Int64 scale_multiplier) const
{
constexpr Int64 millisecond_multiplier = 1'000;
constexpr Int64 microsecond_multiplier = 1'000 * millisecond_multiplier;
constexpr Int64 divider = microsecond_multiplier / millisecond_multiplier;

auto components = DB::DecimalUtils::splitWithScaleMultiplier(datetime, scale_multiplier);

if (datetime.value < 0 && components.fractional)
{
components.fractional = scale_multiplier + (components.whole ? Int64(-1) : Int64(1)) * components.fractional;
--components.whole;
}
Int64 fractional = components.fractional;
if (scale_multiplier > microsecond_multiplier)
fractional = fractional / (scale_multiplier / microsecond_multiplier);
else if (scale_multiplier < microsecond_multiplier)
fractional = fractional * (microsecond_multiplier / scale_multiplier);

UInt16 millisecond = static_cast<UInt16>(fractional / divider);
return millisecond;
}

unsigned toMinute(Time t) const
{
if (t >= 0 && offset_is_whole_number_of_hours_during_epoch)
Expand Down Expand Up @@ -1146,9 +1122,9 @@ class DateLUTImpl
DateOrTime toStartOfMinuteInterval(DateOrTime t, UInt64 minutes) const
{
Int64 divisor = 60 * minutes;
if (offset_is_whole_number_of_minutes_during_epoch) [[likely]]
if (likely(offset_is_whole_number_of_minutes_during_epoch))
{
if (t >= 0) [[likely]]
if (likely(t >= 0))
return static_cast<DateOrTime>(t / divisor * divisor);
return static_cast<DateOrTime>((t + 1 - divisor) / divisor * divisor);
}
Expand Down Expand Up @@ -1363,7 +1339,7 @@ class DateLUTImpl

UInt8 saturateDayOfMonth(Int16 year, UInt8 month, UInt8 day_of_month) const
{
if (day_of_month <= 28) [[likely]]
if (likely(day_of_month <= 28))
return day_of_month;

UInt8 days_in_month = daysInMonth(year, month);
Expand Down
11 changes: 5 additions & 6 deletions src/Functions/DateTimeTransforms.cpp
Expand Up @@ -10,17 +10,16 @@ namespace ErrorCodes

void throwDateIsNotSupported(const char * name)
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal argument of type Date for function {}", name);
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date of argument for function {}", name);
}

void throwDate32IsNotSupported(const char * name)
void throwDateTimeIsNotSupported(const char * name)
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal argument of type Date32 for function {}", name);
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type DateTime of argument for function {}", name);
}

void throwDateTimeIsNotSupported(const char * name)
void throwDate32IsNotSupported(const char * name)
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal argument of type DateTime for function {}", name);
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type Date32 of argument for function {}", name);
}

}

0 comments on commit 1b042be

Please sign in to comment.