Skip to content

Commit ac076a5

Browse files
committed
sql: factor out construction of run helpers
The updateRun and deleteRun helpers will be used by both Update and Delete, and UpdateSwap and DeleteSwap, respectively. Factor out construction functions for these helpers in the execFactory. Informs: #144503 Release note: None
1 parent 18a6a91 commit ac076a5

File tree

1 file changed

+104
-61
lines changed

1 file changed

+104
-61
lines changed

pkg/sql/opt_exec_factory.go

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,59 +1587,29 @@ func (ef *execFactory) ConstructUpdate(
15871587
return nil, errors.AssertionFailedf("execution requires all update columns have a fetch column")
15881588
}
15891589

1590-
// Derive table and column descriptors.
15911590
rowsNeeded := !returnColOrdSet.Empty()
15921591
tabDesc := table.(*optTable).desc
1593-
fetchCols := makeColList(table, fetchColOrdSet)
1594-
updateCols := makeColList(table, updateColOrdSet)
1595-
1596-
// Create the table updater, which does the bulk of the work.
1597-
ru, err := row.MakeUpdater(
1598-
ef.planner.ExecCfg().Codec,
1599-
tabDesc,
1600-
ordinalsToIndexes(table, uniqueWithTombstoneIndexes),
1601-
ordinalsToIndexes(table, lockedIndexes),
1602-
updateCols,
1603-
fetchCols,
1604-
row.UpdaterDefault,
1605-
ef.planner.SessionData(),
1606-
&ef.planner.ExecCfg().Settings.SV,
1607-
ef.planner.ExecCfg().GetRowMetrics(ef.planner.SessionData().Internal),
1608-
)
1609-
if err != nil {
1610-
return nil, err
1611-
}
16121592

16131593
upd := updateNodePool.Get().(*updateNode)
16141594
*upd = updateNode{
16151595
singleInputPlanNode: singleInputPlanNode{input.(planNode)},
1616-
run: updateRun{
1617-
tu: tableUpdater{ru: ru},
1618-
checkOrds: checks,
1619-
numPassthrough: len(passthrough),
1620-
},
16211596
}
16221597

1623-
upd.run.regionLocalInfo.setupEnforceHomeRegion(ef.planner, table, ru.UpdateCols,
1624-
upd.run.tu.ru.UpdateColIDtoRowIndex)
1625-
16261598
// If rows are not needed, no columns are returned.
1599+
var returnCols []catalog.Column
16271600
if rowsNeeded {
1628-
returnCols := makeColList(table, returnColOrdSet)
1629-
1601+
returnCols = makeColList(table, returnColOrdSet)
16301602
upd.columns = colinfo.ResultColumnsFromColumns(tabDesc.GetID(), returnCols)
16311603
// Add the passthrough columns to the returning columns.
16321604
upd.columns = append(upd.columns, passthrough...)
1605+
}
16331606

1634-
// Set the rowIdxToRetIdx for the mutation. Update returns the non-mutation
1635-
// columns specified, in the same order they are defined in the table.
1636-
//
1637-
// The Updater derives/stores the fetch columns of the mutation and
1638-
// since the return columns are always a subset of the fetch columns,
1639-
// we can use use the fetch columns to generate the mapping for the
1640-
// returned rows.
1641-
upd.run.rowIdxToRetIdx = row.ColMapping(ru.FetchCols, returnCols)
1642-
upd.run.rowsNeeded = true
1607+
// Create the table updater, which does the bulk of the work.
1608+
if err := ef.constructUpdateRun(
1609+
&upd.run, table, fetchColOrdSet, updateColOrdSet, returnColOrdSet, rowsNeeded, returnCols,
1610+
checks, passthrough, uniqueWithTombstoneIndexes, lockedIndexes,
1611+
); err != nil {
1612+
return nil, err
16431613
}
16441614

