UPDATE Statement (Impala 2.8 or higher only)

Updates an arbitrary number of rows in a Kudu table. This statement only works for Impala tables that use the Kudu storage engine.

Syntax:

  1. UPDATE [database_name.]table_name SET col = val [, col = val ... ]
  2. [ FROM joined_table_refs ]
  3. [ WHERE where_conditions ]

Usage notes:

None of the columns that make up the primary key can be updated by the SET clause.

The conditions in the WHERE clause are the same ones allowed for the SELECT statement. See SELECT Statement for details.

If the WHERE clause is omitted, all rows in the table are updated.

The conditions in the WHERE clause can refer to any combination of primary key columns or other columns. Referring to primary key columns in the WHERE clause is more efficient than referring to non-primary key columns.

Because Kudu currently does not enforce strong consistency during concurrent DML operations, be aware that the results after this statement finishes might be different than you intuitively expect:

  • If some rows cannot be updated because their some primary key columns are not found, due to their being deleted by a concurrent DELETE operation, the statement succeeds but returns a warning.

  • An UPDATE statement might also overlap with INSERT, UPDATE, or UPSERT statements running concurrently on the same table. After the statement finishes, there might be more or fewer matching rows than expected in the table because it is undefined whether the UPDATE applies to rows that are inserted or updated while the UPDATE is in progress.

The number of affected rows is reported in an impala-shell message and in the query profile.

The optional FROM clause lets you restrict the updates to only the rows in the specified table that are part of the result set for a join query. The join clauses can include non-Kudu tables, but the table from which the rows are deleted must be a Kudu table.

Statement type: DML

Important: After adding or replacing data in a table used in performance-critical queries, issue a COMPUTE STATS statement to make sure all statistics are up-to-date. Consider updating statistics for a table after any INSERT, LOAD DATA, or CREATE TABLE AS SELECT statement in Impala, or after loading data through Hive and doing a REFRESH table_name in Impala. This technique is especially important for tables that are very large, used in join queries, or both.

Examples:

The following examples show how to perform a simple update on a table, with or without a WHERE clause:

  1. -- Set all rows to the same value for column c3.
  2. -- In this case, c1 and c2 are primary key columns
  3. -- and so cannot be updated.
  4. UPDATE kudu_table SET c3 = 'not applicable';
  5. -- Update only the rows that match the condition.
  6. UPDATE kudu_table SET c3 = NULL WHERE c1 > 100 AND c3 IS NULL;
  7. -- Does not update any rows, because the WHERE condition is always false.
  8. UPDATE kudu_table SET c3 = 'impossible' WHERE 1 = 0;
  9. -- Change the values of multiple columns in a single UPDATE statement.
  10. UPDATE kudu_table SET c3 = upper(c3), c4 = FALSE, c5 = 0 WHERE c6 = TRUE;

The following examples show how to perform an update using the FROM keyword with a join clause:

  1. -- Uppercase a column value, only for rows that have
  2. -- an ID that matches the value from another table.
  3. UPDATE kudu_table SET c3 = upper(c3)
  4. FROM kudu_table JOIN non_kudu_table
  5. ON kudu_table.id = non_kudu_table.id;
  6. -- Same effect as previous statement.
  7. -- Assign table aliases in FROM clause, then refer to
  8. -- short names elsewhere in the statement.
  9. UPDATE t1 SET c3 = upper(c3)
  10. FROM kudu_table t1 JOIN non_kudu_table t2
  11. ON t1.id = t2.id;
  12. -- Same effect as previous statements, but more efficient.
  13. -- Use WHERE clause to skip updating values that are
  14. -- already uppercase.
  15. UPDATE t1 SET c3 = upper(c3)
  16. FROM kudu_table t1 JOIN non_kudu_table t2
  17. ON t1.id = t2.id
  18. WHERE c3 != upper(c3);

Related information:

Using Impala to Query Kudu Tables, INSERT Statement, DELETE Statement (Impala 2.8 or higher only), UPSERT Statement (Impala 2.8 or higher only)

Parent topic: Impala SQL Statements