Skip to main content

Merge

Struct Merge 

Source
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: DmlTarget

MERGE 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: Meta

Source location and node identity.

Trait Implementations§

Source§

impl<X: Clone + Extension> Clone for Merge<X>

Source§

fn clone(&self) -> Merge<X>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<X: Debug + Extension> Debug for Merge<X>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<X: Eq + Extension> Eq for Merge<X>

Source§

impl<X: Hash + Extension> Hash for Merge<X>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<X: PartialEq + Extension> PartialEq for Merge<X>

Source§

fn eq(&self, other: &Merge<X>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<X: Extension + Render> Render for Merge<X>

Source§

fn render(&self, ctx: &RenderCtx<'_>, f: &mut Formatter<'_>) -> Result

Return the render for this value.
Source§

fn operand_binding_power(&self) -> Option<BindingPower>

The binding power this node contributes when it appears as an operand, or None (the default) for a self-delimiting node — an atom, call, or constructor — that never needs parentheses. Read more
Source§

impl<X> Serialize for Merge<X>
where X: Serialize + Extension,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<X: Extension> Spanned for Merge<X>

Source§

fn span(&self) -> Span

Return the span for this value.
Source§

impl<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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynAstExt for T
where T: Extension + Render + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Erase to &dyn Any for downcasting a node back to its concrete type.
Source§

fn dyn_clone(&self) -> Box<dyn DynAstExt>

Clone into a fresh box — the object-safe stand-in for Clone (whose Self-returning signature cannot go through a vtable).
Source§

fn dyn_eq(&self, other: &dyn DynAstExt) -> bool

Structural equality against another erased node — the object-safe stand-in for 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.
Source§

fn dyn_hash(&self, state: &mut dyn Hasher)

Feed this node’s hash into an erased hasher — the object-safe stand-in for Hash::hash (whose generic H: Hasher cannot go through a vtable).
Source§

impl<T> Extension for T
where T: Clone + Debug + Eq + Hash + Spanned,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> RenderExt for T
where T: Render,

Source§

fn displayed<'a>(&'a self, ctx: &'a RenderCtx<'a>) -> Displayed<'a, T>

Pair this node with an explicit canonical RenderCtx so format!, to_string, and {} render it. This is the canonical path: the ctx’s resolver and source must match the node’s parse.
Source§

fn debug_sql<'a>(&'a self, resolver: &'a dyn Resolver) -> DebugSql<'a, Self>
where Self: Sized,

Render this node for debugging against an explicitly-supplied resolver (the debug-SQL mitigation), returning a Display adapter. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.