16451615
if autoCommit {
@@ -1660,6 +1630,62 @@ func (ef *execFactory) ConstructUpdate(
16601630
return &rowCountNode{source: upd}, nil
16611631
}
16621632

1633+
func (ef *execFactory) constructUpdateRun(
1634+
run *updateRun,
1635+
table cat.Table,
1636+
fetchColOrdSet exec.TableColumnOrdinalSet,
1637+
updateColOrdSet exec.TableColumnOrdinalSet,
1638+
returnColOrdSet exec.TableColumnOrdinalSet,
1639+
rowsNeeded bool,
1640+
returnCols []catalog.Column,
1641+
checks exec.CheckOrdinalSet,
1642+
passthrough colinfo.ResultColumns,
1643+
uniqueWithTombstoneIndexes cat.IndexOrdinals,
1644+
lockedIndexes cat.IndexOrdinals,
1645+
) error {
1646+
tabDesc := table.(*optTable).desc
1647+
fetchCols := makeColList(table, fetchColOrdSet)
1648+
updateCols := makeColList(table, updateColOrdSet)
1649+
1650+
// Create the table updater.
1651+
ru, err := row.MakeUpdater(
1652+
ef.planner.ExecCfg().Codec,
1653+
tabDesc,
1654+
ordinalsToIndexes(table, uniqueWithTombstoneIndexes),
1655+
ordinalsToIndexes(table, lockedIndexes),
1656+
updateCols,
1657+
fetchCols,
1658+
row.UpdaterDefault,
1659+
ef.planner.SessionData(),
1660+
&ef.planner.ExecCfg().Settings.SV,
1661+
ef.planner.ExecCfg().GetRowMetrics(ef.planner.SessionData().Internal),
1662+
)
1663+
if err != nil {
1664+
return err
1665+
}
1666+
1667+
run.tu = tableUpdater{ru: ru}
1668+
run.checkOrds = checks
1669+
run.numPassthrough = len(passthrough)
1670+
1671+
run.regionLocalInfo.setupEnforceHomeRegion(ef.planner, table, ru.UpdateCols,
1672+
run.tu.ru.UpdateColIDtoRowIndex)
1673+
1674+
if rowsNeeded {
1675+
// Set the rowIdxToRetIdx for the mutation. Update returns the non-mutation
1676+
// columns specified, in the same order they are defined in the table.
1677+
//
1678+
// The Updater derives/stores the fetch columns of the mutation and
1679+
// since the return columns are always a subset of the fetch columns,
1680+
// we can use use the fetch columns to generate the mapping for the
1681+
// returned rows.
1682+
run.rowIdxToRetIdx = row.ColMapping(ru.FetchCols, returnCols)
1683+
run.rowsNeeded = true
1684+
}
1685+
1686+
return nil
1687+
}
1688+
16631689
func (ef *execFactory) ConstructUpsert(
16641690
input exec.Node,
16651691
table cat.Table,
@@ -1770,46 +1796,31 @@ func (ef *execFactory) ConstructDelete(
17701796
lockedIndexes cat.IndexOrdinals,
17711797
autoCommit bool,
17721798
) (exec.Node, error) {
1773-
// Derive table and column descriptors.
17741799
rowsNeeded := !returnColOrdSet.Empty()
17751800
tabDesc := table.(*optTable).desc
1776-
fetchCols := makeColList(table, fetchColOrdSet)
1777-
1778-
// Create the table deleter, which does the bulk of the work.
1779-
rd := row.MakeDeleter(
1780-
ef.planner.ExecCfg().Codec,
1781-
tabDesc,
1782-
ordinalsToIndexes(table, lockedIndexes),
1783-
fetchCols,
1784-
ef.planner.SessionData(),
1785-
&ef.planner.ExecCfg().Settings.SV,
1786-
ef.planner.ExecCfg().GetRowMetrics(ef.planner.SessionData().Internal),
1787-
)
17881801

17891802
// Now make a delete node. We use a pool.
17901803
del := deleteNodePool.Get().(*deleteNode)
17911804
*del = deleteNode{
17921805
singleInputPlanNode: singleInputPlanNode{input.(planNode)},
1793-
run: deleteRun{
1794-
td: tableDeleter{rd: rd},
1795-
numPassthrough: len(passthrough),
1796-
},
17971806
}
17981807

17991808
// If rows are not needed, no columns are returned.
1809+
var returnCols []catalog.Column
18001810
if rowsNeeded {
1801-
returnCols := makeColList(table, returnColOrdSet)
1811+
returnCols = makeColList(table, returnColOrdSet)
18021812
// Delete returns the non-mutation columns specified, in the same
18031813
// order they are defined in the table.
18041814
del.columns = colinfo.ResultColumnsFromColumns(tabDesc.GetID(), returnCols)
1805-
18061815
// Add the passthrough columns to the returning columns.
18071816
del.columns = append(del.columns, passthrough...)
1808-
1809-
del.run.rowIdxToRetIdx = row.ColMapping(rd.FetchCols, returnCols)
1810-
del.run.rowsNeeded = true
18111817
}
18121818

1819+
// Create the table deleter, which does the bulk of the work.
1820+
ef.constructDeleteRun(
1821+
&del.run, table, fetchColOrdSet, rowsNeeded, returnCols, passthrough, lockedIndexes,
1822+
)
1823+
18131824
if autoCommit {
18141825
del.enableAutoCommit()
18151826
}
@@ -1828,6 +1839,38 @@ func (ef *execFactory) ConstructDelete(
18281839
return &rowCountNode{source: del}, nil
18291840
}
18301841

1842+
func (ef *execFactory) constructDeleteRun(
1843+
run *deleteRun,
1844+
table cat.Table,
1845+
fetchColOrdSet exec.TableColumnOrdinalSet,
1846+
rowsNeeded bool,
1847+
returnCols []catalog.Column,
1848+
passthrough colinfo.ResultColumns,
1849+
lockedIndexes cat.IndexOrdinals,
1850+
) {
1851+
tabDesc := table.(*optTable).desc
1852+
fetchCols := makeColList(table, fetchColOrdSet)
1853+
1854+
// Create the table deleter.
1855+
rd := row.MakeDeleter(
1856+
ef.planner.ExecCfg().Codec,
1857+
tabDesc,
1858+
ordinalsToIndexes(table, lockedIndexes),
1859+
fetchCols,
1860+
ef.planner.SessionData(),
1861+
&ef.planner.ExecCfg().Settings.SV,
1862+
ef.planner.ExecCfg().GetRowMetrics(ef.planner.SessionData().Internal),
1863+
)
1864+
1865+
run.td = tableDeleter{rd: rd}
1866+
run.numPassthrough = len(passthrough)
1867+
1868+
if rowsNeeded {
1869+
run.rowIdxToRetIdx = row.ColMapping(rd.FetchCols, returnCols)
1870+
run.rowsNeeded = true
1871+
}
1872+
}
1873+
18311874
func (ef *execFactory) ConstructDeleteRange(
18321875
table cat.Table,
18331876
needed exec.TableColumnOrdinalSet,

0 commit comments

Comments
 (0)