forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UUID Presto type (facebookincubator#10078)
Summary: Pull Request resolved: facebookincubator#10078 UUID is a logical type backed by HUGEINT physical type. Add uuid() function, CAST(uuid AS varchar) and CAST(varchar AS uuid). ``` presto> select uuid(); _col0 -------------------------------------- 9a97a14a-25d7-48c7-842b-38539d056e2f (1 row) ``` Reviewed By: xiaoxmeng, amitkdutta Differential Revision: D58222115 fbshipit-source-id: c8bb54ae391e2f02e6dde5d6879c89df34270757
- Loading branch information
1 parent
7fe89b4
commit 179b108
Showing
15 changed files
with
420 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
============== | ||
UUID functions | ||
============== | ||
|
||
.. function:: uuid() -> uuid | ||
|
||
Returns a pseudo randomly generated UUID (type 4). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <boost/uuid/uuid.hpp> | ||
#include <boost/uuid/uuid_generators.hpp> | ||
|
||
#include "velox/functions/Macros.h" | ||
#include "velox/functions/Registerer.h" | ||
#include "velox/functions/prestosql/types/UuidType.h" | ||
|
||
namespace facebook::velox::functions { | ||
|
||
template <typename T> | ||
struct UuidFunction { | ||
VELOX_DEFINE_FUNCTION_TYPES(T); | ||
|
||
static constexpr bool is_deterministic = false; | ||
|
||
FOLLY_ALWAYS_INLINE void call(int128_t& result) { | ||
boost::uuids::uuid uuid = generator_(); | ||
memcpy(&result, uuid.data, 16); | ||
} | ||
|
||
private: | ||
boost::uuids::random_generator generator_; | ||
}; | ||
|
||
inline void registerUuidFunctions(const std::string& prefix) { | ||
registerUuidType(); | ||
registerFunction<UuidFunction, Uuid>({prefix + "uuid"}); | ||
} | ||
|
||
} // namespace facebook::velox::functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/common/base/tests/GTestUtils.h" | ||
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h" | ||
|
||
namespace facebook::velox::functions::prestosql { | ||
|
||
namespace { | ||
|
||
class UuidFunctionsTest : public functions::test::FunctionBaseTest {}; | ||
|
||
TEST_F(UuidFunctionsTest, uuid) { | ||
auto result = | ||
evaluate<FlatVector<int128_t>>("uuid()", makeRowVector(ROW({}), 10)); | ||
|
||
// Sanity check results. All values are unique. | ||
std::unordered_set<int128_t> uuids; | ||
for (auto i = 0; i < 10; ++i) { | ||
const auto uuid = result->valueAt(i); | ||
ASSERT_TRUE(uuids.insert(uuid).second); | ||
} | ||
ASSERT_EQ(10, uuids.size()); | ||
} | ||
|
||
TEST_F(UuidFunctionsTest, typeof) { | ||
auto result = evaluate("typeof(uuid())", makeRowVector(ROW({}), 10)); | ||
|
||
auto expected = makeConstant("uuid", 10); | ||
velox::test::assertEqualVectors(expected, result); | ||
|
||
result = evaluate( | ||
"typeof(array_constructor(uuid(), uuid()))", makeRowVector(ROW({}), 10)); | ||
|
||
expected = makeConstant("array(uuid)", 10); | ||
velox::test::assertEqualVectors(expected, result); | ||
} | ||
|
||
TEST_F(UuidFunctionsTest, castAsVarchar) { | ||
auto result = evaluate<FlatVector<StringView>>( | ||
"cast(uuid() as varchar)", makeRowVector(ROW({}), 10)); | ||
|
||
// Sanity check results. All strings are unique. Each string is 36 bytes | ||
// long. | ||
std::unordered_set<std::string> uuids; | ||
for (auto i = 0; i < 10; ++i) { | ||
const auto uuid = result->valueAt(i).str(); | ||
ASSERT_EQ(36, uuid.size()); | ||
ASSERT_TRUE(uuids.insert(uuid).second); | ||
} | ||
ASSERT_EQ(10, uuids.size()); | ||
} | ||
|
||
TEST_F(UuidFunctionsTest, castRoundTrip) { | ||
auto strings = makeFlatVector<std::string>({ | ||
"33355449-2c7d-43d7-967a-f53cd23215ad", | ||
"eed9f812-4b0c-472f-8a10-4ae7bff79a47", | ||
"f768f36d-4f09-4da7-a298-3564d8f3c986", | ||
}); | ||
|
||
auto uuids = evaluate("cast(c0 as uuid)", makeRowVector({strings})); | ||
auto stringsCopy = evaluate("cast(c0 as varchar)", makeRowVector({uuids})); | ||
auto uuidsCopy = evaluate("cast(c0 as uuid)", makeRowVector({stringsCopy})); | ||
|
||
velox::test::assertEqualVectors(strings, stringsCopy); | ||
velox::test::assertEqualVectors(uuids, uuidsCopy); | ||
} | ||
|
||
TEST_F(UuidFunctionsTest, unsupportedCast) { | ||
auto input = makeRowVector(ROW({}), 10); | ||
VELOX_ASSERT_THROW( | ||
evaluate("cast(uuid() as integer)", input), | ||
"Cannot cast UUID to INTEGER"); | ||
VELOX_ASSERT_THROW( | ||
evaluate("cast(123 as uuid())", input), "Cannot cast BIGINT to UUID."); | ||
} | ||
|
||
} // namespace | ||
} // namespace facebook::velox::functions::prestosql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/functions/prestosql/types/UuidType.h" | ||
#include <boost/lexical_cast.hpp> | ||
#include <boost/uuid/uuid.hpp> | ||
#include <boost/uuid/uuid_io.hpp> | ||
|
||
namespace facebook::velox { | ||
|
||
namespace { | ||
|
||
class UuidCastOperator : public exec::CastOperator { | ||
public: | ||
bool isSupportedFromType(const TypePtr& other) const override { | ||
return VARCHAR()->equivalent(*other); | ||
} | ||
|
||
bool isSupportedToType(const TypePtr& other) const override { | ||
return VARCHAR()->equivalent(*other); | ||
} | ||
|
||
void castTo( | ||
const BaseVector& input, | ||
exec::EvalCtx& context, | ||
const SelectivityVector& rows, | ||
const TypePtr& resultType, | ||
VectorPtr& result) const override { | ||
context.ensureWritable(rows, resultType, result); | ||
|
||
if (input.typeKind() == TypeKind::VARCHAR) { | ||
castFromString(input, context, rows, *result); | ||
} else { | ||
VELOX_UNSUPPORTED( | ||
"Cast from {} to UUID not yet supported", resultType->toString()); | ||
} | ||
} | ||
|
||
void castFrom( | ||
const BaseVector& input, | ||
exec::EvalCtx& context, | ||
const SelectivityVector& rows, | ||
const TypePtr& resultType, | ||
VectorPtr& result) const override { | ||
context.ensureWritable(rows, resultType, result); | ||
|
||
if (resultType->kind() == TypeKind::VARCHAR) { | ||
castToString(input, context, rows, *result); | ||
} else { | ||
VELOX_UNSUPPORTED( | ||
"Cast from UUID to {} not yet supported", resultType->toString()); | ||
} | ||
} | ||
|
||
private: | ||
static void castToString( | ||
const BaseVector& input, | ||
exec::EvalCtx& context, | ||
const SelectivityVector& rows, | ||
BaseVector& result) { | ||
auto* flatResult = result.as<FlatVector<StringView>>(); | ||
const auto* uuids = input.as<SimpleVector<int128_t>>(); | ||
|
||
context.applyToSelectedNoThrow(rows, [&](auto row) { | ||
const auto uuid = uuids->valueAt(row); | ||
|
||
boost::uuids::uuid u; | ||
memcpy(&u, &uuid, 16); | ||
|
||
std::string s = boost::lexical_cast<std::string>(u); | ||
|
||
exec::StringWriter<false> result(flatResult, row); | ||
result.append(s); | ||
result.finalize(); | ||
}); | ||
} | ||
|
||
static void castFromString( | ||
const BaseVector& input, | ||
exec::EvalCtx& context, | ||
const SelectivityVector& rows, | ||
BaseVector& result) { | ||
auto* flatResult = result.as<FlatVector<int128_t>>(); | ||
const auto* uuidStrings = input.as<SimpleVector<StringView>>(); | ||
|
||
context.applyToSelectedNoThrow(rows, [&](auto row) { | ||
const auto uuidString = uuidStrings->valueAt(row); | ||
|
||
auto uuid = boost::lexical_cast<boost::uuids::uuid>(uuidString); | ||
|
||
int128_t u; | ||
memcpy(&u, &uuid, 16); | ||
|
||
flatResult->set(row, u); | ||
}); | ||
} | ||
}; | ||
|
||
class UuidTypeFactories : public CustomTypeFactories { | ||
public: | ||
UuidTypeFactories() = default; | ||
|
||
TypePtr getType() const override { | ||
return UUID(); | ||
} | ||
|
||
exec::CastOperatorPtr getCastOperator() const override { | ||
return std::make_shared<UuidCastOperator>(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void registerUuidType() { | ||
registerCustomType("uuid", std::make_unique<const UuidTypeFactories>()); | ||
} | ||
|
||
} // namespace facebook::velox |
Oops, something went wrong.