Skip to content

Commit 654dc82

Browse files
Merge pull request ClickHouse#75735 from yariks5s/time/time64_v2
Implement Time and Time64 data types, ClickHouse#71943
2 parents f497100 + dc7ea2b commit 654dc82

File tree

111 files changed

+5665
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+5665
-142
lines changed

base/base/Decimal.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace DB
1010
{
1111
template <class> struct Decimal;
1212
class DateTime64;
13+
class Time64;
1314

1415
#define FOR_EACH_UNDERLYING_DECIMAL_TYPE(M) \
1516
M(Int32) \
@@ -142,6 +143,16 @@ class DateTime64 : public Decimal64
142143

143144
constexpr DateTime64(const Base & v): Base(v) {} // NOLINT(google-explicit-constructor)
144145
};
146+
147+
class Time64 : public Decimal64
148+
{
149+
public:
150+
using Base = Decimal64;
151+
using Base::Base;
152+
using NativeType = Base::NativeType;
153+
154+
constexpr Time64(const Base & v): Base(v) {} // NOLINT(google-explicit-constructor)
155+
};
145156
}
146157

147158
constexpr UInt64 max_uint_mask = std::numeric_limits<UInt64>::max();
@@ -173,6 +184,15 @@ namespace std
173184
}
174185
};
175186

