Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: support scalar expressions (& placeholders) for alter range ... relocate #73807

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/generated/sql/bnf/alter_range_relocate_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
alter_range_relocate_stmt ::=
'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' iconst64 'FOR' select_stmt
| 'ALTER' 'RANGE' iconst64 relocate_kw 'LEASE' 'TO' iconst64
| 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt
| 'ALTER' 'RANGE' iconst64 relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64
'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' a_expr 'FOR' select_stmt
| 'ALTER' 'RANGE' a_expr relocate_kw 'LEASE' 'TO' a_expr
| 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' a_expr 'TO' a_expr 'FOR' select_stmt
| 'ALTER' 'RANGE' a_expr relocate_kw relocate_subject_nonlease 'FROM' a_expr 'TO' a_expr
6 changes: 3 additions & 3 deletions docs/generated/sql/bnf/alter_zone_range_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
alter_zone_range_stmt ::=
'ALTER' 'RANGE' range_name 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'RANGE' range_name 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'RANGE' range_name 'CONFIGURE' 'ZONE' 'DISCARD'
'ALTER' 'RANGE' a_expr 'CONFIGURE' 'ZONE' 'USING' variable '=' 'COPY' 'FROM' 'PARENT' ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'RANGE' a_expr 'CONFIGURE' 'ZONE' 'USING' variable '=' value ( ( ',' variable '=' value | ',' variable '=' 'COPY' 'FROM' 'PARENT' ) )*
| 'ALTER' 'RANGE' a_expr 'CONFIGURE' 'ZONE' 'DISCARD'
10 changes: 5 additions & 5 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1843,13 +1843,13 @@ alter_database_primary_region_stmt ::=
| 'ALTER' 'DATABASE' database_name 'SET' primary_region_clause

alter_zone_range_stmt ::=
'ALTER' 'RANGE' zone_name set_zone_config
'ALTER' 'RANGE' a_expr set_zone_config

alter_range_relocate_stmt ::=
'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' iconst64 'FOR' select_stmt
| 'ALTER' 'RANGE' iconst64 relocate_kw 'LEASE' 'TO' iconst64
| 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64 'FOR' select_stmt
| 'ALTER' 'RANGE' iconst64 relocate_kw relocate_subject_nonlease 'FROM' iconst64 'TO' iconst64
'ALTER' 'RANGE' relocate_kw 'LEASE' 'TO' a_expr 'FOR' select_stmt
| 'ALTER' 'RANGE' a_expr relocate_kw 'LEASE' 'TO' a_expr
| 'ALTER' 'RANGE' relocate_kw relocate_subject_nonlease 'FROM' a_expr 'TO' a_expr 'FOR' select_stmt
| 'ALTER' 'RANGE' a_expr relocate_kw relocate_subject_nonlease 'FROM' a_expr 'TO' a_expr

