This is an automated email from the ASF dual-hosted git repository.

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 2a96827cfd Script transform truncates a BigNumber result to a whole 
number, fixe… (#9303)
2a96827cfd is described below

commit 2a96827cfdab5cecac97c75ca721480bf74e78dc
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Fri Nov 6 20:17:14 2026 +0100

    Script transform truncates a BigNumber result to a whole number, fixe… 
(#9303)
    
    * Script transform truncates a BigNumber result to a whole number, fixes 
#9302
    
    * fix remarks
---
 .../hop/pipeline/transforms/script/Script.java     | 55 ++++++++++++++------
 .../script/messages/messages_en_US.properties      |  1 +
 .../hop/pipeline/transforms/script/ScriptTest.java | 58 ++++++++++++++++++++--
 3 files changed, 95 insertions(+), 19 deletions(-)

diff --git 
a/plugins/transforms/script/src/main/java/org/apache/hop/pipeline/transforms/script/Script.java
 
b/plugins/transforms/script/src/main/java/org/apache/hop/pipeline/transforms/script/Script.java
index 9e2554dc89..e0254860e0 100644
--- 
a/plugins/transforms/script/src/main/java/org/apache/hop/pipeline/transforms/script/Script.java
+++ 
b/plugins/transforms/script/src/main/java/org/apache/hop/pipeline/transforms/script/Script.java
@@ -19,6 +19,7 @@
 package org.apache.hop.pipeline.transforms.script;
 
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Date;
 import javax.script.Bindings;
 import javax.script.Compilable;
@@ -36,6 +37,7 @@ import org.apache.hop.core.row.IRowMeta;
 import org.apache.hop.core.row.IValueMeta;
 import org.apache.hop.core.row.RowDataUtil;
 import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaNumber;
 import org.apache.hop.core.util.Utils;
 import org.apache.hop.core.xml.XmlHandler;
 import org.apache.hop.i18n.BaseMessages;
@@ -54,6 +56,11 @@ import org.apache.hop.pipeline.transform.TransformMeta;
 public class Script extends BaseTransform<ScriptMeta, ScriptData> implements 
ITransform {
   private static final Class<?> PKG = Script.class;
 
+  /**
+   * Converts a Double script result to a BigNumber; NORMAL storage keeps no 
state, so it is shared.
+   */
+  private static final IValueMeta DOUBLE_RESULT_META = new 
ValueMetaNumber("scriptResult");
+
   public static final int ABORT_PIPELINE = -1;
   public static final int ERROR_PIPELINE = -2;
   public static final int CONTINUE_PIPELINE = 0;
@@ -548,8 +555,7 @@ public class Script extends BaseTransform<ScriptMeta, 
ScriptData> implements ITr
                 return null;
               } else if 
(classType.equalsIgnoreCase(CONST_ORG_MOZILLA_JAVASCRIPT_NATIVE_NUMBER)) {
                 Number nb = (Number) result;
-                // sure
-                return new BigDecimal(nb.longValue());
+                return floatingPointToBigNumber(field, nb.doubleValue());
               } else if (classType.equalsIgnoreCase(
                   CONST_ORG_MOZILLA_JAVASCRIPT_NATIVE_JAVA_OBJECT)) {
                 // Is it a BigDecimal class ?
@@ -559,18 +565,24 @@ public class Script extends BaseTransform<ScriptMeta, 
ScriptData> implements ITr
                   String string = (String) result;
                   return new BigDecimal(string);
                 }
-              } else if (classType.equalsIgnoreCase("java.lang.Byte")) {
-                return new BigDecimal(((java.lang.Byte) result).longValue());
-              } else if (classType.equalsIgnoreCase("java.lang.Short")) {
-                return new BigDecimal(((Short) result).longValue());
-              } else if (classType.equalsIgnoreCase("java.lang.Integer")) {
-                return new BigDecimal(((Integer) result).longValue());
-              } else if (classType.equalsIgnoreCase("java.lang.Long")) {
-                return new BigDecimal((Long) result);
-              } else if (classType.equalsIgnoreCase(CONST_JAVA_LANG_DOUBLE)) {
-                return new BigDecimal(((Double) result).longValue());
-              } else if (classType.equalsIgnoreCase("java.lang.String")) {
-                return new BigDecimal(Long.parseLong((String) result));
+              } else if (result instanceof BigDecimal bigDecimal) {
+                // Groovy decimal literals and arithmetic
+                return bigDecimal;
+              } else if (result instanceof BigInteger bigInteger) {
+                // Groovy, Jython and JavaScript BigInt
+                return new BigDecimal(bigInteger);
+              } else if (result instanceof Double || result instanceof Float) {
+                // A Float goes through its own text, so 0.1f stays 0.1 
instead of 0.10000000149...
+                double value =
+                    result instanceof Float
+                        ? Double.parseDouble(result.toString())
+                        : (Double) result;
+                return floatingPointToBigNumber(field, value);
+              } else if (result instanceof Number number) {
+                // Byte, Short, Integer, Long
+                return BigDecimal.valueOf(number.longValue());
+              } else if (result instanceof String string) {
+                return new BigDecimal(string);
               } else {
                 throw new HopRuntimeException(
                     "JavaScript conversion to BigNumber not implemented for " 
+ classType);
@@ -608,6 +620,21 @@ public class Script extends BaseTransform<ScriptMeta, 
ScriptData> implements ITr
     }
   }
 
