Skip to content

Commit

Permalink
Add TDigest type and varbinary conversion (#12270)
Browse files Browse the repository at this point in the history
Summary:
Add new TDigest Type and Varbinary conversion

X-link: prestodb/presto#24505


Differential Revision: D69208299
  • Loading branch information
natashasehgal authored and facebook-github-bot committed Feb 5, 2025
1 parent ad4291b commit a316b93
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
4 changes: 4 additions & 0 deletions velox/functions/prestosql/TypeOf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "velox/functions/prestosql/types/IPAddressType.h"
#include "velox/functions/prestosql/types/IPPrefixType.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/TDigestType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"
#include "velox/functions/prestosql/types/UuidType.h"

Expand Down Expand Up @@ -80,6 +81,9 @@ std::string typeName(const TypePtr& type) {
if (isHyperLogLogType(type)) {
return "HyperLogLog";
}
if (isTDigestType(type)) {
return "TDigest";
}
return "varbinary";
case TypeKind::TIMESTAMP:
return "timestamp";
Expand Down
1 change: 1 addition & 0 deletions velox/functions/prestosql/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ add_executable(
TransformValuesTest.cpp
TrimFunctionsTest.cpp
TypeOfTest.cpp
TDigestCastTest.cpp
URLFunctionsTest.cpp
UuidFunctionsTest.cpp
WidthBucketArrayTest.cpp
Expand Down
48 changes: 48 additions & 0 deletions velox/functions/prestosql/tests/TDigestCastTest.cpp
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});
}
3 changes: 3 additions & 0 deletions velox/functions/prestosql/tests/TypeOfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "velox/functions/prestosql/tests/utils/FunctionBaseTest.h"
#include "velox/functions/prestosql/types/HyperLogLogType.h"
#include "velox/functions/prestosql/types/JsonType.h"
#include "velox/functions/prestosql/types/TDigestType.h"
#include "velox/functions/prestosql/types/TimestampWithTimeZoneType.h"

namespace facebook::velox::functions {
Expand Down Expand Up @@ -56,6 +57,8 @@ TEST_F(TypeOfTest, basic) {

EXPECT_EQ("HyperLogLog", typeOf(HYPERLOGLOG()));

EXPECT_EQ("TDigest", typeOf(TDIGEST()));

EXPECT_EQ("unknown", typeOf(UNKNOWN()));

EXPECT_EQ("array(integer)", typeOf(ARRAY(INTEGER())));
Expand Down
1 change: 1 addition & 0 deletions velox/functions/prestosql/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ velox_add_library(
velox_presto_types
HyperLogLogType.cpp
JsonType.cpp
TDigestType.cpp
TimestampWithTimeZoneType.cpp
UuidType.cpp
IPAddressType.cpp
Expand Down
24 changes: 24 additions & 0 deletions velox/functions/prestosql/types/TDigestType.cpp
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
93 changes: 93 additions & 0 deletions velox/functions/prestosql/types/TDigestType.h
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
41 changes: 41 additions & 0 deletions velox/functions/prestosql/types/tests/TDigestTypeTest.cpp
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

0 comments on commit a316b93

Please sign in to comment.