diff --git a/docs/en/sql-reference/functions/uuid-functions.md b/docs/en/sql-reference/functions/uuid-functions.md index d1b833c2439..851294ff5dc 100644 --- a/docs/en/sql-reference/functions/uuid-functions.md +++ b/docs/en/sql-reference/functions/uuid-functions.md @@ -627,53 +627,6 @@ Result: └──────────────────────────────────────┴──────────────────┘ ``` -## UUIDv7ToDateTime - -Returns the timestamp component of a UUID version 7. - -**Syntax** - -``` sql -UUIDv7ToDateTime(uuid[, timezone]) -``` - -**Arguments** - -- `uuid` — [UUID](../data-types/uuid.md) of version 7. -- `timezone` — [Timezone name](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-timezone) for the returned value (optional). [String](../../sql-reference/data-types/string.md). - -**Returned value** - -- Timestamp with milliseconds precision. If the UUID is not a valid version 7 UUID, it returns 1970-01-01 00:00:00.000. - -Type: [DateTime64(3)](/docs/en/sql-reference/data-types/datetime64.md). - -**Usage examples** - -``` sql -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1')) -``` - -Result: - -```response -┌─UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'))─┐ -│ 2024-04-22 15:30:29.048 │ -└──────────────────────────────────────────────────────────────────┘ -``` - -``` sql -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York') -``` - -Result: - -```response -┌─UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York')─┐ -│ 2024-04-22 08:30:29.048 │ -└──────────────────────────────────────────────────────────────────────────────────────┘ -``` - ## serverUUID() Returns the random UUID generated during the first start of the ClickHouse server. The UUID is stored in file `uuid` in the ClickHouse server directory (e.g. `/var/lib/clickhouse/`) and retained between server restarts. diff --git a/src/Functions/FunctionsCodingUUID.cpp b/src/Functions/FunctionsCodingUUID.cpp index 76d7edf5277..ee7d88bbaf0 100644 --- a/src/Functions/FunctionsCodingUUID.cpp +++ b/src/Functions/FunctionsCodingUUID.cpp @@ -403,84 +403,6 @@ public: } }; -class FunctionUUIDv7ToDateTime : public IFunction -{ -public: - static constexpr auto name = "UUIDv7ToDateTime"; - static FunctionPtr create(ContextPtr) { return std::make_shared(); } - - static constexpr UInt32 datetime_scale = 3; - - String getName() const override { return name; } - size_t getNumberOfArguments() const override { return 0; } - bool useDefaultImplementationForConstants() const override { return true; } - ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; } - bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; } - bool isVariadic() const override { return true; } - - DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override - { - if (arguments.empty() || arguments.size() > 2) - throw Exception( - ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Wrong number of arguments for function {}: should be 1 or 2", getName()); - - if (!checkAndGetDataType(arguments[0].type.get())) - { - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Illegal type {} of first argument of function {}, expected UUID", - arguments[0].type->getName(), - getName()); - } - - String timezone; - if (arguments.size() == 2) - { - timezone = extractTimeZoneNameFromColumn(arguments[1].column.get(), arguments[1].name); - - if (timezone.empty()) - throw Exception( - ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, - "Function {} supports a 2nd argument (optional) that must be a valid time zone", - getName()); - } - - return std::make_shared(datetime_scale, timezone); - } - - ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override - { - const ColumnWithTypeAndName & col_type_name = arguments[0]; - const ColumnPtr & column = col_type_name.column; - - if (const auto * col_in = checkAndGetColumn(column.get())) - { - const auto & vec_in = col_in->getData(); - const UUID * uuids = vec_in.data(); - const size_t size = vec_in.size(); - - auto col_res = ColumnDateTime64::create(size, datetime_scale); - auto & vec_res = col_res->getData(); - - for (size_t i = 0; i < size; ++i) - { - uint64_t hiBytes = DB::UUIDHelpers::getHighBytes(uuids[i]); - if ((hiBytes & 0xf000) == 0x7000) - { - uint64_t ms = hiBytes >> 16; - vec_res[i] = DecimalUtils::decimalFromComponents( - ms / intExp10(datetime_scale), ms % intExp10(datetime_scale), datetime_scale); - } - } - - return col_res; - } - else - throw Exception( - ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", arguments[0].column->getName(), getName()); - } -}; - REGISTER_FUNCTION(CodingUUID) { factory.registerFunction(); @@ -501,19 +423,6 @@ This function accepts a UUID and returns a FixedString(16) as its binary represe )"}}, .categories{"UUID"}}, FunctionFactory::CaseSensitive); - - factory.registerFunction( - FunctionDocumentation{ - .description = R"( -This function extracts the timestamp from a UUID and returns it as a DateTime64(3) typed value. -The function expects the UUID having version 7 to be provided as the first argument. -An optional second argument can be passed to specify a timezone for the timestamp. -)", - .examples{ - {"uuid","select UUIDv7ToDateTime(generateUUIDv7())", ""}, - {"uuid","select generateUUIDv7() as uuid, UUIDv7ToDateTime(uuid), UUIDv7ToDateTime(uuid, 'America/New_York')", ""}}, - .categories{"UUID"}}, - FunctionFactory::CaseSensitive); } } diff --git a/tests/queries/0_stateless/00396_uuid_to_num.reference b/tests/queries/0_stateless/00396_uuid_to_num.reference new file mode 100644 index 00000000000..b81e0b3e48f --- /dev/null +++ b/tests/queries/0_stateless/00396_uuid_to_num.reference @@ -0,0 +1,3 @@ +-- UUIDToNum -- +1 +1 diff --git a/tests/queries/0_stateless/00396_uuid_v7.sql b/tests/queries/0_stateless/00396_uuid_to_num.sql similarity index 52% rename from tests/queries/0_stateless/00396_uuid_v7.sql rename to tests/queries/0_stateless/00396_uuid_to_num.sql index 5be5a72a324..dc2457142ca 100644 --- a/tests/queries/0_stateless/00396_uuid_v7.sql +++ b/tests/queries/0_stateless/00396_uuid_to_num.sql @@ -7,13 +7,3 @@ SELECT UUIDToNum(toUUID('00112233-4455-6677-8899-aabbccddeeff'), 3); -- { server SELECT UUIDToNum('00112233-4455-6677-8899-aabbccddeeff', 1); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT UUIDToNum(toUUID('00112233-4455-6677-8899-aabbccddeeff'), '1'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } SELECT UUIDToNum(toUUID('00112233-4455-6677-8899-aabbccddeeff'), materialize(1)); -- { serverError ILLEGAL_COLUMN } - -SELECT '-- UUIDv7toDateTime --'; -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York'); -SELECT UUIDv7ToDateTime(); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 1); -- { serverError ILLEGAL_COLUMN } -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/New_York', 1); -- { serverError NUMBER_OF_ARGUMENTS_DOESNT_MATCH } -SELECT UUIDv7ToDateTime('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'); -- { serverError ILLEGAL_TYPE_OF_ARGUMENT } -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), 'America/NewYork'); -- { serverError BAD_ARGUMENTS } -SELECT UUIDv7ToDateTime(toUUID('018f05c9-4ab8-7b86-b64e-c9f03fbd45d1'), materialize('America/New_York')); -- { serverError ILLEGAL_COLUMN } - diff --git a/tests/queries/0_stateless/00396_uuid_v7.reference b/tests/queries/0_stateless/00396_uuid_v7.reference deleted file mode 100644 index 46a80dcf19b..00000000000 --- a/tests/queries/0_stateless/00396_uuid_v7.reference +++ /dev/null @@ -1,5 +0,0 @@ --- UUIDToNum -- -1 -1 --- UUIDv7toDateTime -- -2024-04-22 08:30:29.048