-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathsequences
158 lines (111 loc) · 3.29 KB
/
sequences
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# LogicTest: default distsql parallel-stmts
# SEQUENCE CREATION
statement ok
CREATE SEQUENCE foo
# A sequence with the same name can't be created again.
statement error pgcode 42P07 relation "foo" already exists
CREATE SEQUENCE foo
statement ok
CREATE SEQUENCE IF NOT EXISTS foo
statement error pgcode 42601 conflicting or redundant options
CREATE SEQUENCE bar INCREMENT 5 MAXVALUE 1000 INCREMENT 2
# Sequences are in the same namespace as tables.
statement error pgcode 42P07 relation "foo" already exists
CREATE TABLE foo (k BYTES PRIMARY KEY, v BYTES)
# You can't create with 0 increment.
statement error pgcode 22023 INCREMENT must not be zero
CREATE SEQUENCE zero_test INCREMENT 0
# Test unimplemented syntax.
statement error pq: unimplemented at or near "EOF"
CREATE SEQUENCE err_test AS INT2
statement error pq: unimplemented at or near "EOF"
CREATE SEQUENCE err_test OWNED BY someuser
# DML & DDL ERRORS
statement error pgcode 42809 cannot run INSERT on sequence "foo" - sequences are not updateable
INSERT INTO foo VALUES (1, 2, 3)
statement error pgcode 42809 cannot run UPDATE on sequence "foo" - sequences are not updateable
UPDATE foo SET value = 5
statement error pgcode 42809 cannot run DELETE on sequence "foo" - sequences are not updateable
DELETE FROM foo
statement error pgcode 42809 cannot run TRUNCATE on sequence "foo" - sequences are not updateable
TRUNCATE foo
# Drop table on sequences doesn't work; you have to use DROP SEQUENCE.
statement error pgcode 42809 "foo" is not a table
DROP TABLE foo
# USING THE nextval() FUNCTION
# `select * from foo` currently returns 0 columns & 0 rows;
# testing against that is a Test-Script syntax error.
query I
SELECT nextval('foo')
----
1
query I
SELECT nextval('foo')
----
2
# You can create a sequence with different increment.
statement ok
CREATE SEQUENCE bar INCREMENT 5
query I
SELECT nextval('bar')
----
1
query I
SELECT nextval('bar')
----
6
# You can create a sequence with different start and increment.
statement ok
CREATE SEQUENCE baz START 2 INCREMENT 5
query I
SELECT nextval('baz')
----
2
query I
SELECT nextval('baz')
----
7
# You can create and use a sequence with special characters.
statement ok
CREATE SEQUENCE spécial
query I
SELECT nextval('spécial')
----
1
# You can't call nextval on a table.
statement ok
CREATE TABLE kv (k bytes primary key, v bytes)
statement error pgcode 42809 "kv" is not a sequence
SELECT nextval('kv')
# Parse errors in the argument to nextval are handled.
statement error pq: nextval\(\): syntax error at or near "@"
SELECT nextval('@#%@!324234')
# You can create and find sequences from other databases.
statement ok
CREATE DATABASE other_db
statement ok
USE other_db
statement ok
CREATE SEQUENCE other_db_test
statement ok
USE test
query I
SELECT nextval('other_db.other_db_test')
----
1
# USE WITH TABLES
# You can use a sequence in a DEFAULT expression to create an auto-incrementing primary key.
statement ok
CREATE SEQUENCE blog_posts_id_seq
statement ok
CREATE TABLE blog_posts (id INT PRIMARY KEY DEFAULT nextval('blog_posts_id_seq'), title text)
statement ok
INSERT INTO blog_posts (title) values ('foo')
statement ok
INSERT INTO blog_posts (title) values ('bar')
# TODO(vilterp) change this to [1, 2] once initial-evaluation bug has been fixed
query I
SELECT id FROM blog_posts ORDER BY id
----
2
3