////
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.
////
:documentationPath: /pipeline/transforms/
:language: en_US
:description: The Merge Rows (Diff) transform compares and merges data within two rows of data, adding a flag for each row.

= image:transforms/icons/mergerows.svg[Merge rows (diff) transform Icon, role="image-doc-icon"] Merge rows (diff)

[%noheader,cols="3a,1a", role="table-no-borders" ]
|===
|
== Description

The Merge Rows (Diff) transform compares and merges rows between two data streams called **Reference** (previous data) and **Compare** (new data), adding a flag to each row.

|
== Supported Engines
[%noheader,cols="2,1a",frame=none, role="table-supported-engines"]
!===
!Hop Engine! image:check_mark.svg[Supported, 24]
!Spark! image:question_mark.svg[Maybe Supported, 24]
!Flink! image:question_mark.svg[Maybe Supported, 24]
!Dataflow! image:question_mark.svg[Maybe Supported, 24]
!===
|===

== Usage


This transform is useful for comparing data collected at two different times.

For example, the source system of your data warehouse might not contain a timestamp of the last data update.

You could use this transform to compare the two data streams and merge the dates and timestamps in the rows.

Based on key fields and comparison fields, this transform merges reference rows with compare rows  and creates merged output rows.

A flag in each row indicates how the values were compared and merged.
The possible flag values are:

* **identical**: The key was found in both rows, and the compared values are identical.

* **changed**: The key was found in both rows, but one or more compared values are different.

* **new**: The key was not found in the reference rows.

* **deleted**: The key was not found in the compare rows.

If the row's flag is **identical** or **deleted**, the merged output row is  based on the reference row.

If the flag is **new** or **changed**, the merged output row is based on the compare row.

You can also send the merged and flagged rows to a subsequent transform in your pipeline, such as xref:pipeline/transforms/switchcase.adoc[Switch-Case] or  xref:pipeline/transforms/synchronizeaftermerge.adoc[Synchronize after merge].
In the subsequent transform, you can use the flag field generated by **Merge rows (diff)** to control updates/inserts/deletes on a target table.

== Options

[options="header"]
|===
|Option|Description
|Transform name|Name of the transform.
|Reference rows origin|Specify the transform that produces the reference rows. It's a Stream with original rows (rows that you want to compare the new rows to).
|Compare rows origin|Specify the transform that produces the compare rows. It's a Stream with new rows
|Flag fieldname|Specify the name of the flag field on the output stream.
|Keys to match|Specify fields containing the keys on which to match. Click "Get key fields" to insert all of the fields from the reference rows
|Values to compare|Specify fields contaning the values to compare. Click "Get value fields" to insert all of the fields from the compare rows.
Key fields do not need to be repeated here.
|===
