SteNicholas commented on code in PR #203:
URL: https://github.com/apache/paimon-cpp/pull/203#discussion_r3781204349


##########
src/paimon/common/utils/saturating_cast.h:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <cmath>
+#include <limits>
+#include <type_traits>
+
+namespace paimon {
+
+/// Converts a double to a signed integral type with the saturation policy 
docs/code-style.md
+/// requires (the one Java applies and 
NumericPrimitiveCastExecutor::JavaFloatingToIntegerCast
+/// implements in core/): NaN converts to 0 and an out-of-range value 
saturates at the bounds
+/// of TargetType. A bare static_cast of an unrepresentable double is 
undefined behavior and
+/// diverges across architectures (x86 cvttsd2si yields the "integer 
indefinite" value, aarch64
+/// fcvtzs saturates), so doubles that are not provably in range must go 
through this helper.
+template <typename TargetType>
+inline TargetType SaturatingDoubleToInteger(double value) {
+    static_assert(std::is_integral_v<TargetType> && 
std::is_signed_v<TargetType>,

Review Comment:
   This accepts every signed integral type, but it does not implement the 
Java/Paimon policy claimed above for narrow targets. For example, 
SaturatingDoubleToInteger<int8_t>(300.9) returns 127, while Java first converts 
to int32_t and then narrows the low bits, producing 44; a huge value similarly 
yields 127 here instead of -1. Please either implement the existing 
wide-then-narrow behavior for int8_t/int16_t, or restrict TargetType to the 
currently intended int32_t and int64_t instantiations and document that 
narrower contract.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to