From 7f84880e9e71ceb3bb51da14dd21ad2dcf3d4494 Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Mon, 10 Jul 2023 09:51:37 +0800 Subject: [PATCH 1/3] Fix sparkSemantic test failure --- velox/type/Conversions.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/velox/type/Conversions.h b/velox/type/Conversions.h index c081525e67c6..234d158bd3c8 100644 --- a/velox/type/Conversions.h +++ b/velox/type/Conversions.h @@ -294,12 +294,9 @@ struct Converter< } if constexpr (std::is_same_v) { return std::numeric_limits::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 && v < LimitType::minLimit()) { + } else if (!std::is_same_v && v < LimitType::minLimit()) { return LimitType::min(); } return LimitType::cast(v); From 30c23c78d1d223aa7e79db757ba5d966f334dfe4 Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Mon, 10 Jul 2023 09:52:30 +0800 Subject: [PATCH 2/3] Fix decimalToInt test failure --- velox/expression/CastExpr.cpp | 22 ++++++++++++++++++---- velox/type/Conversions.h | 25 ++++++++++++++++++------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/velox/expression/CastExpr.cpp b/velox/expression/CastExpr.cpp index 31901971452a..8dfe98870af1 100644 --- a/velox/expression/CastExpr.cpp +++ b/velox/expression/CastExpr.cpp @@ -47,16 +47,30 @@ void applyCastKernel( vector_size_t row, const SimpleVector::NativeType>* input, FlatVector::NativeType>* result) { - auto output = util::Converter::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::cast( + input->valueAt(row), input->type()); + } else { + output = util::Converter::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::cast( + input->valueAt(row), input->type()); + result->set(row, output); + } else { + auto output = util::Converter::cast( + input->valueAt(row)); + result->set(row, output); + } } } diff --git a/velox/type/Conversions.h b/velox/type/Conversions.h index 234d158bd3c8..5c8b51cfd438 100644 --- a/velox/type/Conversions.h +++ b/velox/type/Conversions.h @@ -41,7 +41,7 @@ struct Converter { template static typename TypeTraits::NativeType - cast(T val, bool& nullOutput, const TypePtr& toType) { + cast(T val, const TypePtr& toType) { VELOX_UNSUPPORTED( "Conversion of {} to {} is not supported", CppToType::name, @@ -53,6 +53,11 @@ template <> struct Converter { using T = bool; + template + static T cast(const From& v, const TypePtr& toType) { + VELOX_NYI(); + } + template static T cast(const From& v) { return folly::to(v); @@ -369,7 +374,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 { @@ -467,18 +472,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 -struct Converter { +template +struct Converter { + + template + static std::string cast(const T& v, const TypePtr& fromType) { + VELOX_NYI(); + } + // Same semantics of TypeKind::VARCHAR converter. template static std::string cast(const T& val) { - return Converter::cast(val); + return Converter::cast(val); } }; @@ -560,7 +571,7 @@ struct Converter { using T = typename TypeTraits::NativeType; template - static T cast(const From& v, bool& nullOutput, const TypePtr& toType) { + static T cast(const From& v, const TypePtr& toType) { VELOX_NYI(); } From eb34576f66eb46aadf5750f6c52c8fb6f9551a7a Mon Sep 17 00:00:00 2001 From: PHILO-HE Date: Mon, 10 Jul 2023 09:57:25 +0800 Subject: [PATCH 3/3] Format the code --- velox/type/Conversions.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/velox/type/Conversions.h b/velox/type/Conversions.h index 5c8b51cfd438..c62dab32d66c 100644 --- a/velox/type/Conversions.h +++ b/velox/type/Conversions.h @@ -40,8 +40,9 @@ struct Converter { } template - static typename TypeTraits::NativeType - cast(T val, const TypePtr& toType) { + static typename TypeTraits::NativeType cast( + T val, + const TypePtr& toType) { VELOX_UNSUPPORTED( "Conversion of {} to {} is not supported", CppToType::name, @@ -299,9 +300,9 @@ struct Converter< } if constexpr (std::is_same_v) { return std::numeric_limits::min(); - // bool type's min is 0, but spark expects true for casting negative float - // data. } else if (!std::is_same_v && 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); @@ -480,7 +481,6 @@ struct Converter< template struct Converter { - template static std::string cast(const T& v, const TypePtr& fromType) { VELOX_NYI(); @@ -489,7 +489,8 @@ struct Converter { // Same semantics of TypeKind::VARCHAR converter. template static std::string cast(const T& val) { - return Converter::cast(val); + return Converter::cast( + val); } };