From b412ea5f6d85c68d1cb246df2d17b93e8c95720a Mon Sep 17 00:00:00 2001 From: Roman Vasin Date: Mon, 25 Jul 2022 17:06:11 +0000 Subject: [PATCH] Improve generateRandom() for Date32; fix tests 01087_table_function_generate, 01277_fromUnixTimestamp64, 01691_DateTime64_clamp and 01702_toDateTime_from_string_clamping --- src/Common/DateLUTImpl.h | 4 +++- src/Functions/FunctionsConversion.h | 2 +- src/Storages/StorageGenerateRandom.cpp | 5 ++++- .../01087_table_function_generate.reference | 20 +++++++++---------- .../01277_fromUnixTimestamp64.reference | 4 ++-- .../0_stateless/01277_fromUnixTimestamp64.sql | 18 ++++++++--------- .../01691_DateTime64_clamp.reference | 8 ++++---- ..._toDateTime_from_string_clamping.reference | 6 +++--- .../01702_toDateTime_from_string_clamping.sql | 4 ++-- 9 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/Common/DateLUTImpl.h b/src/Common/DateLUTImpl.h index 56f2e87ccb5..fbbd29d7a2b 100644 --- a/src/Common/DateLUTImpl.h +++ b/src/Common/DateLUTImpl.h @@ -19,8 +19,10 @@ #define DATE_LUT_MAX (0xFFFFFFFFU - 86400) #define DATE_LUT_MAX_DAY_NUM 0xFFFF +#define DAYNUM_OFFSET_EPOCH 25567 + /// Max int value of Date32, DATE LUT cache size minus daynum_offset_epoch -#define DATE_LUT_MAX_EXTEND_DAY_NUM (DATE_LUT_SIZE - 25567) +#define DATE_LUT_MAX_EXTEND_DAY_NUM (DATE_LUT_SIZE - DAYNUM_OFFSET_EPOCH) /// A constant to add to time_t so every supported time point becomes non-negative and still has the same remainder of division by 3600. /// If we treat "remainder of division" operation in the sense of modular arithmetic (not like in C++). diff --git a/src/Functions/FunctionsConversion.h b/src/Functions/FunctionsConversion.h index af75e4f49ba..62f95031e58 100644 --- a/src/Functions/FunctionsConversion.h +++ b/src/Functions/FunctionsConversion.h @@ -538,7 +538,7 @@ template struct ConvertImplgetData().resize(limit); - fillBufferWithRandomData(reinterpret_cast(column->getData().data()), limit * sizeof(Int32), rng); + + for (size_t i = 0; i < limit; ++i) + column->getData()[i] = (rng() % static_cast(DATE_LUT_SIZE)) - DAYNUM_OFFSET_EPOCH; + return column; } case TypeIndex::UInt32: [[fallthrough]]; diff --git a/tests/queries/0_stateless/01087_table_function_generate.reference b/tests/queries/0_stateless/01087_table_function_generate.reference index d62ff5618fc..53792bfb579 100644 --- a/tests/queries/0_stateless/01087_table_function_generate.reference +++ b/tests/queries/0_stateless/01087_table_function_generate.reference @@ -70,16 +70,16 @@ DateTime64(3, \'UTC\') DateTime64(6, \'UTC\') DateTime64(6, \'UTC\') 1992-12-28 09:26:04.030 1971-07-29 06:20:38.230976 1980-03-26 15:49:55.428516 2051-12-11 07:09:13.162 1982-01-12 00:25:45.754492 2010-05-17 07:01:28.452864 Date32 -1934-01-06 -2039-08-16 -2103-11-03 -2064-08-14 -2187-08-21 -2099-04-08 -1947-06-22 -2012-01-19 -2170-07-09 -2263-01-17 +2120-04-24 +1908-10-02 +2105-09-04 +2129-03-23 +1921-04-05 +2020-04-14 +2251-12-25 +2266-03-27 +2161-02-18 +2172-07-24 - Float32 Float64 -1.3551149e32 1.2262973812461839e235 diff --git a/tests/queries/0_stateless/01277_fromUnixTimestamp64.reference b/tests/queries/0_stateless/01277_fromUnixTimestamp64.reference index a9ffd259af0..28006c1d168 100644 --- a/tests/queries/0_stateless/01277_fromUnixTimestamp64.reference +++ b/tests/queries/0_stateless/01277_fromUnixTimestamp64.reference @@ -4,6 +4,6 @@ Asia/Makassar 1234567891011 2009-02-14 07:31:31.011 1970-01-15 14:56:07.891011 1 non-const column 1234567891011 2009-02-13 23:31:31.011 1970-01-15 06:56:07.891011 1970-01-01 00:20:34.567891011 upper range bound -9904447342 2283-11-10 19:22:22.123 2283-11-10 19:22:22.123456 1925-01-01 00:00:00.413905173 +10413688942 2299-12-30 19:22:22.123 2299-12-30 19:22:22.123456 1900-01-01 00:00:00.413905173 lower range bound --1420066799 1925-01-01 01:00:00.877 1925-01-01 01:00:00.876544 1925-01-01 01:00:00.876543211 +-2208985199 1900-01-01 01:00:00.877 1900-01-01 01:00:00.876544 1900-01-01 01:00:00.876543211 diff --git a/tests/queries/0_stateless/01277_fromUnixTimestamp64.sql b/tests/queries/0_stateless/01277_fromUnixTimestamp64.sql index e76a4db7a27..846ffa094a5 100644 --- a/tests/queries/0_stateless/01277_fromUnixTimestamp64.sql +++ b/tests/queries/0_stateless/01277_fromUnixTimestamp64.sql @@ -46,10 +46,10 @@ SELECT SELECT 'upper range bound'; WITH - 9904447342 AS timestamp, - CAST(9904447342123 AS Int64) AS milli, - CAST(9904447342123456 AS Int64) AS micro, - CAST(9904447342123456789 AS Int64) AS nano, + 10413688942 AS timestamp, + CAST(10413688942123 AS Int64) AS milli, + CAST(10413688942123456 AS Int64) AS micro, + CAST(10413688942123456789 AS Int64) AS nano, 'UTC' AS tz SELECT timestamp, @@ -59,13 +59,13 @@ SELECT SELECT 'lower range bound'; WITH - -1420066799 AS timestamp, - CAST(-1420066799123 AS Int64) AS milli, - CAST(-1420066799123456 AS Int64) AS micro, - CAST(-1420066799123456789 AS Int64) AS nano, + -2208985199 AS timestamp, + CAST(-2208985199123 AS Int64) AS milli, + CAST(-2208985199123456 AS Int64) AS micro, + CAST(-2208985199123456789 AS Int64) AS nano, 'UTC' AS tz SELECT timestamp, fromUnixTimestamp64Milli(milli, tz), fromUnixTimestamp64Micro(micro, tz), - fromUnixTimestamp64Nano(nano, tz); \ No newline at end of file + fromUnixTimestamp64Nano(nano, tz); diff --git a/tests/queries/0_stateless/01691_DateTime64_clamp.reference b/tests/queries/0_stateless/01691_DateTime64_clamp.reference index 6272103440c..75435aebd67 100644 --- a/tests/queries/0_stateless/01691_DateTime64_clamp.reference +++ b/tests/queries/0_stateless/01691_DateTime64_clamp.reference @@ -18,10 +18,10 @@ SELECT toDateTime64(toFloat32(bitShiftLeft(toUInt64(1),33)), 2, 'Asia/Istanbul') SELECT toDateTime64(toFloat64(bitShiftLeft(toUInt64(1),33)), 2, 'Asia/Istanbul') FORMAT Null; -- These are outsize of extended range and hence clamped SELECT toDateTime64(-1 * bitShiftLeft(toUInt64(1), 35), 2, 'Asia/Istanbul'); -1925-01-01 02:00:00.00 +1900-01-01 01:56:56.00 SELECT CAST(-1 * bitShiftLeft(toUInt64(1), 35) AS DateTime64(3, 'Asia/Istanbul')); -1925-01-01 02:00:00.000 +1900-01-01 01:56:56.000 SELECT CAST(bitShiftLeft(toUInt64(1), 35) AS DateTime64(3, 'Asia/Istanbul')); -2282-12-31 03:00:00.000 +2299-12-31 23:59:59.000 SELECT toDateTime64(bitShiftLeft(toUInt64(1), 35), 2, 'Asia/Istanbul'); -2282-12-31 03:00:00.00 +2299-12-31 23:59:59.00 diff --git a/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.reference b/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.reference index f27bf42c7a5..ecea0a9f69f 100644 --- a/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.reference +++ b/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.reference @@ -1,4 +1,4 @@ 1940-10-09 21:13:17.6 -2283-11-11 23:46:43.6 -2283-11-11 23:46:40.1 -1925-01-01 00:00:00.9 +2284-06-04 23:46:43.6 +2299-12-31 23:40:00.1 +1900-01-01 00:00:00.9 diff --git a/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.sql b/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.sql index b0dbd1dfc84..e84bb35b3a5 100644 --- a/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.sql +++ b/tests/queries/0_stateless/01702_toDateTime_from_string_clamping.sql @@ -1,4 +1,4 @@ SELECT toString(toDateTime('-922337203.6854775808', 1, 'Asia/Istanbul')); SELECT toString(toDateTime('9922337203.6854775808', 1, 'Asia/Istanbul')); -SELECT toDateTime64(CAST('10000000000.1' AS Decimal64(1)), 1, 'Asia/Istanbul'); -SELECT toDateTime64(CAST('-10000000000.1' AS Decimal64(1)), 1, 'Asia/Istanbul'); +SELECT toDateTime64(CAST('10500000000.1' AS Decimal64(1)), 1, 'Asia/Istanbul'); +SELECT toDateTime64(CAST('-10500000000.1' AS Decimal64(1)), 1, 'Asia/Istanbul');