Skip to content

Commit 33fca7a

Browse files
committed
opt: add swap flag to MutationPrivate
Inside the optimizer we can simply use a boolean flag to distinguish between update and delete, and update swap and delete swap, respectively. Add this flag to MutationPrivate and use it in execbuild to construct the swap nodes. Informs: #144503 Release note: None
1 parent c956f73 commit 33fca7a

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

pkg/sql/opt/exec/execbuilder/mutation.go

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -463,19 +463,37 @@ func (b *Builder) buildUpdate(upd *memo.UpdateExpr) (_ execPlan, outputCols colO
463463
}
464464
}
465465

466-
node, err := b.factory.ConstructUpdate(
467-
input.root,
468-
tab,
469-
fetchColOrds,
470-
updateColOrds,
471-
returnColOrds,
472-
checkOrds,
473-
passthroughCols,
474-
upd.UniqueWithTombstoneIndexes,
475-
lockedIndexes,
476-
b.allowAutoCommit && len(upd.UniqueChecks) == 0 &&
477-
len(upd.FKChecks) == 0 && len(upd.FKCascades) == 0 && upd.AfterTriggers == nil,
478-
)
466+
allowAutoCommit := b.allowAutoCommit && len(upd.UniqueChecks) == 0 &&
467+
len(upd.FKChecks) == 0 && len(upd.FKCascades) == 0 && upd.AfterTriggers == nil
468+
var node exec.Node
469+
if upd.Swap {
470+
if !checkOrds.Empty() || len(upd.UniqueWithTombstoneIndexes) != 0 {
471+
return execPlan{}, colOrdMap{}, errors.AssertionFailedf("update swap does not support checks")
472+
}
473+
node, err = b.factory.ConstructUpdateSwap(
474+
input.root,
475+
tab,
476+
fetchColOrds,
477+
updateColOrds,
478+
returnColOrds,
479+
passthroughCols,
480+
lockedIndexes,
481+
allowAutoCommit,
482+
)
483+
} else {
484+
node, err = b.factory.ConstructUpdate(
485+
input.root,
486+
tab,
487+
fetchColOrds,
488+
updateColOrds,
489+
returnColOrds,
490+
checkOrds,
491+
passthroughCols,
492+
upd.UniqueWithTombstoneIndexes,
493+
lockedIndexes,
494+
allowAutoCommit,
495+
)
496+
}
479497
if err != nil {
480498
return execPlan{}, colOrdMap{}, err
481499
}
@@ -624,16 +642,30 @@ func (b *Builder) buildDelete(del *memo.DeleteExpr) (_ execPlan, outputCols colO
624642
}
625643
}
626644

627-
node, err := b.factory.ConstructDelete(
628-
input.root,
629-
tab,
630-
fetchColOrds,
631-
returnColOrds,
632-
passthroughCols,
633-
lockedIndexes,
634-
b.allowAutoCommit && len(del.FKChecks) == 0 &&
635-
len(del.FKCascades) == 0 && del.AfterTriggers == nil,
636-
)
645+
allowAutoCommit := b.allowAutoCommit && len(del.FKChecks) == 0 &&
646+
len(del.FKCascades) == 0 && del.AfterTriggers == nil
647+
var node exec.Node
648+
if del.Swap {
649+
node, err = b.factory.ConstructDeleteSwap(
650+
input.root,
651+
tab,
652+
fetchColOrds,
653+
returnColOrds,
654+
passthroughCols,
655+
lockedIndexes,
656+
allowAutoCommit,
657+
)
658+
} else {
659+
node, err = b.factory.ConstructDelete(
660+
input.root,
661+
tab,
662+
fetchColOrds,
663+
returnColOrds,
664+
passthroughCols,
665+
lockedIndexes,
666+
allowAutoCommit,
667+
)
668+
}
637669
if err != nil {
638670
return execPlan{}, colOrdMap{}, err
639671
}

pkg/sql/opt/memo/expr_format.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,9 @@ func FormatPrivate(f *ExprFmtCtx, private interface{}, physProps *physical.Requi
19181918
fmt.Fprintf(f.Buffer, " %s", seq.Name())
19191919

19201920
case *MutationPrivate:
1921+
if t.Swap {
1922+
fmt.Fprint(f.Buffer, " (swap)")
1923+
}
19211924
f.formatIndex(t.Table, cat.PrimaryIndex, false /* reverse */)
19221925

19231926
case *LockPrivate:

pkg/sql/opt/memo/logical_props_builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,10 @@ func (b *logicalPropsBuilder) buildMutationProps(mutation RelExpr, rel *props.Re
16101610
// -----------
16111611
// Inherit cardinality from input.
16121612
rel.Cardinality = inputProps.Cardinality
1613+
if private.Swap {
1614+
// Swap mutations can return zero rows.
1615+
rel.Cardinality = rel.Cardinality.AsLowAs(0)
1616+
}
16131617

16141618
// Statistics
16151619
// ----------

pkg/sql/opt/ops/mutation.opt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ define MutationPrivate {
211211
# VectorInsert indicates that the mutation is an insert with a specialized
212212
# vectorized implementation used for Copy statements.
213213
VectorInsert bool
214+
215+
# Swap indicates that the mutation is an update swap or delete swap.
216+
Swap bool
214217
}
215218

216219
# Update evaluates a relational input expression that fetches existing rows from

0 commit comments

Comments
 (0)