Skip to content

Commit

Permalink
sql: change SPLIT AT to take a select statement
Browse files Browse the repository at this point in the history
`SPLIT AT` now takes an arbitrary select statement. Existing uses must switch to
using `VALUES`; e.g. `ALTER TABLE t SPLIT AT (x, y)` becomes `ALTER TABLE t
SPLIT AT VALUES (x, y)`.

Part of cockroachdb#13665, implements part of RFC cockroachdb#14146.
  • Loading branch information
RaduBerinde committed Mar 21, 2017
1 parent 054528a commit 40f7517
Show file tree
Hide file tree
Showing 13 changed files with 3,116 additions and 3,002 deletions.
22 changes: 13 additions & 9 deletions pkg/ccl/sqlccl/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,23 @@ func bankDataInsertStmts(count int) []string {
return statements
}

func bankSplitStmts(numAccounts int, numRanges int) []string {
func bankSplitStmt(numAccounts int, numRanges int) string {
// Asking for more splits than ranges doesn't make any sense. Because of the
// way go benchmarks work, split each row into a range instead of erroring.
if numRanges > numAccounts {
numRanges = numAccounts
}
var statements []string

var stmt bytes.Buffer
stmt.WriteString("ALTER TABLE bench.bank SPLIT AT VALUES ")

for i, incr := 1, numAccounts/numRanges; i < numRanges; i++ {
s := fmt.Sprintf(`ALTER TABLE bench.bank SPLIT AT (%d)`, i*incr)
statements = append(statements, s)
if i > 1 {
stmt.WriteString(", ")
}
fmt.Fprintf(&stmt, "(%d)", i*incr)
}
return statements
return stmt.String()
}

func backupRestoreTestSetup(
Expand All @@ -115,10 +120,9 @@ func backupRestoreTestSetup(
for _, insert := range bankDataInsertStmts(numAccounts) {
sqlDB.Exec(insert)
}
for _, split := range bankSplitStmts(numAccounts, backupRestoreDefaultRanges) {
// This occasionally flakes, so ignore errors.
_, _ = sqlDB.DB.Exec(split)
}
split := bankSplitStmt(numAccounts, backupRestoreDefaultRanges)
// This occasionally flakes, so ignore errors.
_, _ = sqlDB.DB.Exec(split)
}

if err := tc.WaitForFullReplication(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/storageccl/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestExport(t *testing.T) {
t.Fatalf("expected a deletion tombstone got %s", v.PrettyPrint())
}

sqlDB.Exec(`ALTER TABLE export.export SPLIT AT (2)`)
sqlDB.Exec(`ALTER TABLE export.export SPLIT AT VALUES (2)`)
_, paths5, kvs5 := exportAndSlurp(hlc.Timestamp{})
if expected := 2; len(paths5) != expected {
t.Fatalf("expected %d files in export got %d", expected, len(paths5))
Expand Down
13 changes: 7 additions & 6 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,13 @@ func TestParse(t *testing.T) {
{`COPY t FROM STDIN`},
{`COPY t (a, b, c) FROM STDIN`},

{`ALTER TABLE a SPLIT AT (1)`},
{`ALTER TABLE d.a SPLIT AT ('b', 2)`},
{`ALTER INDEX a@i SPLIT AT (1)`},
{`ALTER INDEX d.a@i SPLIT AT (2)`},
{`ALTER INDEX i SPLIT AT (1)`},
{`ALTER INDEX d.i SPLIT AT (2)`},
{`ALTER TABLE a SPLIT AT VALUES (1)`},
{`ALTER TABLE a SPLIT AT SELECT * FROM t`},
{`ALTER TABLE d.a SPLIT AT VALUES ('b', 2)`},
{`ALTER INDEX a@i SPLIT AT VALUES (1)`},
{`ALTER INDEX d.a@i SPLIT AT VALUES (2)`},
{`ALTER INDEX i SPLIT AT VALUES (1)`},
{`ALTER INDEX d.i SPLIT AT VALUES (2)`},

{`BACKUP foo TO 'bar'`},
{`BACKUP foo.foo, baz.baz TO 'bar'`},
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/parser/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import "bytes"
type Split struct {
Table NormalizableTableName
Index *TableNameWithIndex
Exprs Exprs
// Each row contains values for the columns in the PK or index (or a prefix
// of the columns).
Rows *Select
}

// Format implements the NodeFormatter interface.
Expand All @@ -35,7 +37,6 @@ func (node *Split) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("TABLE ")
FormatNode(buf, f, node.Table)
}
buf.WriteString(" SPLIT AT (")
FormatNode(buf, f, node.Exprs)
buf.WriteString(")")
buf.WriteString(" SPLIT AT ")
FormatNode(buf, f, node.Rows)
}
Loading

0 comments on commit 40f7517

Please sign in to comment.