Skip to content

Commit

Permalink
Merge #58072 #58431
Browse files Browse the repository at this point in the history
58072: sql: alias SHOW STATEMENTS to SHOW QUERIES r=rafiss a=ivern

Fixes #56240

This commit makes SHOW STATEMENTS a (preferred) alias of SHOW QUERIES.

Release note (sql change): SHOW STATEMENTS is now an alias of SHOW QUERIES.
The new syntax is preferred by the SQL parser.

58431: opt: assign placeholders for cascade queries r=rytaft a=rytaft

Prior to this commit, it was possible that placeholders could
appear in cascade queries without getting replaced, causing an error
at execution time. This commit fixes the problem by ensuring that
placeholders are replaced before the cascade query is executed.

Fixes #58028

Release note (bug fix): Fixed an internal error that could occur when
executing prepared statements with placeholders that delete data from
columns referenced by a foreign key with ON DELETE CASCADE.

Co-authored-by: Javier Fernandez-Ivern <[email protected]>
Co-authored-by: Rebecca Taft <[email protected]>
  • Loading branch information
3 people committed Jan 4, 2021
3 parents 3ebe64c + 26f763f + 00ec8b2 commit 7bcb2ce
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 45 deletions.
5 changes: 0 additions & 5 deletions docs/generated/sql/bnf/show_queries.bnf

This file was deleted.

9 changes: 9 additions & 0 deletions docs/generated/sql/bnf/show_statements.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
show_statements_stmt ::=
'SHOW' 'CLUSTER' 'STATEMENTS'
| 'SHOW' 'CLUSTER' 'QUERIES'
| 'SHOW' 'LOCAL' 'STATEMENTS'
| 'SHOW' 'LOCAL' 'QUERIES'
| 'SHOW' 'ALL' 'CLUSTER' 'STATEMENTS'
| 'SHOW' 'ALL' 'CLUSTER' 'QUERIES'
| 'SHOW' 'ALL' 'LOCAL' 'STATEMENTS'
| 'SHOW' 'ALL' 'LOCAL' 'QUERIES'
2 changes: 1 addition & 1 deletion docs/generated/sql/bnf/show_var.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ show_stmt ::=
| show_jobs_stmt
| show_locality_stmt
| show_schedules_stmt
| show_queries_stmt
| show_statements_stmt
| show_ranges_stmt
| show_range_for_row_stmt
| show_regions_stmt
Expand Down
13 changes: 9 additions & 4 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ show_stmt ::=
| show_jobs_stmt
| show_locality_stmt
| show_schedules_stmt
| show_queries_stmt
| show_statements_stmt
| show_ranges_stmt
| show_range_for_row_stmt
| show_regions_stmt
Expand Down Expand Up @@ -654,9 +654,9 @@ show_schedules_stmt ::=
| 'SHOW' schedule_state 'SCHEDULES' opt_schedule_executor_type
| 'SHOW' 'SCHEDULE' a_expr

show_queries_stmt ::=
'SHOW' opt_cluster 'QUERIES'
| 'SHOW' 'ALL' opt_cluster 'QUERIES'
show_statements_stmt ::=
'SHOW' opt_cluster statements_or_queries
| 'SHOW' 'ALL' opt_cluster statements_or_queries

show_ranges_stmt ::=
'SHOW' 'RANGES' 'FROM' 'TABLE' table_name
Expand Down Expand Up @@ -1057,6 +1057,7 @@ unreserved_keyword ::=
| 'SPLIT'
| 'SQL'
| 'START'
| 'STATEMENTS'
| 'STATISTICS'
| 'STDIN'
| 'STORAGE'
Expand Down Expand Up @@ -1518,6 +1519,10 @@ opt_cluster ::=
'CLUSTER'
| 'LOCAL'

statements_or_queries ::=
'STATEMENTS'
| 'QUERIES'

