diff --git a/be/src/vec/core/field.cpp b/be/src/vec/core/field.cpp index b378885ae65f59..9a0004dcd46e08 100644 --- a/be/src/vec/core/field.cpp +++ b/be/src/vec/core/field.cpp @@ -27,6 +27,7 @@ #include "vec/core/accurate_comparison.h" #include "vec/core/decimal_comparison.h" #include "vec/data_types/data_type_decimal.h" +#include "vec/functions/cast/cast_to_string.h" #include "vec/io/io_helper.h" #include "vec/io/var_int.h" #include "vec/runtime/timestamptz_value.h" @@ -791,10 +792,16 @@ std::string_view Field::as_string_view() const { #undef MATCH_PRIMITIVE_TYPE -#define MATCH_PRIMITIVE_TYPE(primite_type) \ +#define MATCH_NUMBER_TYPE(primite_type) \ + if (type == primite_type) { \ + const auto& v = get::NearestFieldType>(); \ + return CastToString::from_number(v); \ + } + +#define MATCH_DECIMAL_TYPE(primite_type) \ if (type == primite_type) { \ const auto& v = get::NearestFieldType>(); \ - return std::to_string(v); \ + return CastToString::from_decimal(v.get_value(), v.get_scale()); \ } std::string Field::to_string() const { @@ -803,63 +810,41 @@ std::string Field::to_string() const { const auto& s = get(); return {s.data(), s.size()}; } - if (type == TYPE_DECIMAL32) { - const auto& v = get::NearestFieldType>(); - return v.get_value().to_string(v.get_scale()); - } - if (type == TYPE_DECIMAL64) { - const auto& v = get::NearestFieldType>(); - return v.get_value().to_string(v.get_scale()); - } - if (type == TYPE_DECIMALV2) { - const auto& v = get::NearestFieldType>(); - return v.get_value().to_string(v.get_scale()); - } - if (type == TYPE_DECIMAL128I) { - const auto& v = get::NearestFieldType>(); - return v.get_value().to_string(v.get_scale()); - } - if (type == TYPE_DECIMAL256) { - const auto& v = get::NearestFieldType>(); - return v.get_value().to_string(v.get_scale()); - } - if (type == TYPE_LARGEINT) { - const auto& v = get::NearestFieldType>(); - return int128_to_string(v); - } + MATCH_DECIMAL_TYPE(TYPE_DECIMAL32); + MATCH_DECIMAL_TYPE(TYPE_DECIMAL64); + MATCH_DECIMAL_TYPE(TYPE_DECIMALV2); + MATCH_DECIMAL_TYPE(TYPE_DECIMAL128I); + MATCH_DECIMAL_TYPE(TYPE_DECIMAL256); + if (type == TYPE_DATE || type == TYPE_DATETIME) { - const auto& v = get::NearestFieldType>(); - std::string buf(40, 0); - auto* to = binary_cast(v).to_string(buf.data()); - buf.resize(to - buf.data() - 1); - return buf; + const auto& v = binary_cast( + get::NearestFieldType>()); + return CastToString::from_date_or_datetime(v); } if (type == TYPE_DATEV2) { - const auto& v = get::NearestFieldType>(); - return binary_cast>((uint32_t)v).to_string(); + const auto& v = binary_cast>( + (uint32_t)get::NearestFieldType>()); + return CastToString::from_datev2(v); } if (type == TYPE_DATETIMEV2) { - const auto& v = get::NearestFieldType>(); - return binary_cast>(v).to_string(); + const auto& v = binary_cast>( + (uint64_t)get::NearestFieldType>()); + return CastToString::from_datetimev2(v); } - MATCH_PRIMITIVE_TYPE(TYPE_BOOLEAN); - MATCH_PRIMITIVE_TYPE(TYPE_TINYINT); - MATCH_PRIMITIVE_TYPE(TYPE_SMALLINT); - MATCH_PRIMITIVE_TYPE(TYPE_INT); - MATCH_PRIMITIVE_TYPE(TYPE_BIGINT); - MATCH_PRIMITIVE_TYPE(TYPE_FLOAT); - MATCH_PRIMITIVE_TYPE(TYPE_DOUBLE); - MATCH_PRIMITIVE_TYPE(TYPE_TIME); - MATCH_PRIMITIVE_TYPE(TYPE_TIMEV2); - // MATCH_PRIMITIVE_TYPE(TYPE_IPV4); - // MATCH_PRIMITIVE_TYPE(TYPE_IPV6); - MATCH_PRIMITIVE_TYPE(TYPE_UINT32); - MATCH_PRIMITIVE_TYPE(TYPE_UINT64); + MATCH_NUMBER_TYPE(TYPE_BOOLEAN); + MATCH_NUMBER_TYPE(TYPE_TINYINT); + MATCH_NUMBER_TYPE(TYPE_SMALLINT); + MATCH_NUMBER_TYPE(TYPE_INT); + MATCH_NUMBER_TYPE(TYPE_BIGINT); + MATCH_NUMBER_TYPE(TYPE_LARGEINT); + MATCH_NUMBER_TYPE(TYPE_FLOAT); + MATCH_NUMBER_TYPE(TYPE_DOUBLE); throw Exception( Status::FatalError("type not supported for to_string, type={}", get_type_name())); } -#undef MATCH_PRIMITIVE_TYPE +#undef MATCH_NUMBER_TYPE +#undef MATCH_DECIMAL_TYPE #define DECLARE_FUNCTION(FUNC_NAME) \ template void Field::FUNC_NAME( \ diff --git a/be/src/vec/functions/cast/cast_to_string.h b/be/src/vec/functions/cast/cast_to_string.h index 1fa1e72a0f73c6..0a2b9d08846072 100644 --- a/be/src/vec/functions/cast/cast_to_string.h +++ b/be/src/vec/functions/cast/cast_to_string.h @@ -61,7 +61,7 @@ struct CastToString { static inline void push_datev2(const DateV2Value& from, BufferWritable& bw); static inline std::string from_datetimev2(const DateV2Value& from, - UInt32 scale); + UInt32 scale = -1); static inline std::string from_timestamptz(const TimestampTzValue& from, UInt32 scale, const cctz::time_zone* timezone = nullptr); static inline void push_datetimev2(const DateV2Value& from, UInt32 scale, @@ -563,4 +563,4 @@ inline WrapperType create_string_wrapper(const DataTypePtr& from_type) { }; // namespace CastWrapper } // namespace doris::vectorized -#include "common/compile_check_end.h" \ No newline at end of file +#include "common/compile_check_end.h"