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 bd9aed9af4 XML join declares the fields it writes, fixes #9511 (#9520)
bd9aed9af4 is described below

commit bd9aed9af4ed272216d198495b50122d58ced3b1
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Tue Nov 17 10:33:38 2026 +0100

    XML join declares the fields it writes, fixes #9511 (#9520)
    
    * XML join declares the fields it writes, fixes #9511
    
    * fix remarks
---
 .../transforms/xml/xmljoin/XmlJoinMeta.java        | 48 +++++++++++-
 .../xml/xmljoin/XmlJoinMetaGetFieldsTest.java      | 88 ++++++++++++++++++++++
 2 files changed, 134 insertions(+), 2 deletions(-)

diff --git 
a/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java
 
b/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java
index dd822b2866..6f8c257175 100644
--- 
a/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java
+++ 
b/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java
@@ -107,6 +107,40 @@ public class XmlJoinMeta extends 
BaseTransformMeta<XmlJoin, XmlJoinData> {
     encoding = Const.UTF_8;
   }
 
+  /**
+   * At design time the info streams of this transform have no transform set, 
so Hop passes an empty
+   * row for the target. Look the target transform up by name and use its 
fields instead.
+   */
+  @Override
+  public void getFields(
+      PipelineMeta pipelineMeta,
+      IRowMeta row,
+      String name,
+      IRowMeta[] info,
+      TransformMeta nextTransform,
+      IVariables variables,
+      IHopMetadataProvider metadataProvider)
+      throws HopTransformException {
+    if (!isKnown(info, 0) && pipelineMeta != null) {
+      TransformMeta targetTransform = 
pipelineMeta.findTransform(getTargetXmlTransform());
+      if (targetTransform != null) {
+        IRowMeta[] withTarget =
+            new IRowMeta[] {
+              pipelineMeta.getTransformFields(variables, targetTransform),
+              info != null && info.length > 1 ? info[1] : null
+            };
+        getFields(row, name, withTarget, nextTransform, variables, 
metadataProvider);
+        return;
+      }
+    }
+    getFields(row, name, info, nextTransform, variables, metadataProvider);
+  }
+
+  /** An empty row is the row of a stream without a transform: its fields are 
not known. */
+  private static boolean isKnown(IRowMeta[] info, int index) {
+    return info != null && info.length > index && info[index] != null && 
!info[index].isEmpty();
+  }
+
   @Override
   public void getFields(
       IRowMeta row,
@@ -120,6 +154,16 @@ public class XmlJoinMeta extends 
BaseTransformMeta<XmlJoin, XmlJoinData> {
     IValueMeta v = new 
ValueMetaString(variables.resolve(this.getValueXmlField()));
     v.setOrigin(name);
 
+    if (isKnown(info, 0)) {
+      IRowMeta knownTarget = info[0];
+      // The target's row with the joined XML, as processRow() builds it, 
whatever rows the
+      // previous transforms give here
+      IRowMeta target = knownTarget.clone();
+      row.clear();
+      row.addRowMeta(target);
+      row.addValueMeta(v);
+      return;
+    }
     try {
       /*
          Row should only include fields from the target and not the source. 
During the preview table
@@ -127,11 +171,11 @@ public class XmlJoinMeta extends 
BaseTransformMeta<XmlJoin, XmlJoinData> {
          remove the source fields.
       */
       List<String> targetFieldNames = null;
-      IRowMeta targetRowMeta = info[0];
+      IRowMeta targetRowMeta = info != null && info.length > 0 ? info[0] : 
null;
       if (targetRowMeta != null) {
         targetFieldNames = Arrays.asList(targetRowMeta.getFieldNames());
       }
-      IRowMeta sourceRowMeta = info[1];
+      IRowMeta sourceRowMeta = info != null && info.length > 1 ? info[1] : 
null;
       if (sourceRowMeta != null) {
         for (String fieldName : sourceRowMeta.getFieldNames()) {
           if (targetFieldNames == null || 
!targetFieldNames.contains(fieldName)) {
diff --git 
a/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java
 
b/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java
index 5ac27f6d7f..26e9a607b0 100644
--- 
a/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java
+++ 
b/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java
@@ -20,12 +20,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
+import java.util.List;
 import org.apache.hop.core.HopClientEnvironment;
 import org.apache.hop.core.row.IRowMeta;
+import org.apache.hop.core.row.IValueMeta;
 import org.apache.hop.core.row.RowMeta;
+import org.apache.hop.core.row.value.ValueMetaInteger;
 import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.core.variables.IVariables;
 import org.apache.hop.core.variables.Variables;
+import org.apache.hop.metadata.api.IHopMetadataProvider;
+import org.apache.hop.pipeline.PipelineHopMeta;
 import org.apache.hop.pipeline.PipelineMeta;
+import org.apache.hop.pipeline.transform.BaseTransformMeta;
 import org.apache.hop.pipeline.transform.TransformMeta;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -150,4 +157,85 @@ class XmlJoinMetaGetFieldsTest {
     assertEquals("c", strings[1]);
     assertEquals("result xml field name", strings[2]);
   }
+
+  /**
+   * The previous fields are merged in the order of the hops: with the 
source's hop first, a field
+   * both streams have keeps the source's type, and the fields come in the 
source's order. The
+   * transform writes the target's row, so that is what it declares.
+   */
+  @Test
+  void testGetFieldsGiveTheTargetRowWhateverTheOrderOfThePreviousFields() 
throws Exception {
+    RowMeta source = new RowMeta();
+    source.addValueMeta(new ValueMetaInteger("c"));
+    source.addValueMeta(new ValueMetaString("a"));
+    RowMeta target = new RowMeta();
+    target.addValueMeta(new ValueMetaString("a"));
+    target.addValueMeta(new ValueMetaString("c"));
+    // As PipelineMeta merges the fields of the previous transforms, the 
source's first
+    RowMeta rowMeta = source.clone();
+    xmlJoinMeta.setValueXmlField("xml");
+
+    xmlJoinMeta.getFields(
+        rowMeta, "join", new IRowMeta[] {target, source}, null, new 
Variables(), null);
+
+    assertEquals(List.of("a", "c", "xml"), List.of(rowMeta.getFieldNames()));
+    assertEquals(IValueMeta.TYPE_STRING, rowMeta.getValueMeta(1).getType());
+  }
+
+  /**
+   * At design time Hop gives an empty row for the target stream: the info 
streams of the XML join
+   * have no transform set. The fields are those of the target transform, 
found by name, plus the
+   * XML field, whatever the order of the hops.
+   */
+  @Test
+  void testGetFieldsAtDesignTimeGiveTheTargetRow() throws Exception {
+    PipelineMeta designTime = new PipelineMeta();
+    TransformMeta target =
+        new TransformMeta(
+            "Fields",
+            "target",
+            new DeclaresFields(new ValueMetaString("a"), new 
ValueMetaString("xml")));
+    TransformMeta source =
+        new TransformMeta("Fields", "source", new DeclaresFields(new 
ValueMetaInteger("b")));
+    xmlJoinMeta.setTargetXmlTransform("target");
+    xmlJoinMeta.setSourceXmlTransform("source");
+    xmlJoinMeta.setTargetXmlField("xml");
+    xmlJoinMeta.setValueXmlField("out");
+    TransformMeta join = new TransformMeta("XMLJoin", "join", xmlJoinMeta);
+    designTime.addTransform(target);
+    designTime.addTransform(source);
+    designTime.addTransform(join);
+    // The source's hop first: its fields come first in the merged previous 
fields
+    designTime.addPipelineHop(new PipelineHopMeta(source, join));
+    designTime.addPipelineHop(new PipelineHopMeta(target, join));
+    xmlJoinMeta.searchInfoAndTargetTransforms(designTime.getTransforms());
+
+    IRowMeta fields = designTime.getTransformFields(new Variables(), join);
+
+    assertEquals(List.of("a", "xml", "out"), List.of(fields.getFieldNames()));
+  }
+
+  /** A transform that only declares the fields it is given. */
+  private static class DeclaresFields extends BaseTransformMeta {
+    private final IValueMeta[] fields;
+
+    DeclaresFields(IValueMeta... fields) {
+      this.fields = fields;
+    }
+
+    @Override
+    public void getFields(
+        IRowMeta row,
+        String name,
+        IRowMeta[] info,
+        TransformMeta nextTransform,
+        IVariables variables,
+        IHopMetadataProvider metadataProvider) {
+      for (IValueMeta field : fields) {
+        IValueMeta copy = field.clone();
+        copy.setOrigin(name);
+        row.addValueMeta(copy);
+      }
+    }
+  }
 }

Reply via email to