Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failure in cast #14

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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