RETURNING clause is accepted by the three DML (Data Manipulation Language) statements — INSERT, UPDATE and DELETE/EVICT/TOMBSTONE — and by no other statement.
A DML statement normally reports only the IDs of the documents it changed. Adding RETURNING turns it into a statement that returns a result set built from the affected documents, in the same form as a SELECT statement, so you can read the affected data back without issuing a second query.
Documents Projected
Each statement projects the affected documents at a different point in its processing:A statement populates either its result set (when
RETURNING is present) or its list of mutated document IDs (when it is not), never both.Projection Syntax
RETURNING takes the same projection syntax as a SELECT statement, with these differences:
DISTINCTmay not be specified. The word is taken to be a field name rather than a keyword.RAWprojections are not supported.- The unqualified
*wildcard is only accepted as the sole element of the projection list; combining it with further elements is an error. A qualifiedalias.*has no such restriction. - Aggregates are permitted and are evaluated over the whole set of affected documents; there is no
GROUP BYclause. When any element of the projection is an aggregate, the remaining elements must not depend on the documents, otherwise an error is raised.
SELECT projection.
Examples
Return the complete documents that were inserted:DQL
DQL
DQL
DQL
SELECT projection:
DQL
RETURNING and Legacy DML
RETURNING is implemented by the query engine’s DML operators only. The legacy (non-operator-model) implementation does not support it, so a statement that combines the two is rejected with the error RETURNING clause with legacy DML is not supported.
Legacy handling is selected in either of two ways:
- the
#disable_dmldirective on an individual statement, which disablesRETURNINGfor that statement only; or - the
DQL_USE_LEGACY_DMLsystem parameter, which does the same for every statement.
USE IDS on UPDATE and DELETE/EVICT/TOMBSTONE, and INSERT sourced from a SELECT.
DQL_USE_LEGACY_DML defaults to false in the Edge SDK (small peer) and true on Ditto Server (big peer). RETURNING is therefore available by default in the SDK, and on Ditto Server only where that parameter has been set to false.