This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new b21ef786e53 branch-4.0: [fix](float) Fix float field `to_string`
(#59737)
b21ef786e53 is described below
commit b21ef786e53b0face0a991fde063e5b618c0bd7f
Author: Qi Chen <[email protected]>
AuthorDate: Mon Jan 12 09:27:36 2026 +0800
branch-4.0: [fix](float) Fix float field `to_string` (#59737)
Cherry-pick #58176
Co-authored-by: Gabriel <[email protected]>
---
be/src/vec/core/field.cpp | 83 ++++++++++++------------------
be/src/vec/functions/cast/cast_to_string.h | 4 +-
2 files changed, 36 insertions(+), 51 deletions(-)
diff --git a/be/src/vec/core/field.cpp b/be/src/vec/core/field.cpp
index b378885ae65..9a0004dcd46 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<typename
PrimitiveTypeTraits<primite_type>::NearestFieldType>(); \
+ return CastToString::from_number(v);
\
+ }
+
+#define MATCH_DECIMAL_TYPE(primite_type)
\
if (type == primite_type) {
\
const auto& v = get<typename
PrimitiveTypeTraits<primite_type>::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<String>();
return {s.data(), s.size()};
}
- if (type == TYPE_DECIMAL32) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DECIMAL32>::NearestFieldType>();
- return v.get_value().to_string(v.get_scale());
- }
- if (type == TYPE_DECIMAL64) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DECIMAL64>::NearestFieldType>();
- return v.get_value().to_string(v.get_scale());
- }
- if (type == TYPE_DECIMALV2) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DECIMALV2>::NearestFieldType>();
- return v.get_value().to_string(v.get_scale());
- }
- if (type == TYPE_DECIMAL128I) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DECIMAL128I>::NearestFieldType>();
- return v.get_value().to_string(v.get_scale());
- }
- if (type == TYPE_DECIMAL256) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DECIMAL256>::NearestFieldType>();
- return v.get_value().to_string(v.get_scale());
- }
- if (type == TYPE_LARGEINT) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_LARGEINT>::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<typename
PrimitiveTypeTraits<TYPE_DATE>::NearestFieldType>();
- std::string buf(40, 0);
- auto* to = binary_cast<int64_t,
doris::VecDateTimeValue>(v).to_string(buf.data());
- buf.resize(to - buf.data() - 1);
- return buf;
+ const auto& v = binary_cast<int64_t, doris::VecDateTimeValue>(
+ get<typename
PrimitiveTypeTraits<TYPE_DATE>::NearestFieldType>());
+ return CastToString::from_date_or_datetime(v);
}
if (type == TYPE_DATEV2) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DATEV2>::NearestFieldType>();
- return binary_cast<uint32_t,
DateV2Value<DateV2ValueType>>((uint32_t)v).to_string();
+ const auto& v = binary_cast<uint32_t, DateV2Value<DateV2ValueType>>(
+ (uint32_t)get<typename
PrimitiveTypeTraits<TYPE_DATEV2>::NearestFieldType>());
+ return CastToString::from_datev2(v);
}
if (type == TYPE_DATETIMEV2) {
- const auto& v = get<typename
PrimitiveTypeTraits<TYPE_DATETIMEV2>::NearestFieldType>();
- return binary_cast<uint64_t,
DateV2Value<DateTimeV2ValueType>>(v).to_string();
+ const auto& v = binary_cast<uint64_t,
DateV2Value<DateTimeV2ValueType>>(
+ (uint64_t)get<typename
PrimitiveTypeTraits<TYPE_DATETIMEV2>::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<TYPE_NULL>(
\
diff --git a/be/src/vec/functions/cast/cast_to_string.h
b/be/src/vec/functions/cast/cast_to_string.h
index 1fa1e72a0f7..0a2b9d08846 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<DateV2ValueType>& from,
BufferWritable& bw);
static inline std::string from_datetimev2(const
DateV2Value<DateTimeV2ValueType>& 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<DateTimeV2ValueType>&
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"
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]