|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package sql |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/kv/kvpb" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 15 | + "github.com/cockroachdb/errors" |
| 16 | +) |
| 17 | + |
| 18 | +var deleteSwapNodePool = sync.Pool{ |
| 19 | + New: func() interface{} { |
| 20 | + return &deleteSwapNode{} |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +type deleteSwapNode struct { |
| 25 | + // Unlike insertFastPathNode, deleteSwapNode reads from input in order to |
| 26 | + // support projections, which are used by some DELETE statements. |
| 27 | + singleInputPlanNode |
| 28 | + |
| 29 | + // columns is set if this DELETE is returning any rows, to be |
| 30 | + // consumed by a renderNode upstream. This occurs when there is a |
| 31 | + // RETURNING clause with some scalar expressions. |
| 32 | + columns colinfo.ResultColumns |
| 33 | + |
| 34 | + run deleteRun |
| 35 | +} |
| 36 | + |
| 37 | +var _ mutationPlanNode = &deleteSwapNode{} |
| 38 | + |
| 39 | +func (d *deleteSwapNode) startExec(params runParams) error { |
| 40 | + // Cache traceKV during execution, to avoid re-evaluating it for every row. |
| 41 | + d.run.traceKV = params.p.ExtendedEvalContext().Tracing.KVTracingEnabled() |
| 42 | + |
| 43 | + d.run.mustValidateOldPKValues = true |
| 44 | + |
| 45 | + d.run.initRowContainer(params, d.columns) |
| 46 | + |
| 47 | + return d.run.td.init(params.ctx, params.p.txn, params.EvalContext()) |
| 48 | +} |
| 49 | + |
| 50 | +// Next is required because batchedPlanNode inherits from planNode, but |
| 51 | +// batchedPlanNode doesn't really provide it. See the explanatory comments |
| 52 | +// in plan_batch.go. |
| 53 | +func (d *deleteSwapNode) Next(params runParams) (bool, error) { panic("not valid") } |
| 54 | + |
| 55 | +// Values is required because batchedPlanNode inherits from planNode, but |
| 56 | +// batchedPlanNode doesn't really provide it. See the explanatory comments |
| 57 | +// in plan_batch.go. |
| 58 | +func (d *deleteSwapNode) Values() tree.Datums { panic("not valid") } |
| 59 | + |
| 60 | +// BatchedNext implements the batchedPlanNode interface. |
| 61 | +func (d *deleteSwapNode) BatchedNext(params runParams) (bool, error) { |
| 62 | + if d.run.done { |
| 63 | + return false, nil |
| 64 | + } |
| 65 | + |
| 66 | + // Delete swap does everything in one batch. There should only be a single row |
| 67 | + // of input, to ensure the savepoint rollback below has the correct SQL |
| 68 | + // semantics. |
| 69 | + |
| 70 | + if err := params.p.cancelChecker.Check(); err != nil { |
| 71 | + return false, err |
| 72 | + } |
| 73 | + |
| 74 | + next, err := d.input.Next(params) |
| 75 | + if next { |
| 76 | + if err := d.run.processSourceRow(params, d.input.Values()); err != nil { |
| 77 | + return false, err |
| 78 | + } |
| 79 | + // Verify that there was only a single row of input. |
| 80 | + next, err = d.input.Next(params) |
| 81 | + if next { |
| 82 | + return false, errors.AssertionFailedf("expected only 1 row as input to delete swap") |
| 83 | + } |
| 84 | + } |
| 85 | + if err != nil { |
| 86 | + return false, err |
| 87 | + } |
| 88 | + |
| 89 | + // Delete swap works by optimistically modifying every index in the same |
| 90 | + // batch. If the row does not actually exist, the write to the primary index |
| 91 | + // will fail with ConditionFailedError, but writes to some secondary indexes |
| 92 | + // might succeed. We use a savepoint here to undo those writes. |
| 93 | + sp, err := d.run.td.createSavepoint(params.ctx) |
| 94 | + if err != nil { |
| 95 | + return false, err |
| 96 | + } |
| 97 | + |
| 98 | + d.run.td.setRowsWrittenLimit(params.extendedEvalCtx.SessionData()) |
| 99 | + if err := d.run.td.finalize(params.ctx); err != nil { |
| 100 | + // If this was a ConditionFailedError, it means the row did not exist in the |
| 101 | + // primary index. We must roll back to the savepoint above to undo writes to |
| 102 | + // all secondary indexes. |
| 103 | + if condErr := (*kvpb.ConditionFailedError)(nil); errors.As(err, &condErr) { |
| 104 | + // Reset the table writer so that it looks like there were no rows to |
| 105 | + // delete. |
| 106 | + d.run.td.rowsWritten = 0 |
| 107 | + d.run.td.clearLastBatch(params.ctx) |
| 108 | + if err := d.run.td.rollbackToSavepoint(params.ctx, sp); err != nil { |
| 109 | + return false, err |
| 110 | + } |
| 111 | + return false, nil |
| 112 | + } |
| 113 | + return false, err |
| 114 | + } |
| 115 | + |
| 116 | + // Remember we're done for the next call to BatchedNext(). |
| 117 | + d.run.done = true |
| 118 | + |
| 119 | + // Possibly initiate a run of CREATE STATISTICS. |
| 120 | + params.ExecCfg().StatsRefresher.NotifyMutation(d.run.td.tableDesc(), d.run.td.lastBatchSize) |
| 121 | + |
| 122 | + return d.run.td.lastBatchSize > 0, nil |
| 123 | +} |
| 124 | + |
| 125 | +// BatchedCount implements the batchedPlanNode interface. |
| 126 | +func (d *deleteSwapNode) BatchedCount() int { |
| 127 | + return d.run.td.lastBatchSize |
| 128 | +} |
| 129 | + |
| 130 | +// BatchedValues implements the batchedPlanNode interface. |
| 131 | +func (d *deleteSwapNode) BatchedValues(rowIdx int) tree.Datums { |
| 132 | + return d.run.td.rows.At(rowIdx) |
| 133 | +} |
| 134 | + |
| 135 | +func (d *deleteSwapNode) Close(ctx context.Context) { |
| 136 | + d.run.td.close(ctx) |
| 137 | + *d = deleteSwapNode{} |
| 138 | + deleteSwapNodePool.Put(d) |
| 139 | +} |
| 140 | + |
| 141 | +func (d *deleteSwapNode) rowsWritten() int64 { |
| 142 | + return d.run.td.rowsWritten |
| 143 | +} |
0 commit comments