Skip to content

Commit

Permalink
Merge pull request facebookincubator#14 from PHILO-HE/fix-ut
Browse files Browse the repository at this point in the history
Fix test failure in cast
  • Loading branch information
zhejiangxiaomai authored Jul 10, 2023
2 parents 1cf7da4 + eb34576 commit 030bc8c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
22 changes: 18 additions & 4 deletions velox/expression/CastExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,30 @@ void applyCastKernel(
vector_size_t row,
const SimpleVector<typename TypeTraits<FromKind>::NativeType>* input,
FlatVector<typename TypeTraits<ToKind>::NativeType>* result) {
auto output = util::Converter<ToKind, void, Truncate, AllowDecimal>::cast(
input->valueAt(row));

// Special handling for string target type
if constexpr (ToKind == TypeKind::VARCHAR || ToKind == TypeKind::VARBINARY) {
std::string output;
if (input->type()->isDecimal()) {
output = util::Converter<ToKind, void, Truncate, AllowDecimal>::cast(
input->valueAt(row), input->type());
} else {
output = util::Converter<ToKind, void, Truncate, AllowDecimal>::cast(
input->valueAt(row));
}
// Write the result output to the output vector
auto writer = exec::StringWriter<>(result, row);
writer.copy_from(output);
writer.finalize();
} else {
result->set(row, output);
if (input->type()->isDecimal()) {
auto output = util::Converter<ToKind, void, Truncate, AllowDecimal>::cast(
input->valueAt(row), input->type());
result->set(row, output);
} else {
auto output = util::Converter<ToKind, void, Truncate, AllowDecimal>::cast(
input->valueAt(row));
result->set(row, output);
}
}
}

Expand Down
37 changes: 23 additions & 14 deletions velox/type/Conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ struct Converter {
}

template <typename T>
static typename TypeTraits<KIND>::NativeType
cast(T val, bool& nullOutput, const TypePtr& toType) {
static typename TypeTraits<KIND>::NativeType cast(
T val,
const TypePtr& toType) {
VELOX_UNSUPPORTED(
"Conversion of {} to {} is not supported",
CppToType<T>::name,
Expand All @@ -53,6 +54,11 @@ template <>
struct Converter<TypeKind::BOOLEAN> {
using T = bool;

template <typename From>
static T cast(const From& v, const TypePtr& toType) {
VELOX_NYI();
}

template <typename From>
static T cast(const From& v) {
return folly::to<T>(v);
Expand Down Expand Up @@ -294,12 +300,9 @@ struct Converter<
}
if constexpr (std::is_same_v<T, int128_t>) {
return std::numeric_limits<int128_t>::min();
} else if (v < LimitType::minLimit()) {
return LimitType::min();
}
// bool type's min is 0, but spark expects true for casting negative float
// data.
if (!std::is_same_v<T, bool> && v < LimitType::minLimit()) {
} else if (!std::is_same_v<T, bool> && v < LimitType::minLimit()) {
// bool type's min is 0, but spark expects true for casting negative
// float data. So filter out bool type here.
return LimitType::min();
}
return LimitType::cast(v);
Expand Down Expand Up @@ -372,7 +375,7 @@ struct Converter<
}
}

static T cast(const int128_t& v, bool& nullOutput) {
static T cast(const int128_t& v) {
if constexpr (TRUNCATE) {
return T(v);
} else {
Expand Down Expand Up @@ -470,18 +473,24 @@ struct Converter<
"Conversion of Timestamp to Real or Double is not supported");
}

static T cast(const int128_t& d, bool& nullOutput) {
static T cast(const int128_t& d) {
VELOX_UNSUPPORTED(
"Conversion of int128_t to Real or Double is not supported");
}
};

template <bool TRUNCATE>
struct Converter<TypeKind::VARBINARY, void, TRUNCATE> {
template <bool TRUNCATE, bool ALLOW_DECIMAL>
struct Converter<TypeKind::VARBINARY, void, TRUNCATE, ALLOW_DECIMAL> {
template <typename T>
static std::string cast(const T& v, const TypePtr& fromType) {
VELOX_NYI();
}

// Same semantics of TypeKind::VARCHAR converter.
template <typename T>
static std::string cast(const T& val) {
return Converter<TypeKind::VARCHAR, void, TRUNCATE>::cast(val);
return Converter<TypeKind::VARCHAR, void, TRUNCATE, ALLOW_DECIMAL>::cast(
val);
}
};

Expand Down Expand Up @@ -563,7 +572,7 @@ struct Converter<TypeKind::DATE, void, TRUNCATE, ALLOW_DECIMAL> {
using T = typename TypeTraits<TypeKind::DATE>::NativeType;

template <typename From>
static T cast(const From& v, bool& nullOutput, const TypePtr& toType) {
static T cast(const From& v, const TypePtr& toType) {
VELOX_NYI();
}

Expand Down

0 comments on commit 030bc8c

Please sign in to comment.