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 1060be5619 Row metadata stored as JSON keeps a field's origin and how 
its values… (#9558)
1060be5619 is described below

commit 1060be561934286584ebd9c38a3ecf4aa2ec7b37
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Thu Nov 19 11:17:17 2026 +0100

    Row metadata stored as JSON keeps a field's origin and how its values… 
(#9558)
    
    * Row metadata stored as JSON keeps a field's origin and how its values are 
written, read and compared, fixes #9557
    
    * fix remarks
---
 .../apache/hop/core/row/value/ValueMetaBase.java   | 136 +++++++++++-
 .../org/apache/hop/core/row/JsonRowMetaTest.java   | 227 +++++++++++++++++++++
 .../core/row/value/ValueMetaAvroRecordTest.java    |   7 +-
 3 files changed, 366 insertions(+), 4 deletions(-)

diff --git 
a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java 
b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
index d6d1a58de4..614564383f 100644
--- a/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
+++ b/core/src/main/java/org/apache/hop/core/row/value/ValueMetaBase.java
@@ -105,6 +105,26 @@ public class ValueMetaBase implements IValueMeta {
   private static final String CONST_STORAGE_META = "storage-meta";
   private static final String CONST_LENGTH = "length";
   private static final String CONST_PRECISION = "precision";
+
+  // Keys of the field settings in the JSON form of the metadata 
(storeMetaInJson)
+  private static final String JSON_ORIGIN = "origin";
+  private static final String JSON_COMMENTS = "comments";
+  private static final String JSON_DECIMAL_SYMBOL = "decimalSymbol";
+  private static final String JSON_GROUPING_SYMBOL = "groupingSymbol";
+  private static final String JSON_CURRENCY_SYMBOL = "currencySymbol";
+  private static final String JSON_ROUNDING_TYPE = "roundingType";
+  private static final String JSON_TRIM_TYPE = "trimType";
+  private static final String JSON_DATE_FORMAT_LENIENT = "dateFormatLenient";
+  private static final String JSON_DATE_FORMAT_LOCALE = "dateFormatLocale";
+  private static final String JSON_DATE_FORMAT_TIME_ZONE = 
"dateFormatTimeZone";
+  private static final String JSON_LENIENT_STRING_TO_NUMBER = 
"lenientStringToNumber";
+  private static final String JSON_STRING_ENCODING = "stringEncoding";
+  private static final String JSON_CASE_INSENSITIVE = "caseInsensitive";
+  private static final String JSON_COLLATOR_DISABLED = "collatorDisabled";
+  private static final String JSON_COLLATOR_LOCALE = "collatorLocale";
+  private static final String JSON_COLLATOR_STRENGTH = "collatorStrength";
+  private static final String JSON_SORTED_DESCENDING = "sortedDescending";
+  private static final String JSON_OUTPUT_PADDING = "outputPaddingEnabled";
   private static final String CONST_COLLATOR_STRENGTH = "collator_strength";
   private static final String CONST_FALSE = "false";
   private static final String CONST_CANNOT_CONVERT =
@@ -339,9 +359,7 @@ public class ValueMetaBase implements IValueMeta {
     this.dateFormatTimeZone = TimeZone.getDefault();
     this.identicalFormat = true;
     this.bigNumberFormatting = true;
-    this.lenientStringToNumber =
-        convertStringToBoolean(
-            
Const.NVL(System.getProperty(Const.HOP_LENIENT_STRING_TO_NUMBER_CONVERSION, 
"N"), "N"));
+    this.lenientStringToNumber = defaultLenientStringToNumber();
     this.emptyStringAndNullAreDifferent =
         convertStringToBoolean(
             
Const.NVL(System.getProperty(Const.HOP_EMPTY_STRING_DIFFERS_FROM_NULL, "N"), 
"N"));
@@ -6077,6 +6095,48 @@ public class ValueMetaBase implements IValueMeta {
     jValue.put(CONST_LENGTH, getLength());
     jValue.put(CONST_PRECISION, getPrecision());
     jValue.put("conversionMask", getConversionMask());
+    // The rest of the field's settings: where it comes from and how its 
values are written, read
+    // and compared.
+    jValue.put(JSON_ORIGIN, getOrigin());
+    jValue.put(JSON_COMMENTS, getComments());
+    jValue.put(JSON_ROUNDING_TYPE, getRoundingType());
+    jValue.put(JSON_TRIM_TYPE, (long) getTrimType());
+    jValue.put(JSON_DATE_FORMAT_LENIENT, isDateFormatLenient());
+    jValue.put(JSON_STRING_ENCODING, getStringEncoding());
+    jValue.put(JSON_CASE_INSENSITIVE, isCaseInsensitive());
+    jValue.put(JSON_COLLATOR_DISABLED, isCollatorDisabled());
+    jValue.put(JSON_COLLATOR_STRENGTH, (long) getCollatorStrength());
+
+    // The settings below start out as the regional settings of the JVM that 
creates the field.
+    // JSON row metadata is read on another machine (the Beam and Spark 
workers), which must use
+    // its own regional settings for a field nobody configured. So only a 
value that differs from
+    // this JVM's default is written: one somebody set. A setting deliberately 
equal to this
+    // JVM's default can't be told apart and is left to the reader as well.
+    putIfNotDefault(
+        jValue, JSON_DECIMAL_SYMBOL, getDecimalSymbol(), "" + 
Const.getDefaultDecimalSeparator());
+    putIfNotDefault(
+        jValue,
+        JSON_GROUPING_SYMBOL,
+        getGroupingSymbol(),
+        "" + Const.getDefaultGroupingSeparator());
+    putIfNotDefault(
+        jValue, JSON_CURRENCY_SYMBOL, getCurrencySymbol(), 
Const.getDefaultCurrencySymbol());
+    Locale formatLocale = Locale.getDefault(Locale.Category.FORMAT);
+    if (getDateFormatLocale() != null && 
!getDateFormatLocale().equals(formatLocale)) {
+      jValue.put(JSON_DATE_FORMAT_LOCALE, 
getDateFormatLocale().toLanguageTag());
+    }
+    if (getDateFormatTimeZone() != null
+        && 
!getDateFormatTimeZone().getID().equals(TimeZone.getDefault().getID())) {
+      jValue.put(JSON_DATE_FORMAT_TIME_ZONE, getDateFormatTimeZone().getID());
+    }
+    if (getCollatorLocale() != null && 
!getCollatorLocale().equals(formatLocale)) {
+      jValue.put(JSON_COLLATOR_LOCALE, getCollatorLocale().toLanguageTag());
+    }
+    if (isLenientStringToNumber() != defaultLenientStringToNumber()) {
+      jValue.put(JSON_LENIENT_STRING_TO_NUMBER, isLenientStringToNumber());
+    }
+    jValue.put(JSON_SORTED_DESCENDING, isSortedDescending());
+    jValue.put(JSON_OUTPUT_PADDING, isOutputPaddingEnabled());
   }
 
   @Override
@@ -6087,6 +6147,76 @@ public class ValueMetaBase implements IValueMeta {
     setPrecision((int) precision);
     String conversionMask = (String) jValue.get("conversionMask");
     setConversionMask(conversionMask);
+
+    // JSON written by older versions has none of the keys below: the defaults 
stay.
+    //
+    if (jValue.containsKey(JSON_ORIGIN)) {
+      setOrigin((String) jValue.get(JSON_ORIGIN));
+    }
+    if (jValue.containsKey(JSON_COMMENTS)) {
+      setComments((String) jValue.get(JSON_COMMENTS));
+    }
+    if (jValue.containsKey(JSON_DECIMAL_SYMBOL)) {
+      setDecimalSymbol((String) jValue.get(JSON_DECIMAL_SYMBOL));
+    }
+    if (jValue.containsKey(JSON_GROUPING_SYMBOL)) {
+      setGroupingSymbol((String) jValue.get(JSON_GROUPING_SYMBOL));
+    }
+    if (jValue.containsKey(JSON_CURRENCY_SYMBOL)) {
+      setCurrencySymbol((String) jValue.get(JSON_CURRENCY_SYMBOL));
+    }
+    if (jValue.containsKey(JSON_ROUNDING_TYPE)) {
+      setRoundingType((String) jValue.get(JSON_ROUNDING_TYPE));
+    }
+    if (jValue.get(JSON_TRIM_TYPE) instanceof Long trim) {
+      setTrimType(trim.intValue());
+    }
+    if (jValue.get(JSON_DATE_FORMAT_LENIENT) instanceof Boolean lenient) {
+      setDateFormatLenient(lenient);
+    }
+    if (jValue.get(JSON_DATE_FORMAT_LOCALE) instanceof String locale) {
+      setDateFormatLocale(Locale.forLanguageTag(locale));
+    }
+    if (jValue.get(JSON_DATE_FORMAT_TIME_ZONE) instanceof String timeZone) {
+      setDateFormatTimeZone(TimeZone.getTimeZone(timeZone));
+    }
+    if (jValue.get(JSON_LENIENT_STRING_TO_NUMBER) instanceof Boolean lenient) {
+      setLenientStringToNumber(lenient);
+    }
+    if (jValue.containsKey(JSON_STRING_ENCODING)) {
+      setStringEncoding((String) jValue.get(JSON_STRING_ENCODING));
+    }
+    if (jValue.get(JSON_CASE_INSENSITIVE) instanceof Boolean caseInsensitive) {
+      setCaseInsensitive(caseInsensitive);
+    }
+    if (jValue.get(JSON_COLLATOR_DISABLED) instanceof Boolean collatorOff) {
+      setCollatorDisabled(collatorOff);
+    }
+    // The locale creates the collator, the strength is set on it: locale 
first.
+    if (jValue.get(JSON_COLLATOR_LOCALE) instanceof String locale) {
+      setCollatorLocale(Locale.forLanguageTag(locale));
+    }
+    if (jValue.get(JSON_COLLATOR_STRENGTH) instanceof Long strength) {
+      setCollatorStrength(strength.intValue());
+    }
+    if (jValue.get(JSON_SORTED_DESCENDING) instanceof Boolean descending) {
+      setSortedDescending(descending);
+    }
+    if (jValue.get(JSON_OUTPUT_PADDING) instanceof Boolean padding) {
+      setOutputPaddingEnabled(padding);
+    }
+  }
+
+  private static void putIfNotDefault(
+      JSONObject jValue, String key, String value, String jvmDefault) {
+    if (!Objects.equals(value, jvmDefault)) {
+      jValue.put(key, value);
+    }
+  }
+
+  private static boolean defaultLenientStringToNumber() {
+    return convertStringToBoolean(
+        
Const.NVL(System.getProperty(Const.HOP_LENIENT_STRING_TO_NUMBER_CONVERSION, 
"N"), "N"));
   }
 
   public static final class TrimTypeCodeConverter implements IIntCodeConverter 
{
diff --git a/core/src/test/java/org/apache/hop/core/row/JsonRowMetaTest.java 
b/core/src/test/java/org/apache/hop/core/row/JsonRowMetaTest.java
new file mode 100644
index 0000000000..9cc5ea32f0
--- /dev/null
+++ b/core/src/test/java/org/apache/hop/core/row/JsonRowMetaTest.java
@@ -0,0 +1,227 @@
+/*
+ * 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.
+ */
+
+package org.apache.hop.core.row;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.text.Collator;
+import java.time.Instant;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+import org.apache.hop.core.HopClientEnvironment;
+import org.apache.hop.core.row.value.ValueMetaDate;
+import org.apache.hop.core.row.value.ValueMetaNumber;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.junit.rules.RestoreHopEnvironmentExtension;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Row metadata written as JSON (Beam engine, execution information) comes 
back as the fields that
+ * were written, as it does from the binary format.
+ */
+@ExtendWith(RestoreHopEnvironmentExtension.class)
+class JsonRowMetaTest {
+
+  @BeforeAll
+  static void init() throws Exception {
+    HopClientEnvironment.init();
+  }
+
+  @Test
+  void theFieldsKeepTheirTypeFormatAndOrigin() {
+    IRowMeta rowMeta = new RowMeta();
+    ValueMetaNumber price = new ValueMetaNumber("price", 9, 2);
+    price.setConversionMask("0.00");
+    price.setOrigin("read orders");
+    price.setComments("unit price, excluding VAT");
+    rowMeta.addValueMeta(price);
+    rowMeta.addValueMeta(new ValueMetaString("name"));
+
+    IRowMeta copy = JsonRowMeta.fromJson(JsonRowMeta.toJson(rowMeta));
+
+    assertEquals(2, copy.size());
+    IValueMeta copiedPrice = copy.getValueMeta(0);
+    assertEquals("price", copiedPrice.getName());
+    assertEquals(IValueMeta.TYPE_NUMBER, copiedPrice.getType());
+    assertEquals(9, copiedPrice.getLength());
+    assertEquals(2, copiedPrice.getPrecision());
+    assertEquals("0.00", copiedPrice.getConversionMask());
+    assertEquals("read orders", copiedPrice.getOrigin());
+    assertEquals("unit price, excluding VAT", copiedPrice.getComments());
+    assertNull(copy.getValueMeta(1).getOrigin());
+  }
+
+  /** Symbols, rounding, trimming, date settings and comparing: how the field 
is converted. */
+  @Test
+  void theFieldsKeepHowTheyAreWrittenReadAndCompared() {
+    IRowMeta rowMeta = settingsOtherThanTheDefaults();
+
+    IRowMeta copy = JsonRowMeta.fromJson(JsonRowMeta.toJson(rowMeta));
+
+    assertSettingsOtherThanTheDefaults(copy);
+    // Not part of the binary format, so checked here only
+    assertEquals("half_up", copy.getValueMeta(0).getRoundingType());
+    assertEquals("UTF-16", copy.getValueMeta(1).getStringEncoding());
+  }
+
+  /** The reference: the binary format already keeps all of these. */
+  @Test
+  void theBinaryFormatKeepsTheSameSettings() throws Exception {
+    IRowMeta rowMeta = settingsOtherThanTheDefaults();
+
+    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+    try (DataOutputStream out = new DataOutputStream(bytes)) {
+      rowMeta.writeMeta(out);
+    }
+    IRowMeta copy;
+    try (DataInputStream in = new DataInputStream(new 
ByteArrayInputStream(bytes.toByteArray()))) {
+      copy = new RowMeta(in);
+    }
+
+    assertSettingsOtherThanTheDefaults(copy);
+  }
+
+  /**
+   * Written on one machine (the Beam launcher), read on another (a worker): a 
field nobody
+   * configured formats with the reader's regional settings, not the writer's.
+   */
+  @Test
+  void aFieldNobodyConfiguredUsesTheRegionalSettingsOfTheReader() throws 
Exception {
+    Locale originalLocale = Locale.getDefault();
+    Locale originalFormat = Locale.getDefault(Locale.Category.FORMAT);
+    TimeZone originalTimeZone = TimeZone.getDefault();
+    try {
+      setRegionalSettings(Locale.forLanguageTag("nl-BE"), "Europe/Brussels");
+      IRowMeta rowMeta = new RowMeta();
+      rowMeta.addValueMeta(new ValueMetaNumber("amount"));
+      ValueMetaDate moment = new ValueMetaDate("moment");
+      moment.setConversionMask("yyyy-MM-dd HH:mm");
+      rowMeta.addValueMeta(moment);
+      // Set explicitly, equal to the default of the reader: still written
+      ValueMetaNumber price = new ValueMetaNumber("price");
+      price.setDecimalSymbol(".");
+      rowMeta.addValueMeta(price);
+      String json = JsonRowMeta.toJson(rowMeta);
+
+      setRegionalSettings(Locale.US, "UTC");
+      IRowMeta copy = JsonRowMeta.fromJson(json);
+
+      assertEquals("1.5", copy.getValueMeta(0).getString(1.5));
+      Date tenUtc = Date.from(Instant.parse("2025-11-19T10:00:00Z"));
+      assertEquals("2025-11-19 10:00", copy.getValueMeta(1).getString(tenUtc));
+      assertEquals(".", copy.getValueMeta(2).getDecimalSymbol());
+    } finally {
+      Locale.setDefault(originalLocale);
+      Locale.setDefault(Locale.Category.FORMAT, originalFormat);
+      TimeZone.setDefault(originalTimeZone);
+    }
+  }
+
+  private static void setRegionalSettings(Locale locale, String timeZone) {
+    Locale.setDefault(locale);
+    Locale.setDefault(Locale.Category.FORMAT, locale);
+    TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
+  }
+
+  /** JSON written before these settings were stored loads with the defaults, 
as before. */
+  @Test
+  void olderJsonStillLoadsWithTheDefaults() {
+    String json =
+        
"{\"values\":[{\"name\":\"id\",\"type\":5,\"length\":-1,\"precision\":-1,"
+            + "\"conversionMask\":null}]}";
+
+    IValueMeta id = JsonRowMeta.fromJson(json).getValueMeta(0);
+    IValueMeta defaults = new ValueMetaString("defaults");
+
+    assertEquals("id", id.getName());
+    assertEquals(IValueMeta.TYPE_INTEGER, id.getType());
+    assertNull(id.getOrigin());
+    assertNull(id.getComments());
+    assertEquals(defaults.getTrimType(), id.getTrimType());
+    assertEquals(defaults.isCaseInsensitive(), id.isCaseInsensitive());
+    assertEquals(defaults.isSortedDescending(), id.isSortedDescending());
+    assertEquals(defaults.isDateFormatLenient(), id.isDateFormatLenient());
+    assertEquals(defaults.isLenientStringToNumber(), 
id.isLenientStringToNumber());
+    assertEquals(defaults.isCollatorDisabled(), id.isCollatorDisabled());
+    assertEquals(defaults.isOutputPaddingEnabled(), 
id.isOutputPaddingEnabled());
+  }
+
+  private static IRowMeta settingsOtherThanTheDefaults() {
+    IRowMeta rowMeta = new RowMeta();
+
+    ValueMetaNumber price = new ValueMetaNumber("price");
+    price.setConversionMask("#,##0.00");
+    price.setDecimalSymbol(",");
+    price.setGroupingSymbol(".");
+    price.setCurrencySymbol("EUR");
+    price.setRoundingType("half_up");
+    price.setOutputPaddingEnabled(true);
+    rowMeta.addValueMeta(price);
+
+    ValueMetaString name = new ValueMetaString("name");
+    name.setTrimType(IValueMeta.TRIM_TYPE_BOTH);
+    name.setCaseInsensitive(true);
+    name.setSortedDescending(true);
+    name.setStringEncoding("UTF-16");
+    name.setCollatorDisabled(false);
+    name.setCollatorLocale(Locale.forLanguageTag("fr-FR"));
+    name.setCollatorStrength(Collator.SECONDARY);
+    rowMeta.addValueMeta(name);
+
+    ValueMetaDate day = new ValueMetaDate("day");
+    day.setDateFormatLenient(true);
+    day.setDateFormatLocale(Locale.forLanguageTag("nl-BE"));
+    day.setDateFormatTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
+    day.setLenientStringToNumber(true);
+    rowMeta.addValueMeta(day);
+
+    return rowMeta;
+  }
+
+  private static void assertSettingsOtherThanTheDefaults(IRowMeta copy) {
+    IValueMeta price = copy.getValueMeta(0);
+    assertEquals(",", price.getDecimalSymbol());
+    assertEquals(".", price.getGroupingSymbol());
+    assertEquals("EUR", price.getCurrencySymbol());
+    assertTrue(price.isOutputPaddingEnabled());
+
+    IValueMeta name = copy.getValueMeta(1);
+    assertEquals(IValueMeta.TRIM_TYPE_BOTH, name.getTrimType());
+    assertTrue(name.isCaseInsensitive());
+    assertTrue(name.isSortedDescending());
+    assertFalse(name.isCollatorDisabled());
+    assertEquals(Locale.forLanguageTag("fr-FR"), name.getCollatorLocale());
+    assertEquals(Collator.SECONDARY, name.getCollatorStrength());
+
+    IValueMeta day = copy.getValueMeta(2);
+    assertTrue(day.isDateFormatLenient());
+    assertEquals(Locale.forLanguageTag("nl-BE"), day.getDateFormatLocale());
+    assertEquals("Asia/Tokyo", day.getDateFormatTimeZone().getID());
+    assertTrue(day.isLenientStringToNumber());
+  }
+}
diff --git 
a/core/src/test/java/org/apache/hop/core/row/value/ValueMetaAvroRecordTest.java 
b/core/src/test/java/org/apache/hop/core/row/value/ValueMetaAvroRecordTest.java
index db6ebffb7f..0a7a165ffb 100644
--- 
a/core/src/test/java/org/apache/hop/core/row/value/ValueMetaAvroRecordTest.java
+++ 
b/core/src/test/java/org/apache/hop/core/row/value/ValueMetaAvroRecordTest.java
@@ -188,7 +188,12 @@ class ValueMetaAvroRecordTest {
                 + 
"[\"long\",\"null\"]},{\"name\":\"str\",\"type\":[\"string\",\"null\"]},"
                 + 
"{\"name\":\"uuid\",\"type\":[\"string\",\"null\"]}]},\"precision\":-1,\"name\":"
                 + 
"\"test\",\"length\":-1,\"conversionMask\":null,\"type\":20}");
-    assertEquals(valueNode, expectJsonNode);
+    // The base class adds the field's formatting settings, which depend on 
the default locale:
+    // check the schema and the basics only.
+    expectJsonNode
+        .fieldNames()
+        .forEachRemaining(
+            field -> assertEquals(expectJsonNode.get(field), 
valueNode.get(field), field));
 
     // Read it back...
     //

Reply via email to