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 DISCARD SEQUENCES #86230

Merged
merged 1 commit into from
Aug 25, 2022
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
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/discard_stmt.bnf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
discard_stmt ::=
'DISCARD' 'ALL'
| 'DISCARD' 'SEQUENCES'
1 change: 1 addition & 0 deletions docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/discard
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`, ``},

Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/parser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/sem/tree/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ 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.
func (node *Discard) Format(ctx *FmtCtx) {
switch node.Mode {
case DiscardModeAll:
ctx.WriteString("DISCARD ALL")
case DiscardModeSequences:
ctx.WriteString("DISCARD SEQUENCES")
}
}

Expand Down