-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathunsplit.go
132 lines (113 loc) · 3.38 KB
/
unsplit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2019 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/pkg/errors"
)
type unsplitNode struct {
optColumnsSlot
tableDesc *sqlbase.TableDescriptor
index *sqlbase.IndexDescriptor
rows planNode
run unsplitRun
}
// Unsplit executes a KV unsplit.
// Privileges: INSERT on table.
func (p *planner) Unsplit(ctx context.Context, n *tree.Unsplit) (planNode, error) {
tableDesc, index, err := p.getTableAndIndex(ctx, &n.TableOrIndex, privilege.INSERT)
if err != nil {
return nil, err
}
// Calculate the desired types for the select statement. It is OK if the
// select statement returns fewer columns (the relevant prefix is used).
desiredTypes := make([]*types.T, len(index.ColumnIDs))
for i, colID := range index.ColumnIDs {
c, err := tableDesc.FindColumnByID(colID)
if err != nil {
return nil, err
}
desiredTypes[i] = &c.Type
}
// Create the plan for the unsplit rows source.
rows, err := p.newPlan(ctx, n.Rows, desiredTypes)
if err != nil {
return nil, err
}
cols := planColumns(rows)
if len(cols) == 0 {
return nil, errors.Errorf("no columns in UNSPLIT AT data")
}
if len(cols) > len(index.ColumnIDs) {
return nil, errors.Errorf("too many columns in UNSPLIT AT data")
}
for i := range cols {
if !cols[i].Typ.Equivalent(desiredTypes[i]) {
return nil, errors.Errorf(
"UNSPLIT AT data column %d (%s) must be of type %s, not type %s",
i+1, index.ColumnNames[i], desiredTypes[i], cols[i].Typ,
)
}
}
return &unsplitNode{
tableDesc: tableDesc.TableDesc(),
index: index,
rows: rows,
}, nil
}
var unsplitNodeColumns = sqlbase.ResultColumns{
{
Name: "key",
Typ: types.Bytes,
},
{
Name: "pretty",
Typ: types.String,
},
}
// unsplitRun contains the run-time state of unsplitNode during local execution.
type unsplitRun struct {
lastUnsplitKey []byte
}
func (n *unsplitNode) startExec(params runParams) error {
return nil
}
func (n *unsplitNode) Next(params runParams) (bool, error) {
if ok, err := n.rows.Next(params); err != nil || !ok {
return ok, err
}
rowKey, err := getRowKey(n.tableDesc, n.index, n.rows.Values())
if err != nil {
return false, err
}
if err := params.extendedEvalCtx.ExecCfg.DB.AdminUnsplit(params.ctx, rowKey); err != nil {
return false, err
}
n.run.lastUnsplitKey = rowKey
return true, nil
}
func (n *unsplitNode) Values() tree.Datums {
return tree.Datums{
tree.NewDBytes(tree.DBytes(n.run.lastUnsplitKey)),
tree.NewDString(keys.PrettyPrint(nil /* valDirs */, n.run.lastUnsplitKey)),
}
}
func (n *unsplitNode) Close(ctx context.Context) {
n.rows.Close(ctx)
}