Skip to content

Commit

Permalink
Add tests for casting UNKNOWN type to any other type (#6996)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #6996

Reviewed By: xiaoxmeng

Differential Revision: D54201176

Pulled By: mbasmanova

fbshipit-source-id: 4059e775b6258046d7c012675ea75f7799cafa59
  • Loading branch information
PHILO-HE authored and facebook-github-bot committed Feb 27, 2024
1 parent 5fa959f commit 62b8cfd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions velox/docs/functions/spark/conversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Conversion Functions
====================

Casting from UNKNOWN type to all other scalar types is supported, e.g., cast(NULL as int).

Cast to Integral Types
----------------------

Expand Down
27 changes: 27 additions & 0 deletions velox/expression/tests/CastExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,33 @@ TEST_F(CastExprTest, basics) {
{"1.888", "2.5", "3.6", "100.44", "-100.101", "1", "-2"});
}

TEST_F(CastExprTest, fromUnknownType) {
testCast<UnknownValue, int8_t>(
"tinyint", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, int16_t>(
"smallint", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, int32_t>(
"int", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, int64_t>(
"bigint", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, float>(
"float", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, double>(
"double", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, std::string>(
"string", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, bool>(
"boolean", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, Timestamp>(
"timestamp", {std::nullopt, std::nullopt}, {std::nullopt, std::nullopt});
testCast<UnknownValue, int32_t>(
"date",
{std::nullopt, std::nullopt},
{std::nullopt, std::nullopt},
UNKNOWN(),
DATE());
}

TEST_F(CastExprTest, realAndDoubleToString) {
setLegacyCast(false);
testCast<double, std::string>(
Expand Down
21 changes: 21 additions & 0 deletions velox/vector/tests/utils/VectorMakerStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ class VectorMakerStats {
std::unordered_set<T> distinctSet_;
};

template <>
class VectorMakerStats<UnknownValue> {
public:
void addElement(const UnknownValue& val) {
VELOX_UNREACHABLE();
}

size_t distinctCount() const {
return 1;
}

SimpleVectorStats<UnknownValue> asSimpleVectorStats() {
return {min, max};
}

std::optional<UnknownValue> min;
std::optional<UnknownValue> max;
size_t nullCount{0};
bool isSorted{false};
};

// Generates VectorMakerStats for a given vector of nullable elements.
template <typename T>
VectorMakerStats<EvalType<T>> genVectorMakerStats(
Expand Down

0 comments on commit 62b8cfd

Please sign in to comment.