+  /**
+   * A floating point script result as a BigNumber, converted the way any 
Number field is (no
+   * scientific notation for whole numbers from 1e7 upwards). NaN and 
Infinity, which no BigDecimal
+   * can hold, fail with the field name instead of a bare 
NumberFormatException.
+   */
+  private static BigDecimal floatingPointToBigNumber(ScriptMeta.SField field, 
double value)
+      throws HopValueException {
+    if (Double.isNaN(value) || Double.isInfinite(value)) {
+      throw new HopValueException(
+          BaseMessages.getString(
+              PKG, "Script.Exception.NotFiniteBigNumber", field.getName(), 
Double.toString(value)));
+    }
+    return DOUBLE_RESULT_META.getBigNumber(value);
+  }
+
   private void processEndOfStream() {
     try {
       if (bindings != null) {
diff --git 
a/plugins/transforms/script/src/main/resources/org/apache/hop/pipeline/transforms/script/messages/messages_en_US.properties
 
b/plugins/transforms/script/src/main/resources/org/apache/hop/pipeline/transforms/script/messages/messages_en_US.properties
index e905567435..c5c8bb437c 100644
--- 
a/plugins/transforms/script/src/main/resources/org/apache/hop/pipeline/transforms/script/messages/messages_en_US.properties
+++ 
b/plugins/transforms/script/src/main/resources/org/apache/hop/pipeline/transforms/script/messages/messages_en_US.properties
@@ -18,6 +18,7 @@
 
 Script.Exception.CouldNotExecuteScript=Couldn''t execute this script! Error on 
position {0} :
 Script.Exception.ErrorSettingVariable=Error while setting variable
+Script.Exception.NotFiniteBigNumber=Field [{0}]: the script returned {1}, 
which is not a valid BigNumber
 Script.Log.CouldNotAddDefaultConstants=Couldn''t add default constants to the 
environment.
 Script.Log.CouldNotAttachAdditionalScripts=Could not attach additional scripts 
to the environment.
 Script.Log.CouldNotAttachTransVars=Unable to attach transformation variables
diff --git 
a/plugins/transforms/script/src/test/java/org/apache/hop/pipeline/transforms/script/ScriptTest.java
 
b/plugins/transforms/script/src/test/java/org/apache/hop/pipeline/transforms/script/ScriptTest.java
index c54200a5fe..07afe3fdfa 100644
--- 
a/plugins/transforms/script/src/test/java/org/apache/hop/pipeline/transforms/script/ScriptTest.java
+++ 
b/plugins/transforms/script/src/test/java/org/apache/hop/pipeline/transforms/script/ScriptTest.java
@@ -30,6 +30,7 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Date;
 import java.util.concurrent.TimeUnit;
 import org.apache.hop.core.IRowSet;
@@ -194,13 +195,10 @@ class ScriptTest {
 
   @Test
   void getValueFromScript_bignumberType_bigDecimal_returnsBigDecimal() throws 
HopValueException {
+    // Groovy: 2.5 * 2 is a java.math.BigDecimal
     meta.getFields().add(createField("bn", "BigNumber"));
-    // Script.getValueFromScript TYPE_BIGNUMBER handles NativeNumber, 
NativeJavaObject, boxed
-    // primitives, String;
-    // java.math.BigDecimal falls through to default and throws (could be 
extended to handle
-    // BigDecimal).
     BigDecimal bd = new BigDecimal("123.45");
-    assertThrows(HopValueException.class, () -> script.getValueFromScript(bd, 
0));
+    assertEquals(bd, script.getValueFromScript(bd, 0));
   }
 
   // --- putRow: Integer normalized to Long ---
@@ -307,6 +305,56 @@ class ScriptTest {
     assertEquals(new BigDecimal("999"), script.getValueFromScript("999", 0));
   }
 
+  @Test
+  void getValueFromScript_bignumberType_double_keepsFraction() throws 
HopValueException {
+    meta.getFields().add(createField("bn", "BigNumber"));
+    assertEquals(
+        0, new BigDecimal("2.75").compareTo((BigDecimal) 
script.getValueFromScript(2.75d, 0)));
+  }
+
+  @Test
+  void getValueFromScript_bignumberType_stringWithFraction_keepsFraction()
+      throws HopValueException {
+    meta.getFields().add(createField("bn", "BigNumber"));
+    assertEquals(new BigDecimal("12.345"), script.getValueFromScript("12.345", 
0));
+  }
+
+  @Test
+  void getValueFromScript_bignumberType_wholeDouble_plainNotation() throws 
HopValueException {
+    // GraalJS returns a Double for 2.5 * 2e7; BigDecimal.valueOf would give 
5.0E+7
+    meta.getFields().add(createField("bn", "BigNumber"));
+    BigDecimal value = (BigDecimal) script.getValueFromScript(5.0E7d, 0);
+    assertEquals("50000000", value.toString());
+    assertEquals("10000000000", script.getValueFromScript(1e10d, 
0).toString());
+  }
+
+  @Test
+  void getValueFromScript_bignumberType_float_keepsShortestDecimal() throws 
HopValueException {
+    meta.getFields().add(createField("bn", "BigNumber"));
+    assertEquals(new BigDecimal("0.1"), script.getValueFromScript(0.1f, 0));
+  }
+
+  @Test
+  void getValueFromScript_bignumberType_bigInteger_keepsAllDigits() throws 
HopValueException {
+    // Groovy 10G**30, Jython 10**30, JavaScript 10n**30n
+    meta.getFields().add(createField("bn", "BigNumber"));
+    BigInteger big = BigInteger.TEN.pow(30);
+    assertEquals(new BigDecimal(big), script.getValueFromScript(big, 0));
+  }
+
+  @Test
+  void getValueFromScript_bignumberType_nanOrInfinity_failsNamingTheField() {
+    meta.getFields().add(createField("bn", "BigNumber"));
+    HopValueException nan =
+        assertThrows(HopValueException.class, () -> 
script.getValueFromScript(Double.NaN, 0));
+    assertTrue(nan.getMessage().contains("[bn]"), nan.getMessage());
+    assertTrue(nan.getMessage().contains("NaN"), nan.getMessage());
+    HopValueException infinity =
+        assertThrows(
+            HopValueException.class, () -> 
script.getValueFromScript(Double.POSITIVE_INFINITY, 0));
+    assertTrue(infinity.getMessage().contains("Infinity"), 
infinity.getMessage());
+  }
+
   @Test
   void getValueFromScript_binaryType_returnsResult() throws HopValueException {
     meta.getFields().add(createField("bin", "Binary"));

Reply via email to