|
| 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 logical |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog" |
| 12 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" |
| 14 | +) |
| 15 | + |
| 16 | +// getPhysicalColumns returns the list of columns that are part of the table's |
| 17 | +// primary key and value. |
| 18 | +func getPhysicalColumns(table catalog.TableDescriptor) []catalog.Column { |
| 19 | + columns := table.AllColumns() |
| 20 | + result := make([]catalog.Column, 0, len(columns)) |
| 21 | + for _, col := range columns { |
| 22 | + if !col.IsComputed() && !col.IsVirtual() && !col.IsSystemColumn() { |
| 23 | + result = append(result, col) |
| 24 | + } |
| 25 | + } |
| 26 | + return result |
| 27 | +} |
| 28 | + |
| 29 | +// newInsertStatement returns a statement that can be used to insert a row into |
| 30 | +// the table. |
| 31 | +// |
| 32 | +// The statement will have `n` parameters, where `n` is the number of columns |
| 33 | +// in the table. Parameters are ordered by column ID. |
| 34 | +func newInsertStatement(table catalog.TableDescriptor) (tree.Statement, error) { |
| 35 | + columns := getPhysicalColumns(table) |
| 36 | + |
| 37 | + columnNames := make(tree.NameList, len(columns)) |
| 38 | + for i, col := range columns { |
| 39 | + columnNames[i] = tree.Name(col.GetName()) |
| 40 | + } |
| 41 | + |
| 42 | + parameters := make(tree.Exprs, len(columnNames)) |
| 43 | + for i := range columnNames { |
| 44 | + var err error |
| 45 | + parameters[i], err = tree.NewPlaceholder(fmt.Sprintf("%d", i+1)) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + parameterValues := &tree.ValuesClause{ |
| 52 | + Rows: []tree.Exprs{ |
| 53 | + parameters, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + rows := &tree.Select{ |
| 58 | + Select: parameterValues, |
| 59 | + } |
| 60 | + |
| 61 | + insert := &tree.Insert{ |
| 62 | + Table: &tree.TableRef{ |
| 63 | + TableID: int64(table.GetID()), |
| 64 | + As: tree.AliasClause{Alias: "replication_target"}, |
| 65 | + }, |
| 66 | + Rows: rows, |
| 67 | + Columns: columnNames, |
| 68 | + Returning: tree.AbsentReturningClause, |
| 69 | + } |
| 70 | + |
| 71 | + return insert, nil |
| 72 | +} |
| 73 | + |
| 74 | +// newUpdateStatement returns a statement that can be used to update a row in |
| 75 | +// the table. If a table has `n` columns, the statement will have `2n` |
| 76 | +// parameters, where the first `n` parameters are the previous values of the row |
| 77 | +// and the last `n` parameters are the new values of the row. |
| 78 | +// |
| 79 | +// Parameters are ordered by column ID. |
| 80 | +func newUpdateStatement(table catalog.TableDescriptor) (tree.Statement, error) { |
| 81 | + columns := getPhysicalColumns(table) |
| 82 | + |
| 83 | + // Create WHERE clause placeholders for every column in the table |
| 84 | + var whereClause tree.Expr |
| 85 | + for i, col := range columns { |
| 86 | + placeholder, err := tree.NewPlaceholder(fmt.Sprintf("%d", i+1)) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + colExpr := &tree.ComparisonExpr{ |
| 91 | + Operator: treecmp.MakeComparisonOperator(treecmp.EQ), |
| 92 | + Left: &tree.ColumnItem{ColumnName: tree.Name(col.GetName())}, |
| 93 | + Right: placeholder, |
| 94 | + } |
| 95 | + |
| 96 | + if whereClause == nil { |
| 97 | + whereClause = colExpr |
| 98 | + } else { |
| 99 | + whereClause = &tree.AndExpr{ |
| 100 | + Left: whereClause, |
| 101 | + Right: colExpr, |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + exprs := make(tree.UpdateExprs, len(columns)) |
| 107 | + for i, col := range columns { |
| 108 | + nameNode := tree.Name(col.GetName()) |
| 109 | + names := tree.NameList{nameNode} |
| 110 | + |
| 111 | + // Create a placeholder for the new value (num_columns+i+1) since we |
| 112 | + // placholders to the where and placholders are 1 indexed. |
| 113 | + placeholder, err := tree.NewPlaceholder(fmt.Sprintf("%d", len(columns)+i+1)) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + exprs[i] = &tree.UpdateExpr{ |
| 119 | + Names: names, |
| 120 | + Expr: &tree.CastExpr{ |
| 121 | + Expr: placeholder, |
| 122 | + Type: col.GetType(), |
| 123 | + SyntaxMode: tree.CastPrepend, |
| 124 | + }, |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Create the final update statement |
| 129 | + update := &tree.Update{ |
| 130 | + Table: &tree.TableRef{ |
| 131 | + TableID: int64(table.GetID()), |
| 132 | + As: tree.AliasClause{Alias: "replication_target"}, |
| 133 | + }, |
| 134 | + Exprs: exprs, |
| 135 | + Where: &tree.Where{Type: tree.AstWhere, Expr: whereClause}, |
| 136 | + Returning: tree.AbsentReturningClause, |
| 137 | + } |
| 138 | + |
| 139 | + return update, nil |
| 140 | +} |
| 141 | + |
| 142 | +// newDeleteStatement returns a statement that can be used to delete a row from |
| 143 | +// the table. The statement will have `n` parameters, where `n` is the number of |
| 144 | +// columns in the table. Parameters are used in the WHERE clause to precisely |
| 145 | +// identify the row to delete. |
| 146 | +// |
| 147 | +// Parameters are ordered by column ID. |
| 148 | +func newDeleteStatement(table catalog.TableDescriptor) (tree.Statement, error) { |
| 149 | + columns := getPhysicalColumns(table) |
| 150 | + |
| 151 | + // Create WHERE clause placeholders for every column in the table |
| 152 | + var whereClause tree.Expr |
| 153 | + for i, col := range columns { |
| 154 | + placeholder, err := tree.NewPlaceholder(fmt.Sprintf("%d", i+1)) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + colExpr := &tree.ComparisonExpr{ |
| 159 | + Operator: treecmp.MakeComparisonOperator(treecmp.EQ), |
| 160 | + Left: &tree.ColumnItem{ColumnName: tree.Name(col.GetName())}, |
| 161 | + Right: placeholder, |
| 162 | + } |
| 163 | + |
| 164 | + if whereClause == nil { |
| 165 | + whereClause = colExpr |
| 166 | + } else { |
| 167 | + whereClause = &tree.AndExpr{ |
| 168 | + Left: whereClause, |
| 169 | + Right: colExpr, |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // Create the final delete statement |
| 175 | + delete := &tree.Delete{ |
| 176 | + Table: &tree.TableRef{ |
| 177 | + TableID: int64(table.GetID()), |
| 178 | + As: tree.AliasClause{Alias: "replication_target"}, |
| 179 | + }, |
| 180 | + Where: &tree.Where{Type: tree.AstWhere, Expr: whereClause}, |
| 181 | + Returning: tree.AbsentReturningClause, |
| 182 | + } |
| 183 | + |
| 184 | + return delete, nil |
| 185 | +} |
0 commit comments