Update
Description
The Update transform looks up a row in a table using one or more lookup keys and updates it if needed.
If the row can’t be found, nothing is done.
If the row is found the row in the table is updated.
Options
| Option | Description |
|---|---|
Commit size |
The number of rows to update before running a commit. |
Use batch updates? |
Allows the use of batch updates. |
Rows per statement |
With batch updates, the number of rows one UPDATE statement carries, on a database type with an UPDATE of several rows whose failed batches are recovered (see below). 100 for a new transform; a transform saved before this option existed updates one row per statement. Variables are allowed. See Several rows per statement. |
Skip lookup |
Skips the row lookup. |
Ignore lookup failure? |
Allows the transform to skip lookup failures. |
Flag field (key found) |
Field which contains whether the key was found or not. |
Connection |
The database connection to which data is written. |
Key Lookup table |
Allows you to specify a list of field values and comparators. You can use the following comparators: =, = ~NULL, <>, <, ⇐, >, >=, LIKE, BETWEEN, IS NULL, IS NOT NULL |
SQL button |
Click SQL to generate the SQL to create the table and indexes for correct operation. |
Transform name |
Name of the transform; this name has to be unique in a single pipeline. |
Target schema |
The name of the Schema for the table to which data is written. This is important for data sources that allow for table names with periods in them. |
Target table |
Name of the table in which you want to do the update. |
Update Fields |
Allows you to specify all fields in the table you want to update. |
Batches and error handling
With Use batch updates? on, the updates are sent to the database in batches of the commit size. When the update of a row in a batch fails, what happened to the other rows of the batch depends on the database’s JDBC driver: some run the other rows, some stop at the failing row, and some lose the whole batch.
On a database whose driver’s behaviour has been measured, the transform uses it to work out which rows of a failed batch are in the database, and with an error hop attached it sends each row to exactly one stream: the main output for rows that were updated, the error hop for rows that were refused. A row only appears on the main output after the database has committed its update, and the rows keep their input order, also the rows that needed no update and the rows whose key was not found. On such a database the transform also keeps batching when an error hop is attached. That includes PostgreSQL, where an error hop otherwise switches batching off and every row runs on its own behind a savepoint. Without an error hop a failed batch is rolled back and the transform stops, as before. When the transform is stopped or fails, the rows that still wait in its batch are not updated.
The databases measured so far, with their default connection options, are listed in the development manual under "Creating a dialect". On every other database, and when a connection option changes how the driver handles a failed batch, the transform handles a failed batch as it always did.
To go back to that behaviour on a measured database:
-
set When a batch fails, the driver on the Advanced tab of the connection to As before, or
-
set the variable
HOP_DATABASE_BATCH_RECOVERYtoLEGACY, for every connection.
The same option can name what the driver does, for a connection whose options change it.
When a failed batch is worked out this way, the update of a row can be sent to the database more than once, so a failed batch can fire non-transactional triggers again.
With Use batch updates? off, or a commit size of 0, the rows are updated one by one, as before.
Several rows per statement
With batch updates and Rows per statement above 1, the updates of a batch are written as statements of several rows each, which saves a statement execution and, depending on the driver, a round trip per row. The form depends on the database, for example on PostgreSQL and DuckDB:
UPDATE target AS t SET name = s.name, amount = s.amount
FROM (VALUES (?, ?, ?), (?, ?, ?)) s (id, name, amount)
WHERE t.id = s.id
SQL Server and MySQL join the target table with such a list of rows, and Oracle runs a MERGE with only WHEN MATCHED.
This is done on the database types whose UPDATE of several rows and failed batches have been checked: PostgreSQL, MySQL, Microsoft SQL Server (native), Oracle and DuckDB.
H2 and Apache Derby update one row per statement.
Several rows go in one statement, with a lookup per row and with Skip lookup, only when:
-
every key condition is
=, each on a column of its own (not= ~NULL,<>,BETWEEN,IS NULL, …), and -
no update field writes a key column, or a column another update field writes too.
The lookup, the error for a key that is not found and the flag field work as before: only the rows that need an UPDATE go into the statements. A statement never carries more rows than the commit size, nor more than the database allows; a remainder shorter than the rows per statement is sent as a few statements of smaller sizes. The rows keep their input order: when the same key comes again, the statement is closed and the row goes into the next one, so the last value of a key wins, as with one row per statement.
When a statement of several rows fails, its rows are run again one by one, so with an error hop exactly the refused rows go to the error hop (error code UPD002) and the others are committed; without one the transform stops and the failed batch is rolled back.
Statement-level triggers fire once per statement and see all of its rows: every trigger on SQL Server, FOR EACH STATEMENT triggers on PostgreSQL and triggers without FOR EACH ROW on Oracle.
A trigger that expects one row per UPDATE needs Rows per statement 1.
Row-level triggers still fire once per row.
One row per statement is used, as before, without batch updates (also with a commit size of 0 or a connection shared by a transactional pipeline), for the key conditions and update fields above, on a database without transactions, on MySQL into a table without transactions (MyISAM, MEMORY, …), or when the database type declares no UPDATE of several rows or its limit on parameters leaves room for one row only. The detailed log says which, and how many rows per statement are used.
To go back to one row per statement without changing any transform, tick Write one row per statement on the Advanced tab of the connection, or set the variable HOP_DATABASE_MULTI_ROW_DML to DISABLED for every connection.
HOP_DATABASE_BATCH_RECOVERY=LEGACY does the same, because several rows per statement are only written where failed batches are recovered.
Samples
The samples project has transforms/update.hpl, which updates the table public.employees of the hop-samples connection with 2 rows from a Data grid: the name of id 2 changes from robert to bob.