187+
template <>
188+
struct hash<DB::Time64>
189+
{
190+
size_t operator()(const DB::Time64 & x) const
191+
{
192+
return std::hash<DB::Time64::NativeType>()(x);
193+
}
194+
};
195+
176196
template <>
177197
struct hash<DB::Decimal256>
178198
{

base/base/Decimal_fwd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ using Decimal128 = Decimal<Int128>;
2727
using Decimal256 = Decimal<Int256>;
2828

2929
class DateTime64;
30+
class Time64;
3031

3132
template <class T>
3233
concept is_decimal =
3334
std::is_same_v<T, Decimal32>
3435
|| std::is_same_v<T, Decimal64>
3536
|| std::is_same_v<T, Decimal128>
3637
|| std::is_same_v<T, Decimal256>
37-
|| std::is_same_v<T, DateTime64>;
38+
|| std::is_same_v<T, DateTime64>
39+
|| std::is_same_v<T, Time64>;
3840

3941
template <class T>
4042
concept is_over_big_int =

base/base/TypeName.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ TN_MAP(Decimal64)
4444
TN_MAP(Decimal128)
4545
TN_MAP(Decimal256)
4646
TN_MAP(DateTime64)
47+
TN_MAP(Time64)
4748
TN_MAP(Array)
4849
TN_MAP(Tuple)
4950
TN_MAP(Map)

base/base/extended_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ template <typename T> concept is_floating_point =
7474
M(DataTypeDate) \
7575
M(DataTypeDate32) \
7676
M(DataTypeDateTime) \
77+
M(DataTypeTime) \
7778
M(DataTypeInt8) \
7879
M(DataTypeUInt8) \
7980
M(DataTypeInt16) \
@@ -94,6 +95,7 @@ template <typename T> concept is_floating_point =
9495
M(DataTypeDate, X) \
9596
M(DataTypeDate32, X) \
9697
M(DataTypeDateTime, X) \
98+
M(DataTypeTime, X) \
9799
M(DataTypeInt8, X) \
98100
M(DataTypeUInt8, X) \
99101
M(DataTypeInt16, X) \

ci/jobs/scripts/check_style/aspell-ignore/en/aspell-dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,7 @@ toStartOfYear
30453045
toString
30463046
toStringCutToZero
30473047
toTime
3048+
toTimeWithFixedDate
30483049
toTimeZone
30493050
toType
30503051
toTypeName

docs/en/sql-reference/data-types/datetime.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Resolution: 1 second.
2323

2424
## Speed {#speed}
2525

26-
The `Date` datatype is faster than `DateTime` under _most_ conditions.
26+
The `Date` data type is faster than `DateTime` under _most_ conditions.
2727

2828
The `Date` type requires 2 bytes of storage, while `DateTime` requires 4. However, when the database compresses the database, this difference is amplified. This amplification is due to the minutes and seconds in `DateTime` being less compressible. Filtering and aggregating `Date` instead of `DateTime` is also faster.
2929

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
description: 'Documentation for the Time data type in ClickHouse, which stores
3+
the time range with second precision'
4+
slug: /sql-reference/data-types/time
5+
sidebar_position: 15
6+
sidebar_label: 'Time'
7+
title: 'Time'
8+
---
9+
10+
# Time
11+
12+
The `Time` data type is used to store a time value independent of any calendar date. It is ideal for representing daily schedules, event times, or any situation where only the time component (hours, minutes, seconds) is important.
13+
14+
Syntax:
15+
16+
``` sql
17+
Time()
18+
```
19+
20+
Supported range of values: \[-999:59:59, 999:59:59\].
21+
22+
Resolution: 1 second.
23+
24+
## Speed {#speed}
25+
26+
The `Date` data type is faster than `Time` under _most_ conditions. But the `Time` data type is around the same as `DateTime` data type.
27+
28+
Due to the implementation details, the `Time` and `DateTime` type requires 4 bytes of storage, while `Date` requires 2 bytes. However, when the database compresses the database, this difference is amplified.
29+
30+
## Usage Remarks {#usage-remarks}
31+
32+
The point in time is saved as a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time), regardless of the time zone or daylight saving time.
33+
34+
**Note:** The Time data type does not observe time zones. It represents a time‐of‐day value on its own, without any date or regional offset context. Attempting to apply or change a time zone on Time columns has no effect and is not supported.
35+
36+
## Examples {#examples}
37+
38+
**1.** Creating a table with a `Time`-type column and inserting data into it:
39+
40+
``` sql
41+
CREATE TABLE dt
42+
(
43+
`time` Time,
44+
`event_id` UInt8
45+
)
46+
ENGINE = TinyLog;
47+
```
48+
49+
``` sql
50+
-- Parse Time
51+
-- - from string,
52+
-- - from integer interpreted as number of seconds since 1970-01-01.
53+
INSERT INTO dt VALUES ('100:00:00', 1), (12453, 3);
54+
55+
SELECT * FROM dt;
56+
```
57+
58+
``` text
59+
┌──────time─┬─event_id─┐
60+
1. │ 100:00:00 │ 1 │
61+
2. │ 003:27:33 │ 3 │
62+
└───────────┴──────────┘
63+
```
64+
65+
**2.** Filtering on `Time` values
66+
67+
``` sql
68+
SELECT * FROM dt WHERE time = toTime('100:00:00')
69+
```
70+
71+
``` text
72+
┌──────time─┬─event_id─┐
73+
1. │ 100:00:00 │ 1 │
74+
└───────────┴──────────┘
75+
```
76+
77+
`Time` column values can be filtered using a string value in `WHERE` predicate. It will be converted to `Time` automatically:
78+
79+
``` sql
80+
SELECT * FROM dt WHERE time = '100:00:00'
81+
```
82+
83+
``` text
84+
┌──────time─┬─event_id─┐
85+
1. │ 100:00:00 │ 1 │
86+
└───────────┴──────────┘
87+
```
88+
89+
**3.** Getting a time zone for a `Time`-type column:
90+
91+
``` sql
92+
SELECT toTime(now()) AS column, toTypeName(column) AS x
93+
```
94+
95+
``` text
96+
┌────column─┬─x────┐
97+
1. │ 018:55:15 │ Time │
98+
└───────────┴──────┘
99+
```
100+
101+
102+
## See Also {#see-also}
103+
104+
- [Type conversion functions](../functions/type-conversion-functions.md)
105+
- [Functions for working with dates and times](../functions/date-time-functions.md)
106+
- [Functions for working with arrays](../functions/array-functions.md)
107+
- [The `date_time_input_format` setting](../../operations/settings/settings-formats.md#date_time_input_format)
108+
- [The `date_time_output_format` setting](../../operations/settings/settings-formats.md#date_time_output_format)
109+
- [The `timezone` server configuration parameter](../../operations/server-configuration-parameters/settings.md#timezone)
110+
- [The `session_timezone` setting](../../operations/settings/settings.md#session_timezone)
111+
- [The `DateTime` data type](datetime.md)
112+
- [The `Date` data type](date.md)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
description: 'Documentation for the Time64 data type in ClickHouse, which stores
3+
the time range with sub-second precision'
4+
slug: /sql-reference/data-types/time64
5+
sidebar_position: 17
6+
sidebar_label: 'Time64'
7+
title: 'Time64'
8+
---
9+
10+
# Time64
11+
12+
The Time64 data type allows storing time values with sub-second precision. Unlike DateTime64, it does not include a calendar date, but only represents time. The precision defines the resolution of stored values in fractional seconds.
13+
14+
Tick size (precision): 10<sup>-precision</sup> seconds. Valid range: [ 0 : 9 ].
15+
Typically, are used - 3 (milliseconds), 6 (microseconds), 9 (nanoseconds).
16+
17+
**Syntax:**
18+
19+
``` sql
20+
Time64(precision)
21+
```
22+
23+
Internally, Time64 stores data as an Int64 number of ticks since the start of the day (000:00:00.000000000). The tick resolution is determined by the precision parameter. Optionally, a time zone can be specified at the column level, which affects how time values are interpreted and displayed in text format.
24+
25+
Unlike DateTime64, Time64 does not store a date component, meaning that it only represents time. See details in [Time](../../sql-reference/data-types/time.md).
26+
27+
Supported range of values: \[000:00:00, 999:59:59.99999999\]
28+
29+
## Examples {#examples}
30+
31+
1. Creating a table with `Time64`-type column and inserting data into it:
32+
33+
``` sql
34+
CREATE TABLE t64
35+
(
36+
`timestamp` Time64(3),
37+
`event_id` UInt8
38+
)
39+
ENGINE = TinyLog;
40+
```
41+
42+
``` sql
43+
-- Parse Time
44+
-- - from integer interpreted as number of seconds since 1970-01-01.
45+
-- - from string,
46+
INSERT INTO t64 VALUES (15463123, 1), (154600.123, 2), ('100:00:00', 3);
47+
48+
SELECT * FROM t64;
49+
```
50+
51+
``` text
52+
┌─────timestamp─┬─event_id─┐
53+
1. │ 004:17:43.123 │ 1 │
54+
2. │ 042:56:40.123 │ 2 │
55+
3. │ 100:00:00.000 │ 3 │
56+
└───────────────┴──────────┘
57+
```
58+
59+
2. Filtering on `Time64` values
60+
61+
``` sql
62+
SELECT * FROM t64 WHERE timestamp = toTime64('100:00:00', 3);
63+
```
64+
65+
``` text
66+
┌─────timestamp─┬─event_id─┐
67+
1. │ 100:00:00.000 │ 3 │
68+
└───────────────┴──────────┘
69+
```
70+
71+
Unlike `Time`, `Time64` values are not converted from `String` automatically.
72+
73+
``` sql
74+
SELECT * FROM t64 WHERE timestamp = toTime64(154600.123, 3);
75+
```
76+
77+
``` text
78+
┌─────timestamp─┬─event_id─┐
79+
1. │ 042:56:40.123 │ 2 │
80+
└───────────────┴──────────┘
81+
```
82+
83+
Contrary to inserting, the `toTime64` function will treat all values as the decimal variant, so precision needs to
84+
be given after the decimal point.
85+
86+
3. Getting a time zone for a `Time64`-type value:
87+
88+
``` sql
89+
SELECT toTime64(now(), 3) AS column, toTypeName(column) AS x;
90+
```
91+
92+
``` text
93+
┌────────column─┬─x─────────┐
94+
1. │ 019:14:16.000 │ Time64(3) │
95+
└───────────────┴───────────┘
96+
```
97+
98+
99+
**See Also**
100+
101+
- [Type conversion functions](../../sql-reference/functions/type-conversion-functions.md)
102+
- [Functions for working with dates and times](../../sql-reference/functions/date-time-functions.md)
103+
- [The `date_time_input_format` setting](../../operations/settings/settings-formats.md#date_time_input_format)
104+
- [The `date_time_output_format` setting](../../operations/settings/settings-formats.md#date_time_output_format)
105+
- [The `timezone` server configuration parameter](../../operations/server-configuration-parameters/settings.md#timezone)
106+
- [The `session_timezone` setting](../../operations/settings/settings.md#session_timezone)
107+
- [Operators for working with dates and times](../../sql-reference/operators/index.md#operators-for-working-with-dates-and-times)
108+
- [`Date` data type](../../sql-reference/data-types/date.md)
109+
- [`Time` data type](../../sql-reference/data-types/time.md)
110+
- [`DateTime` data type](../../sql-reference/data-types/datetime.md)

docs/en/sql-reference/functions/date-time-functions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,16 +1644,18 @@ Result:
16441644
**See Also**
16451645
- [date_trunc](#date_trunc)
16461646

1647-
## toTime {#totime}
1647+
## toTimeWithFixedDate {#totimewithfixeddate}
16481648

16491649
Converts a date with time to a certain fixed date, while preserving the time.
16501650

16511651
**Syntax**
16521652

16531653
```sql
1654-
toTime(date[,timezone])
1654+
toTimeWithFixedDate(date[,timezone])
16551655
```
16561656

1657+
Alias: `toTime` - can be used only when the `use_legacy_to_time` setting is enabled.
1658+
16571659
**Arguments**
16581660

16591661
- `date` — Date to convert to a time. [Date](../data-types/date.md)/[DateTime](../data-types/datetime.md)/[DateTime64](../data-types/datetime64.md).

src/Columns/ColumnDecimal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,5 +533,6 @@ template class ColumnDecimal<Decimal64>;
533533
template class ColumnDecimal<Decimal128>;
534534
template class ColumnDecimal<Decimal256>;
535535
template class ColumnDecimal<DateTime64>;
536+
template class ColumnDecimal<Time64>;
536537

537538
}

0 commit comments

Comments
 (0)