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 9f4f2a66980 branch-4.0: [refine](float) Add NormalizeFloat to handle 
special values of floating-point numbers. #56937 (#57038)
9f4f2a66980 is described below

commit 9f4f2a66980894ad4b6ff0682e8c533171c04f19
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Oct 16 19:40:49 2025 +0800

    branch-4.0: [refine](float) Add NormalizeFloat to handle special values of 
floating-point numbers. #56937 (#57038)
    
    Cherry-picked from #56937
    
    Co-authored-by: Mryange <[email protected]>
---
 be/src/common/compare.h                           | 12 ++++++
 be/src/vec/columns/column_vector.cpp              | 13 +-----
 be/src/vec/functions/function_jsonb_transform.cpp |  8 +---
 be/test/common/compare_test.cpp                   | 51 +++++++++++++++++++++++
 4 files changed, 65 insertions(+), 19 deletions(-)

diff --git a/be/src/common/compare.h b/be/src/common/compare.h
index c93bec31bd1..6649700664d 100644
--- a/be/src/common/compare.h
+++ b/be/src/common/compare.h
@@ -223,6 +223,18 @@ template <>
 inline bool EqualTo<float>::operator()(const float& lhs, const float& rhs) 
const {
     return Compare::equal(lhs, rhs);
 }
+
+template <typename T>
+    requires(std::is_floating_point_v<T>)
+void NormalizeFloat(T& val) {
+    if (val == (T)0.0) {
+        // Turn negative zero into positive zero
+        val = (T)0.0;
+    } else if (std::isnan(val)) {
+        val = std::numeric_limits<T>::quiet_NaN();
+    }
+}
+
 } // namespace doris
 
 #include "common/compile_check_end.h"
\ No newline at end of file
diff --git a/be/src/vec/columns/column_vector.cpp 
b/be/src/vec/columns/column_vector.cpp
index 70534f13e59..68cc4ebfbd2 100644
--- a/be/src/vec/columns/column_vector.cpp
+++ b/be/src/vec/columns/column_vector.cpp
@@ -443,20 +443,9 @@ void ColumnVector<T>::replace_column_null_data(const 
uint8_t* __restrict null_ma
 template <PrimitiveType T>
 void ColumnVector<T>::replace_float_special_values() {
     if constexpr (is_float_or_double(T)) {
-        static constexpr float f_neg_zero = -0.0F;
-        static constexpr double d_neg_zero = -0.0;
-        static constexpr size_t byte_size = sizeof(value_type);
-        static const void* p_neg_zero = (byte_size == 4 ? static_cast<const 
void*>(&f_neg_zero)
-                                                        : static_cast<const 
void*>(&d_neg_zero));
         auto s = size();
-        auto* data_ptr = data.data();
         for (size_t i = 0; i < s; ++i) {
-            // replace negative zero with positive zero
-            if (0 == std::memcmp(data_ptr + i, p_neg_zero, byte_size)) {
-                data[i] = 0.0;
-            } else if (is_nan(data[i])) {
-                data[i] = std::numeric_limits<value_type>::quiet_NaN();
-            }
+            NormalizeFloat(data[i]);
         }
     }
 }
diff --git a/be/src/vec/functions/function_jsonb_transform.cpp 
b/be/src/vec/functions/function_jsonb_transform.cpp
index 17ef600c48a..34b9ca97467 100644
--- a/be/src/vec/functions/function_jsonb_transform.cpp
+++ b/be/src/vec/functions/function_jsonb_transform.cpp
@@ -87,13 +87,7 @@ void normalize_json_numbers_to_double(JsonbWriter& 
jsonb_writer, const JsonbValu
             CastParameters params;
             params.is_strict = false;
             JsonbCast::cast_from_json_to_float(jsonb_value, to, params);
-            if (to == 0.0) {
-                // to avoid -0.0
-                to = 0.0;
-            } else if (std::isnan(to)) {
-                // to avoid -nan
-                to = std::numeric_limits<double>::quiet_NaN();
-            }
+            NormalizeFloat(to);
             jsonb_writer.writeDouble(to);
         } else {
             jsonb_writer.writeValue(jsonb_value);
diff --git a/be/test/common/compare_test.cpp b/be/test/common/compare_test.cpp
new file mode 100644
index 00000000000..6b875012569
--- /dev/null
+++ b/be/test/common/compare_test.cpp
@@ -0,0 +1,51 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "common/compare.h"
+
+#include <gtest/gtest.h>
+
+#include <vector>
+
+#include "common/status.h"
+
+namespace doris {
+
+TEST(CompareTest, test) {
+    {
+        double x = -0.0;
+        NormalizeFloat(x);
+        EXPECT_EQ(x, 0.0);
+    }
+    {
+        float x = -0.0F;
+        NormalizeFloat(x);
+        EXPECT_EQ(x, 0.0F);
+    }
+    {
+        double x = std::nan("");
+        NormalizeFloat(x);
+        EXPECT_TRUE(std::isnan(x));
+    }
+    {
+        float x = std::nanf("");
+        NormalizeFloat(x);
+        EXPECT_TRUE(std::isnan(x));
+    }
+}
+
+} // namespace doris


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to