-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TDigest type and varbinary conversion (#12270)
Summary: Add new TDigest Type and Varbinary conversion X-link: prestodb/presto#24505 Differential Revision: D69208299
- Loading branch information
1 parent
ad4291b
commit a316b93
Showing
8 changed files
with
215 additions
and
0 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,48 @@ | ||
/* | ||
* 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/tests/CastBaseTest.h" | ||
#include "velox/functions/prestosql/types/TDigestType.h" | ||
|
||
using namespace facebook::velox; | ||
|
||
class TDigestCastTest : public functions::test::CastBaseTest {}; | ||
|
||
TEST_F(TDigestCastTest, toTDigest) { | ||
testCast<StringView, StringView>( | ||
VARBINARY(), | ||
TDIGEST(), | ||
{"aaa"_sv, ""_sv, std::nullopt}, | ||
{"aaa"_sv, ""_sv, std::nullopt}); | ||
testCast<StringView, StringView>( | ||
VARBINARY(), | ||
TDIGEST(), | ||
{std::nullopt, std::nullopt, std::nullopt, std::nullopt}, | ||
{std::nullopt, std::nullopt, std::nullopt, std::nullopt}); | ||
} | ||
|
||
TEST_F(TDigestCastTest, fromTDigest) { | ||
testCast<StringView, StringView>( | ||
TDIGEST(), | ||
VARBINARY(), | ||
{"aaa"_sv, ""_sv, std::nullopt}, | ||
{"aaa"_sv, ""_sv, std::nullopt}); | ||
testCast<StringView, StringView>( | ||
TDIGEST(), | ||
VARBINARY(), | ||
{std::nullopt, std::nullopt, std::nullopt, std::nullopt}, | ||
{std::nullopt, std::nullopt, std::nullopt, std::nullopt}); | ||
} |
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,24 @@ | ||
/* | ||
* 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/TDigestType.h" | ||
|
||
namespace facebook::velox { | ||
|
||
void registerTDigestType() { | ||
registerCustomType("tdigest", std::make_unique<const TDigestTypeFactories>()); | ||
} | ||
|
||
} // namespace facebook::velox |
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,93 @@ | ||
/* | ||
* 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 "velox/type/SimpleFunctionApi.h" | ||
#include "velox/type/Type.h" | ||
#include "velox/vector/VectorTypeUtils.h" | ||
|
||
namespace facebook::velox { | ||
|
||
class TDigestType : public VarbinaryType { | ||
TDigestType() = default; | ||
|
||
public: | ||
static const std::shared_ptr<const TDigestType>& get() { | ||
static const std::shared_ptr<const TDigestType> instance = | ||
std::shared_ptr<TDigestType>(new TDigestType()); | ||
|
||
return instance; | ||
} | ||
|
||
bool equivalent(const Type& other) const override { | ||
// Pointer comparison works since this type is a singleton. | ||
return this == &other; | ||
} | ||
|
||
const char* name() const override { | ||
return "TDIGEST"; | ||
} | ||
|
||
std::string toString() const override { | ||
return name(); | ||
} | ||
|
||
folly::dynamic serialize() const override { | ||
folly::dynamic obj = folly::dynamic::object; | ||
obj["name"] = "Type"; | ||
obj["type"] = name(); | ||
return obj; | ||
} | ||
}; | ||
|
||
inline bool isTDigestType(const TypePtr& type) { | ||
// Pointer comparison works since this type is a singleton. | ||
return TDigestType::get() == type; | ||
} | ||
|
||
inline std::shared_ptr<const TDigestType> TDIGEST() { | ||
return TDigestType::get(); | ||
} | ||
|
||
// Type to use for inputs and outputs of simple functions, e.g. | ||
// arg_type<TDigest> and out_type<TDigest>. | ||
struct TDigestT { | ||
using type = Varbinary; | ||
static constexpr const char* typeName = "tdigest"; | ||
}; | ||
|
||
using TDigest = CustomType<TDigestT>; | ||
|
||
class TDigestTypeFactories : public CustomTypeFactories { | ||
public: | ||
TypePtr getType() const override { | ||
return TDIGEST(); | ||
} | ||
|
||
// TDigest should be treated as Varbinary during type castings. | ||
exec::CastOperatorPtr getCastOperator() const override { | ||
return nullptr; | ||
} | ||
|
||
AbstractInputGeneratorPtr getInputGenerator( | ||
const InputGeneratorConfig& /*config*/) const override { | ||
return nullptr; | ||
} | ||
}; | ||
|
||
void registerTDigestType(); | ||
|
||
} // namespace facebook::velox |
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,41 @@ | ||
/* | ||
* 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/TDigestType.h" | ||
#include "velox/functions/prestosql/types/tests/TypeTestBase.h" | ||
|
||
namespace facebook::velox::test { | ||
|
||
class TDigestTypeTest : public testing::Test, public TypeTestBase { | ||
public: | ||
TDigestTypeTest() { | ||
registerTDigestType(); | ||
} | ||
}; | ||
|
||
TEST_F(TDigestTypeTest, basic) { | ||
ASSERT_STREQ(TDIGEST()->name(), "TDIGEST"); | ||
ASSERT_STREQ(TDIGEST()->kindName(), "VARBINARY"); | ||
ASSERT_TRUE(TDIGEST()->parameters().empty()); | ||
ASSERT_EQ(TDIGEST()->toString(), "TDIGEST"); | ||
|
||
ASSERT_TRUE(hasType("TDIGEST")); | ||
ASSERT_EQ(*getType("TDIGEST", {}), *TDIGEST()); | ||
} | ||
|
||
TEST_F(TDigestTypeTest, serde) { | ||
testTypeSerde(TDIGEST()); | ||
} | ||
} // namespace facebook::velox::test |