From c8c14a65c14b204d0a5fb6f9c3761942114b76d0 Mon Sep 17 00:00:00 2001 From: e-mbrown Date: Tue, 9 Aug 2022 14:18:54 -0400 Subject: [PATCH] sql: Support DISCARD SEQUENCES Release note (sql change): We now support DISCARD SEQUENCES, which discards all sequence-related state such as currval/lastval information. DISCARD ALL now also discards sequence-related state. Release justification: Bug Fix --- docs/generated/sql/bnf/discard_stmt.bnf | 1 + docs/generated/sql/bnf/stmt_block.bnf | 1 + pkg/sql/discard.go | 12 ++++ pkg/sql/logictest/testdata/logic_test/discard | 60 +++++++++++++++++++ pkg/sql/parser/parse_test.go | 1 - pkg/sql/parser/sql.y | 5 +- pkg/sql/sem/tree/discard.go | 5 ++ 7 files changed, 83 insertions(+), 2 deletions(-) diff --git a/docs/generated/sql/bnf/discard_stmt.bnf b/docs/generated/sql/bnf/discard_stmt.bnf index 73f00c6615c1..9f84f9881f71 100644 --- a/docs/generated/sql/bnf/discard_stmt.bnf +++ b/docs/generated/sql/bnf/discard_stmt.bnf @@ -1,2 +1,3 @@ discard_stmt ::= 'DISCARD' 'ALL' + | 'DISCARD' 'SEQUENCES' diff --git a/docs/generated/sql/bnf/stmt_block.bnf b/docs/generated/sql/bnf/stmt_block.bnf index 8e896b6c0df3..9ee2f35bf423 100644 --- a/docs/generated/sql/bnf/stmt_block.bnf +++ b/docs/generated/sql/bnf/stmt_block.bnf @@ -83,6 +83,7 @@ deallocate_stmt ::= discard_stmt ::= 'DISCARD' 'ALL' + | 'DISCARD' 'SEQUENCES' grant_stmt ::= 'GRANT' privileges 'ON' grant_targets 'TO' role_spec_list opt_with_grant_option diff --git a/pkg/sql/discard.go b/pkg/sql/discard.go index b1ddf6504330..c4abcef719ad 100644 --- a/pkg/sql/discard.go +++ b/pkg/sql/discard.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" + "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/errors" ) @@ -52,6 +53,17 @@ func (n *discardNode) startExec(params runParams) error { // DEALLOCATE ALL params.p.preparedStatements.DeleteAll(params.ctx) + + // DISCARD SEQUENCES + params.p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) { + m.data.SequenceState = sessiondata.NewSequenceState() + m.initSequenceCache() + }) + case tree.DiscardModeSequences: + params.p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) { + m.data.SequenceState = sessiondata.NewSequenceState() + m.initSequenceCache() + }) default: return errors.AssertionFailedf("unknown mode for DISCARD: %d", n.mode) } diff --git a/pkg/sql/logictest/testdata/logic_test/discard b/pkg/sql/logictest/testdata/logic_test/discard index 7ce33dd80e40..8160f4eadb92 100644 --- a/pkg/sql/logictest/testdata/logic_test/discard +++ b/pkg/sql/logictest/testdata/logic_test/discard @@ -54,3 +54,63 @@ BEGIN statement error DISCARD ALL cannot run inside a transaction block DISCARD ALL + +statement ok +ROLLBACK + +statement ok +CREATE SEQUENCE discard_seq_test START WITH 10 + +query I +SELECT nextval('discard_seq_test') +---- +10 + +query I +SELECT lastval() +---- +10 + +query I +SELECT currval('discard_seq_test') +---- +10 + +statement ok +DISCARD SEQUENCES + +statement error pgcode 55000 pq: lastval\(\): lastval is not yet defined in this session +SELECT lastval() + +statement error pgcode 55000 pq: currval\(\): currval of sequence "test.public.discard_seq_test" is not yet defined in this session +SELECT currval('discard_seq_test') + +statement ok +CREATE SEQUENCE discard_seq_test_2 START WITH 10 + +query I +SELECT nextval('discard_seq_test_2') +---- +10 + +statement ok +DISCARD ALL + +statement error pgcode 55000 pq: lastval\(\): lastval is not yet defined in this session +SELECT lastval() + +statement ok +CREATE SEQUENCE S2 CACHE 10 + +query I +SELECT nextval('s2') +---- +1 + +statement ok +DISCARD SEQUENCES + +query I +SELECT nextval('s2') +---- +11 diff --git a/pkg/sql/parser/parse_test.go b/pkg/sql/parser/parse_test.go index 199df1908bdd..855e4b116623 100644 --- a/pkg/sql/parser/parse_test.go +++ b/pkg/sql/parser/parse_test.go @@ -450,7 +450,6 @@ func TestUnimplementedSyntax(t *testing.T) { {`DROP TRIGGER a`, 28296, `drop`, ``}, {`DISCARD PLANS`, 0, `discard plans`, ``}, - {`DISCARD SEQUENCES`, 0, `discard sequences`, ``}, {`DISCARD TEMP`, 0, `discard temp`, ``}, {`DISCARD TEMPORARY`, 0, `discard temp`, ``}, diff --git a/pkg/sql/parser/sql.y b/pkg/sql/parser/sql.y index 65e097002a80..0ffb76464e29 100644 --- a/pkg/sql/parser/sql.y +++ b/pkg/sql/parser/sql.y @@ -4805,7 +4805,10 @@ discard_stmt: $$.val = &tree.Discard{Mode: tree.DiscardModeAll} } | DISCARD PLANS { return unimplemented(sqllex, "discard plans") } -| DISCARD SEQUENCES { return unimplemented(sqllex, "discard sequences") } +| DISCARD SEQUENCES + { + $$.val = &tree.Discard{Mode: tree.DiscardModeSequences} + } | DISCARD TEMP { return unimplemented(sqllex, "discard temp") } | DISCARD TEMPORARY { return unimplemented(sqllex, "discard temp") } | DISCARD error // SHOW HELP: DISCARD diff --git a/pkg/sql/sem/tree/discard.go b/pkg/sql/sem/tree/discard.go index 857693c19ffd..e7407f5caddc 100644 --- a/pkg/sql/sem/tree/discard.go +++ b/pkg/sql/sem/tree/discard.go @@ -23,6 +23,9 @@ type DiscardMode int const ( // DiscardModeAll represents a DISCARD ALL statement. DiscardModeAll DiscardMode = iota + + // DiscardModeSequences represents a DISCARD SEQUENCES statement + DiscardModeSequences ) // Format implements the NodeFormatter interface. @@ -30,6 +33,8 @@ func (node *Discard) Format(ctx *FmtCtx) { switch node.Mode { case DiscardModeAll: ctx.WriteString("DISCARD ALL") + case DiscardModeSequences: + ctx.WriteString("DISCARD SEQUENCES") } }