Merge pull request #3074 from 4ertus2/uuid

Alter Strings to UUID columns (needs pull request #3071 first)
This commit is contained in:
alexey-milovidov 2018-09-08 00:33:35 +03:00 committed by GitHub
commit ec10ee36fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 0 deletions

View File

@ -1468,6 +1468,24 @@ private:
};
}
WrapperType createUUIDWrapper(const DataTypePtr & from_type, const DataTypeUUID * const, bool requested_result_is_nullable) const
{
if (requested_result_is_nullable)
throw Exception{"CAST AS Nullable(UUID) is not implemented", ErrorCodes::NOT_IMPLEMENTED};
FunctionPtr function = FunctionTo<DataTypeUUID>::Type::create(context);
/// Check conversion using underlying function
{
function->getReturnType(ColumnsWithTypeAndName(1, { nullptr, from_type, "" }));
}
return [function] (Block & block, const ColumnNumbers & arguments, const size_t result, size_t input_rows_count)
{
function->execute(block, arguments, result, input_rows_count);
};
}
template <typename FieldType>
WrapperType createDecimalWrapper(const DataTypePtr & from_type, const DataTypeDecimal<FieldType> * to_type) const
{
@ -1919,6 +1937,14 @@ private:
ret = createDecimalWrapper(from_type, checkAndGetDataType<ToDataType>(to_type.get()));
return true;
}
if constexpr (std::is_same_v<ToDataType, DataTypeUUID>)
{
if (isStringOrFixedString(from_type))
{
ret = createUUIDWrapper(from_type, checkAndGetDataType<ToDataType>(to_type.get()), requested_result_is_nullable);
return true;
}
}
return false;
};

View File

@ -0,0 +1,6 @@
00000000-0000-01f8-9cb8-cb1b82fb3900 00000000-0000-01f8-9cb8-cb1b82fb3900
00000000-0000-02f8-9cb8-cb1b82fb3900 00000000-0000-02f8-9cb8-cb1b82fb3900
00000000-0000-03f8-9cb8-cb1b82fb3900 00000000-0000-03f8-9cb8-cb1b82fb3900
00000000-0000-04f8-9cb8-cb1b82fb3900 00000000-0000-04f8-9cb8-cb1b82fb3900
00000000-0000-05f8-9cb8-cb1b82fb3900 00000000-0000-06f8-9cb8-cb1b82fb3900
UUID UUID

View File

@ -0,0 +1,47 @@
USE test;
SELECT '00000000-0000-01f8-9cb8-cb1b82fb3900' AS str, toUUID(str);
SELECT toFixedString('00000000-0000-02f8-9cb8-cb1b82fb3900', 36) AS str, toUUID(str);
SELECT '00000000-0000-03f8-9cb8-cb1b82fb3900' AS str, CAST(str, 'UUID');
SELECT toFixedString('00000000-0000-04f8-9cb8-cb1b82fb3900', 36) AS str, CAST(str, 'UUID');
DROP TABLE IF EXISTS uuid;
CREATE TABLE IF NOT EXISTS uuid
(
created_at DateTime,
id0 String,
id1 FixedString(36)
)
ENGINE = MergeTree
PARTITION BY toDate(created_at)
ORDER BY (created_at);
INSERT INTO uuid VALUES ('2018-01-01 01:02:03', '00000000-0000-05f8-9cb8-cb1b82fb3900', '00000000-0000-06f8-9cb8-cb1b82fb3900');
ALTER TABLE uuid MODIFY COLUMN id0 UUID;
ALTER TABLE uuid MODIFY COLUMN id1 UUID;
SELECT id0, id1 FROM uuid;
SELECT toTypeName(id0), toTypeName(id1) FROM uuid;
DROP TABLE uuid;
-- with UUID in key
CREATE TABLE IF NOT EXISTS uuid
(
created_at DateTime,
id0 String,
id1 FixedString(36)
)
ENGINE = MergeTree
PARTITION BY toDate(created_at)
ORDER BY (created_at, id0, id1);
SET send_logs_level = 'none';
ALTER TABLE uuid MODIFY COLUMN id0 UUID; -- { serverError 44 }
ALTER TABLE uuid MODIFY COLUMN id1 UUID; -- { serverError 44 }
DROP TABLE uuid;