pub struct Merge<X: Extension = NoExt> {
pub with: Option<With<X>>,
pub target: DmlTarget,
pub using: TableWithJoins<X>,
pub on: Expr<X>,
pub clauses: ThinVec<MergeWhenClause<X>>,
pub returning: Option<Returning<X>>,
pub meta: Meta,
}Expand description
MERGE INTO <target> [AS alias] USING <source> ON <condition> <when_clause>+
statement (SQL:2003 feature F312; the standard upsert).
One canonical shape: the standard models MERGE directly, so this is a
shared node gated for acceptance by MutationSyntax::merge
— on in ANSI (SQL:2016) and PostgreSQL 15+, off in MySQL — never a dialect fork.
The clause reuses the existing mutation shapes rather than parallel ones: the
MERGE INTO <target> relation is the shared DmlTarget (carrying the same
ONLY/* inheritance marker UPDATE/DELETE targets do), the USING source is
a TableWithJoins (SQL:2016’s <table reference> — a table, a derived subquery,
or a joined table, exactly as in a FROM clause), and the WHEN actions reuse the
INSERT value element (InsertValue) and the UPDATE ... SET assignment list
(UpdateAssignment).
Boxed at the Statement seam (Statement::Merge) because MERGE
is a fat, infrequent variant — its when_clause list and the embedded source and
condition make it one of the widest statements — so boxing keeps the common
Statement lean.
Fields§
§with: Option<With<X>>Optional statement-level WITH clause (WITH ... MERGE INTO ...; PostgreSQL
15+ and DuckDB, probed on 1.5.4). Not standard surface — SQL:2016’s
<merge statement> takes no WITH — so it is gated by
MutationSyntax::cte_before_merge, the
MERGE counterpart of Insert::with’s cte_before_insert gate.
target: DmlTargetMERGE INTO <target> — the table rows are merged into, plus its optional
correlation alias and PostgreSQL ONLY/* inheritance marker. Reuses the shared
DmlTarget shape (UPDATE/DELETE use the same one), so MERGE INTO ONLY t
and MERGE INTO t * ride the same table_expressions.only gate as those
statements — never a parallel target type.
using: TableWithJoins<X>USING <source> — the data source the target is merged against: a table, a
derived subquery, or a joined table, reusing the TableWithJoins shape (a
relation plus its chained joins, exactly as a FROM item; SQL:2016’s
USING <table reference>) rather than a parallel source type. A bare
comma-separated list is not admitted — the merge source is one <table reference>, so USING a, b rejects (engine-verified on pg_query 17).
on: Expr<X>The ON <join predicate> matching condition.
clauses: ThinVec<MergeWhenClause<X>>WHEN [NOT] MATCHED ... clauses, in source order. The parser guarantees at
least one; the standard requires a non-empty list.
returning: Option<Returning<X>>RETURNING ... output clause (PostgreSQL 17+ and DuckDB, probed on 1.5.4;
where merge_action() is also admitted as an output expression). Rides the
same MutationSyntax::returning gate as the
other DML statements — every shipped dialect with merge on either accepts
both or rejects both.
meta: MetaSource location and node identity.
Trait Implementations§
Source§impl<'de, X> Deserialize<'de> for Merge<X>where
X: Deserialize<'de> + Extension,
impl<'de, X> Deserialize<'de> for Merge<X>where
X: Deserialize<'de> + Extension,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl<X: Eq + Extension> Eq for Merge<X>
Source§impl<X: Extension + Render> Render for Merge<X>
impl<X: Extension + Render> Render for Merge<X>
Source§fn render(&self, ctx: &RenderCtx<'_>, f: &mut Formatter<'_>) -> Result
fn render(&self, ctx: &RenderCtx<'_>, f: &mut Formatter<'_>) -> Result
Source§fn operand_binding_power(&self) -> Option<BindingPower>
fn operand_binding_power(&self) -> Option<BindingPower>
None (the default) for a self-delimiting node — an atom, call, or
constructor — that never needs parentheses. Read moreimpl<X: PartialEq + Extension> StructuralPartialEq for Merge<X>
Auto Trait Implementations§
impl<X> Freeze for Merge<X>where
X: Freeze,
impl<X> RefUnwindSafe for Merge<X>where
X: RefUnwindSafe,
impl<X> Send for Merge<X>where
X: Send,
impl<X> Sync for Merge<X>where
X: Sync,
impl<X> Unpin for Merge<X>where
X: Unpin,
impl<X> UnsafeUnpin for Merge<X>where
X: UnsafeUnpin,
impl<X> UnwindSafe for Merge<X>where
X: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> DynAstExt for T
impl<T> DynAstExt for T
Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&dyn Any for downcasting a node back to its concrete type.Source§fn dyn_clone(&self) -> Box<dyn DynAstExt>
fn dyn_clone(&self) -> Box<dyn DynAstExt>
Clone (whose
Self-returning signature cannot go through a vtable).Source§fn dyn_eq(&self, other: &dyn DynAstExt) -> bool
fn dyn_eq(&self, other: &dyn DynAstExt) -> bool
PartialEq (whose &Self argument cannot go through a vtable). Equal
iff other holds the same concrete type and that type deems the values
equal; differently-typed nodes are never equal.