WL#9807: X Protocol Crud.Insert with upsert

Affects: Server-8.0   —   Status: Complete

The UPSERT command is a very useful operation that allows performing an insert or 
update, depending on whether the record already exists or not, in a single step, 
atomically.

MySQL supports the ON DUPLICATE KEY UPDATE extension to INSERT, which has this 
behaviour, and should be used for implementation.

Limitations
-----------

The intended behaviour is:

* if records exists, replace it with our the values presented, other side create

Building a ON DUPLICATE KEY UPDATE for collections is straight-forward:

  INSERT INTO collection VALUES (key, value) ON DUPLICATE KEY
    UPDATE SET _doc=value;
    
as the structure of the underlying table is known (value goes into _doc) and
unique keys are based on generated columns and aren't updated directly.

For UPSERTS into tables the behaviour is different:

  INSERT INTO collection VALUES ("a", "b", "c") ON DUPLICATE KEY
    UPDATE SET ??="f", ??="b", ??="c";

The column names and keys are not known, unless they come with the query
or they're queried from MySQL. The 2nd introduces the need for
metadata locking, caching and additional queries, which would be a
performance issue.

Therefore this worklog limits the feature to collections only.