opt_compact ::=
'COMPACT'
|
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/docgen/diagrams.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,9 +1298,9 @@ var specs = []stmtSpec{
name: "show_partitions_stmt",
},
{
name: "show_queries",
stmt: "show_queries_stmt",
inline: []string{"opt_cluster"},
name: "show_statements",
stmt: "show_statements_stmt",
inline: []string{"opt_cluster", "statements_or_queries"},
},
{
name: "show_roles_stmt",
Expand Down
30 changes: 30 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/cascade
Original file line number Diff line number Diff line change
Expand Up @@ -4322,3 +4322,33 @@ NULL 3

statement ok
DROP TABLE child, parent

subtest AssignPlaceholders

# Regression test for #58028. Make sure that placeholders get assigned for
# cascade queries.

statement ok
CREATE TABLE parent (a INT PRIMARY KEY, b INT);
CREATE TABLE child (a INT PRIMARY KEY, p_a INT NOT NULL REFERENCES parent(a) ON DELETE CASCADE);
INSERT INTO parent VALUES (1, 2), (3, 4);
INSERT INTO child VALUES (1, 1), (3, 3);

statement ok
PREPARE del AS DELETE FROM parent WHERE a = $1

statement ok
EXECUTE del (1)

query II rowsort
SELECT * FROM parent
----
3 4

query II rowsort
SELECT * FROM child
----
3 3

statement ok
DROP TABLE child, parent
7 changes: 6 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,13 @@ query ITT colnames
SELECT node_id, user_name, query FROM [SHOW QUERIES]
----
node_id user_name query
1 root SELECT node_id, user_name, query FROM [SHOW CLUSTER QUERIES]
1 root SELECT node_id, user_name, query FROM [SHOW CLUSTER STATEMENTS]

query ITT colnames
SELECT node_id, user_name, query FROM [SHOW STATEMENTS]
----
node_id user_name query
1 root SELECT node_id, user_name, query FROM [SHOW CLUSTER STATEMENTS]

query TT colnames,rowsort
SELECT * FROM [SHOW SCHEMAS]
Expand Down
16 changes: 14 additions & 2 deletions pkg/sql/opt/exec/execbuilder/cascades.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,25 @@ func (cb *cascadeBuilder) planCascade(

o.Memo().SetRoot(relExpr, &physical.Required{})

// 3. Optimize the expression.
// 3. Assign placeholders if they exist.
if factory.Memo().HasPlaceholders() {
// Construct a new memo that is copied from the memo created above, but with
// placeholders assigned. Stable operators can be constant-folded at this
// time.
preparedMemo := o.DetachMemo()
factory.FoldingControl().AllowStableFolds()
if err := factory.AssignPlaceholders(preparedMemo); err != nil {
return nil, errors.Wrap(err, "while assigning placeholders in cascade expression")
}
}

// 4. Optimize the expression.
optimizedExpr, err := o.Optimize()
if err != nil {
return nil, errors.Wrap(err, "while optimizing cascade expression")
}

// 4. Execbuild the optimized expression.
// 5. Execbuild the optimized expression.
eb := New(execFactory, factory.Memo(), cb.b.catalog, optimizedExpr, evalCtx, allowAutoCommit)
if bufferRef != nil {
// Set up the With binding.
Expand Down
7 changes: 5 additions & 2 deletions pkg/sql/parser/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,11 @@ func TestContextualHelp(t *testing.T) {

{`SHOW HISTOGRAM ??`, `SHOW HISTOGRAM`},

{`SHOW QUERIES ??`, `SHOW QUERIES`},
{`SHOW LOCAL QUERIES ??`, `SHOW QUERIES`},
{`SHOW QUERIES ??`, `SHOW STATEMENTS`},
{`SHOW LOCAL QUERIES ??`, `SHOW STATEMENTS`},

{`SHOW STATEMENTS ??`, `SHOW STATEMENTS`},
{`SHOW LOCAL STATEMENTS ??`, `SHOW STATEMENTS`},

{`SHOW TRACE ??`, `SHOW TRACE`},
{`SHOW TRACE FOR SESSION ??`, `SHOW TRACE`},
Expand Down
22 changes: 12 additions & 10 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,14 +616,14 @@ func TestParse(t *testing.T) {
{`EXPLAIN SHOW JOBS`},
{`SHOW AUTOMATIC JOBS`},
{`EXPLAIN SHOW AUTOMATIC JOBS`},
{`SHOW CLUSTER QUERIES`},
{`EXPLAIN SHOW CLUSTER QUERIES`},
{`SHOW ALL CLUSTER QUERIES`},
{`EXPLAIN SHOW ALL CLUSTER QUERIES`},
{`SHOW LOCAL QUERIES`},
{`EXPLAIN SHOW LOCAL QUERIES`},
{`SHOW ALL LOCAL QUERIES`},
{`EXPLAIN SHOW ALL LOCAL QUERIES`},
{`SHOW CLUSTER STATEMENTS`},
{`EXPLAIN SHOW CLUSTER STATEMENTS`},
{`SHOW ALL CLUSTER STATEMENTS`},
{`EXPLAIN SHOW ALL CLUSTER STATEMENTS`},
{`SHOW LOCAL STATEMENTS`},
{`EXPLAIN SHOW LOCAL STATEMENTS`},
{`SHOW ALL LOCAL STATEMENTS`},
{`EXPLAIN SHOW ALL LOCAL STATEMENTS`},
{`SHOW CLUSTER SESSIONS`},
{`EXPLAIN SHOW CLUSTER SESSIONS`},
{`SHOW ALL CLUSTER SESSIONS`},
Expand Down Expand Up @@ -2341,8 +2341,10 @@ $function$`,
{`SHOW ALL SESSIONS`, `SHOW ALL CLUSTER SESSIONS`},
{`SHOW TRANSACTIONS`, `SHOW CLUSTER TRANSACTIONS`},
{`SHOW ALL TRANSACTIONS`, `SHOW ALL CLUSTER TRANSACTIONS`},
{`SHOW QUERIES`, `SHOW CLUSTER QUERIES`},
{`SHOW ALL QUERIES`, `SHOW ALL CLUSTER QUERIES`},
{`SHOW QUERIES`, `SHOW CLUSTER STATEMENTS`},
{`SHOW STATEMENTS`, `SHOW CLUSTER STATEMENTS`},
{`SHOW ALL QUERIES`, `SHOW ALL CLUSTER STATEMENTS`},
{`SHOW ALL STATEMENTS`, `SHOW ALL CLUSTER STATEMENTS`},

{`USE foo`, `SET database = foo`},

Expand Down
33 changes: 21 additions & 12 deletions pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func (u *sqlSymUnion) objectNamePrefixList() tree.ObjectNamePrefixList {
%token <str> SKIP_MISSING_SEQUENCES SKIP_MISSING_SEQUENCE_OWNERS SKIP_MISSING_VIEWS SMALLINT SMALLSERIAL SNAPSHOT SOME SPLIT SQL

%token <str> START STATISTICS STATUS STDIN STRICT STRING STORAGE STORE STORED STORING SUBSTRING
%token <str> SURVIVE SURVIVAL SYMMETRIC SYNTAX SYSTEM SQRT SUBSCRIPTION
%token <str> SURVIVE SURVIVAL SYMMETRIC SYNTAX SYSTEM SQRT SUBSCRIPTION STATEMENTS

%token <str> TABLE TABLES TABLESPACE TEMP TEMPLATE TEMPORARY TENANT TESTING_RELOCATE EXPERIMENTAL_RELOCATE TEXT THEN
%token <str> TIES TIME TIMETZ TIMESTAMP TIMESTAMPTZ TO THROTTLING TRAILING TRACE
Expand Down Expand Up @@ -886,7 +886,7 @@ func (u *sqlSymUnion) objectNamePrefixList() tree.ObjectNamePrefixList {
%type <tree.Statement> show_indexes_stmt
%type <tree.Statement> show_partitions_stmt
%type <tree.Statement> show_jobs_stmt
%type <tree.Statement> show_queries_stmt
%type <tree.Statement> show_statements_stmt
%type <tree.Statement> show_ranges_stmt
%type <tree.Statement> show_range_for_row_stmt
%type <tree.Statement> show_locality_stmt
Expand All @@ -910,6 +910,8 @@ func (u *sqlSymUnion) objectNamePrefixList() tree.ObjectNamePrefixList {
%type <tree.Statement> show_zone_stmt
%type <tree.Statement> show_schedules_stmt

%type <str> statements_or_queries

%type <str> session_var
%type <*string> comment_text

Expand Down Expand Up @@ -2956,7 +2958,7 @@ cancel_jobs_stmt:
// %Text:
// CANCEL QUERIES [IF EXISTS] <selectclause>
// CANCEL QUERY [IF EXISTS] <expr>
// %SeeAlso: SHOW QUERIES
// %SeeAlso: SHOW STATEMENTS
cancel_queries_stmt:
CANCEL QUERY a_expr
{
Expand Down Expand Up @@ -4346,7 +4348,7 @@ zone_value:
// %Text:
// SHOW BACKUP, SHOW CLUSTER SETTING, SHOW COLUMNS, SHOW CONSTRAINTS,
// SHOW CREATE, SHOW DATABASES, SHOW ENUMS, SHOW HISTOGRAM, SHOW INDEXES, SHOW
// PARTITIONS, SHOW JOBS, SHOW QUERIES, SHOW RANGE, SHOW RANGES, SHOW REGIONS, SHOW SURVIVAL GOAL,
// PARTITIONS, SHOW JOBS, SHOW STATEMENTS, SHOW RANGE, SHOW RANGES, SHOW REGIONS, SHOW SURVIVAL GOAL,
// SHOW ROLES, SHOW SCHEMAS, SHOW SEQUENCES, SHOW SESSION, SHOW SESSIONS,
// SHOW STATISTICS, SHOW SYNTAX, SHOW TABLES, SHOW TRACE, SHOW TRANSACTION,
// SHOW TRANSACTIONS, SHOW TYPES, SHOW USERS, SHOW LAST QUERY STATISTICS, SHOW SCHEDULES,
Expand All @@ -4368,7 +4370,7 @@ show_stmt:
| show_jobs_stmt // EXTEND WITH HELP: SHOW JOBS
| show_locality_stmt
| show_schedules_stmt // EXTEND WITH HELP: SHOW SCHEDULES
| show_queries_stmt // EXTEND WITH HELP: SHOW QUERIES
| show_statements_stmt // EXTEND WITH HELP: SHOW STATEMENTS
| show_ranges_stmt // EXTEND WITH HELP: SHOW RANGES
| show_range_for_row_stmt
| show_regions_stmt // EXTEND WITH HELP: SHOW REGIONS
Expand Down Expand Up @@ -4734,21 +4736,21 @@ show_constraints_stmt:
}
| SHOW CONSTRAINTS error // SHOW HELP: SHOW CONSTRAINTS

// %Help: SHOW QUERIES - list running queries
// %Help: SHOW STATEMENTS - list running statements
// %Category: Misc
// %Text: SHOW [ALL] [CLUSTER | LOCAL] QUERIES
// %Text: SHOW [ALL] [CLUSTER | LOCAL] STATEMENTS
// %SeeAlso: CANCEL QUERIES
show_queries_stmt:
SHOW opt_cluster QUERIES
show_statements_stmt:
SHOW opt_cluster statements_or_queries
{
$$.val = &tree.ShowQueries{All: false, Cluster: $2.bool()}
}
| SHOW opt_cluster QUERIES error // SHOW HELP: SHOW QUERIES
| SHOW ALL opt_cluster QUERIES
| SHOW opt_cluster statements_or_queries error // SHOW HELP: SHOW STATEMENTS
| SHOW ALL opt_cluster statements_or_queries
{
$$.val = &tree.ShowQueries{All: true, Cluster: $3.bool()}
}
| SHOW ALL opt_cluster QUERIES error // SHOW HELP: SHOW QUERIES
| SHOW ALL opt_cluster statements_or_queries error // SHOW HELP: SHOW STATEMENTS

opt_cluster:
/* EMPTY */
Expand All @@ -4758,6 +4760,12 @@ opt_cluster:
| LOCAL
{ $$.val = false }

// SHOW QUERIES is now an alias for SHOW STATEMENTS
// https://github.com/cockroachdb/cockroach/issues/56240
statements_or_queries:
STATEMENTS
| QUERIES

// %Help: SHOW JOBS - list background jobs
// %Category: Misc
// %Text:
Expand Down Expand Up @@ -12276,6 +12284,7 @@ unreserved_keyword:
| SPLIT
| SQL
| START
| STATEMENTS
| STATISTICS
| STDIN
| STORAGE
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/run_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestCancelDistSQLQuery(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
const queryToCancel = "SELECT * FROM nums ORDER BY num"
cancelQuery := fmt.Sprintf("CANCEL QUERIES SELECT query_id FROM [SHOW CLUSTER QUERIES] WHERE query = '%s'", queryToCancel)
cancelQuery := fmt.Sprintf("CANCEL QUERIES SELECT query_id FROM [SHOW CLUSTER STATEMENTS] WHERE query = '%s'", queryToCancel)

// conn1 is used for the query above. conn2 is solely for the CANCEL statement.
var conn1 *gosql.DB
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/sem/tree/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (node *ShowDatabaseIndexes) Format(ctx *FmtCtx) {
}
}

// ShowQueries represents a SHOW QUERIES statement.
// ShowQueries represents a SHOW STATEMENTS statement.
type ShowQueries struct {
All bool
Cluster bool
Expand All @@ -240,9 +240,9 @@ func (node *ShowQueries) Format(ctx *FmtCtx) {
ctx.WriteString("ALL ")
}
if node.Cluster {
ctx.WriteString("CLUSTER QUERIES")
ctx.WriteString("CLUSTER STATEMENTS")
} else {
ctx.WriteString("LOCAL QUERIES")
ctx.WriteString("LOCAL STATEMENTS")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/tree/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ func (*ShowPartitions) StatementTag() string { return "SHOW PARTITIONS" }
func (*ShowQueries) StatementType() StatementType { return Rows }

// StatementTag returns a short string identifying the type of statement.
func (*ShowQueries) StatementTag() string { return "SHOW QUERIES" }
func (*ShowQueries) StatementTag() string { return "SHOW STATEMENTS" }

// StatementType implements the Statement interface.
func (*ShowJobs) StatementType() StatementType { return Rows }
Expand Down

0 comments on commit 7bcb2ce

Please sign in to comment.