alter_zone_partition_stmt ::=
'ALTER' 'PARTITION' partition_name 'OF' 'TABLE' table_name set_zone_config
Expand Down
6 changes: 3 additions & 3 deletions pkg/kv/kvserver/client_lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 +969,11 @@ func TestAlterRangeRelocate(t *testing.T) {
// We start with having the range under test on (1,2,3).
db := tc.ServerConn(0)
// Move 2 -> 4.
_, err := db.Exec("ALTER RANGE " + rhsDesc.RangeID.String() + " RELOCATE FROM 2 TO 4")
_, err := db.Exec("ALTER RANGE $1 RELOCATE FROM $2 TO $3", rhsDesc.RangeID, 2, 4)
require.NoError(t, err)
require.NoError(t, tc.WaitForVoters(rhsDesc.StartKey.AsRawKey(), tc.Targets(0, 2, 3)...))
// Move lease 1 -> 4.
_, err = db.Exec("ALTER RANGE " + rhsDesc.RangeID.String() + " RELOCATE LEASE TO 4")
_, err = db.Exec("ALTER RANGE $1 RELOCATE LEASE TO $2", rhsDesc.RangeID, 4)
require.NoError(t, err)
testutils.SucceedsSoon(t, func() error {
repl := tc.GetFirstStoreFromServer(t, 3).LookupReplica(rhsDesc.StartKey)
Expand All @@ -988,7 +988,7 @@ func TestAlterRangeRelocate(t *testing.T) {
})

// Move lease 3 -> 5.
_, err = db.Exec("ALTER RANGE RELOCATE FROM 3 TO 5 FOR (SELECT range_id from crdb_internal.ranges where range_id = " + rhsDesc.RangeID.String() + ")")
_, err = db.Exec("ALTER RANGE RELOCATE FROM $1 TO $2 FOR (SELECT range_id from crdb_internal.ranges where range_id = $3)", 3, 5, rhsDesc.RangeID)
require.NoError(t, err)
require.NoError(t, tc.WaitForVoters(rhsDesc.StartKey.AsRawKey(), tc.Targets(0, 3, 4)...))
}
5 changes: 4 additions & 1 deletion pkg/sql/distsql_spec_exec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,10 @@ func (e *distSQLSpecExecFactory) ConstructAlterTableRelocate(
}

func (e *distSQLSpecExecFactory) ConstructAlterRangeRelocate(
input exec.Node, relocateSubject tree.RelocateSubject, toStoreID int64, fromStoreID int64,
input exec.Node,
relocateSubject tree.RelocateSubject,
toStoreID tree.TypedExpr,
fromStoreID tree.TypedExpr,
) (exec.Node, error) {
return nil, unimplemented.NewWithIssue(47473, "experimental opt-driven distsql planning: alter range relocate")
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/sql/opt/exec/execbuilder/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,20 @@ func (b *Builder) buildAlterRangeRelocate(relocate *memo.AlterRangeRelocateExpr)
if err != nil {
return execPlan{}, err
}
scalarCtx := buildScalarCtx{}
toStoreID, err := b.buildScalar(&scalarCtx, relocate.ToStoreID)
if err != nil {
return execPlan{}, err
}
fromStoreID, err := b.buildScalar(&scalarCtx, relocate.FromStoreID)
if err != nil {
return execPlan{}, err
}
node, err := b.factory.ConstructAlterRangeRelocate(
input.root,
relocate.SubjectReplicas,
relocate.ToStoreID,
relocate.FromStoreID,
toStoreID,
fromStoreID,
)
if err != nil {
return execPlan{}, err
Expand Down
135 changes: 135 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,60 @@ vectorized: true
size: 1 column, 1 row
row 0, expr 0: 42

query T
EXPLAIN ALTER TABLE foo RELOCATE VALUES (ARRAY[1], 1)
----
distribution: local
vectorized: true
·
• relocate table
└── • values
size: 2 columns, 1 row

query T
EXPLAIN (VERBOSE) ALTER TABLE foo RELOCATE VALUES (ARRAY[1], 1)
----
distribution: local
vectorized: true
·
• relocate table
│ columns: (key, pretty)
│ estimated row count: 10 (missing stats)
└── • values
columns: (column1, column2)
size: 2 columns, 1 row
row 0, expr 0: ARRAY[1]
row 0, expr 1: 1

query T
EXPLAIN ALTER INDEX foo@a RELOCATE VALUES (ARRAY[1], 1)
----
distribution: local
vectorized: true
·
• relocate table
└── • values
size: 2 columns, 1 row

query T
EXPLAIN (VERBOSE) ALTER INDEX foo@a RELOCATE VALUES (ARRAY[1], 1)
----
distribution: local
vectorized: true
·
• relocate table
│ columns: (key, pretty)
│ estimated row count: 10 (missing stats)
└── • values
columns: (column1, column2)
size: 2 columns, 1 row
row 0, expr 0: ARRAY[1]
row 0, expr 1: 1

query T
EXPLAIN DROP TABLE foo
----
Expand All @@ -184,6 +238,87 @@ vectorized: true
·
• drop table

query T
EXPLAIN ALTER RANGE 20+2 RELOCATE FROM 30+3 TO 40+4
----
distribution: local
vectorized: true
·
• relocate range
│ replicas: VOTERS
│ to: 44
│ from: 33
└── • values
size: 1 column, 1 row

query T
EXPLAIN (VERBOSE) ALTER RANGE 20+2 RELOCATE LEASE TO 40+4
----
distribution: local
vectorized: true
·
• relocate range
│ columns: (range_id, pretty, result)
│ estimated row count: 10 (missing stats)
│ replicas: LEASE
│ to: 44
└── • values
columns: (column1)
size: 1 column, 1 row
row 0, expr 0: 22

query T
EXPLAIN (VERBOSE) ALTER RANGE 20+2 RELOCATE FROM 30+3 TO 40+4
----
distribution: local
vectorized: true
·
• relocate range
│ columns: (range_id, pretty, result)
│ estimated row count: 10 (missing stats)
│ replicas: VOTERS
│ to: 44
│ from: 33
└── • values
columns: (column1)
size: 1 column, 1 row
row 0, expr 0: 22

query T
EXPLAIN ALTER RANGE RELOCATE FROM 30+3 TO 40+4 FOR SELECT 20+2
----
distribution: local
vectorized: true
·
• relocate range
│ replicas: VOTERS
│ to: 44
│ from: 33
└── • values
size: 1 column, 1 row

query T
EXPLAIN (VERBOSE) ALTER RANGE RELOCATE FROM 30+3 TO 40+4 FOR SELECT 20+2
----
distribution: local
vectorized: true
·
• relocate range
│ columns: (range_id, pretty, result)
│ estimated row count: 10 (missing stats)
│ replicas: VOTERS
│ to: 44
│ from: 33
└── • values
columns: ("?column?")
size: 1 column, 1 row
row 0, expr 0: 22

query T
EXPLAIN SHOW DATABASES
----
Expand Down
56 changes: 56 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/subquery
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,62 @@ vectorized: true
statement ok
ALTER TABLE abc SPLIT AT VALUES ((SELECT 1))

query T
EXPLAIN ALTER RANGE RELOCATE FROM 11 TO 22 FOR VALUES ((SELECT 1))
----
distribution: local
vectorized: true
·
• root
├── • relocate range
│ │ replicas: VOTERS
│ │ to: 22
│ │ from: 11
│ │
│ └── • values
│ size: 1 column, 1 row
└── • subquery
│ id: @S1
│ original sql: (SELECT 1)
│ exec mode: one row
└── • values
size: 1 column, 1 row

query T
EXPLAIN ALTER RANGE 22 RELOCATE FROM ((SELECT 1)) TO ((SELECT 2))
----
distribution: local
vectorized: true
·
• root
├── • relocate range
│ │ replicas: VOTERS
│ │ to: @S1
│ │ from: @S2
│ │
│ └── • values
│ size: 1 column, 1 row
├── • subquery
│ │ id: @S1
│ │ original sql: ((SELECT 2))
│ │ exec mode: one row
│ │
│ └── • values
│ size: 1 column, 1 row
└── • subquery
│ id: @S2
│ original sql: ((SELECT 1))
│ exec mode: one row
└── • values
size: 1 column, 1 row

query T
EXPLAIN SELECT EXISTS (SELECT a FROM abc)
----
Expand Down
15 changes: 11 additions & 4 deletions pkg/sql/opt/exec/explain/emit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/dustin/go-humanize"
humanize "github.com/dustin/go-humanize"
)

// Emit produces the EXPLAIN output against the given OutputBuilder. The
Expand Down Expand Up @@ -271,8 +271,8 @@ func (e *emitter) nodeName(n *Node) (string, error) {
}

var nodeNames = [...]string{
alterRangeRelocateOp: "relocate",
alterTableRelocateOp: "relocate",
alterRangeRelocateOp: "relocate range",
alterTableRelocateOp: "relocate table",
alterTableSplitOp: "split",
alterTableUnsplitAllOp: "unsplit all",
alterTableUnsplitOp: "unsplit",
Expand Down Expand Up @@ -828,6 +828,14 @@ func (e *emitter) emitNodeAttributes(n *Node) error {
ob.Attrf("deduplicate", "")
}

case alterRangeRelocateOp:
a := n.args.(*alterRangeRelocateArgs)
ob.Attr("replicas", a.subjectReplicas)
ob.Expr("to", a.toStoreID, nil /* columns */)
if a.subjectReplicas != tree.RelocateLease {
ob.Expr("from", a.fromStoreID, nil /* columns */)
}

case simpleProjectOp,
serializingProjectOp,
ordinalityOp,
Expand All @@ -848,7 +856,6 @@ func (e *emitter) emitNodeAttributes(n *Node) error {
alterTableUnsplitOp,
alterTableUnsplitAllOp,
alterTableRelocateOp,
alterRangeRelocateOp,
controlJobsOp,
controlSchedulesOp,
cancelQueriesOp,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/explain/testdata/gists
Original file line number Diff line number Diff line change
Expand Up @@ -907,12 +907,12 @@ ALTER TABLE abc EXPERIMENTAL_RELOCATE VALUES (ARRAY[1],1)
hash: 8656743312841134920
plan-gist: AgICBAYEL24CBgQ=
explain(shape):
• relocate
• relocate table
└── • values
size: 2 columns, 1 row
explain(gist):
• relocate
• relocate table
└── • values
size: 2 columns, 1 row
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/factory.opt
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,6 @@ define Export {
define AlterRangeRelocate {
input exec.Node
subjectReplicas tree.RelocateSubject
toStoreID int64
fromStoreID int64
toStoreID tree.TypedExpr
fromStoreID tree.TypedExpr
}
Loading