-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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: basic sequences functionality #19664
Conversation
pkg/sql/parser/eval.go
Outdated
@@ -1846,6 +1846,8 @@ type EvalPlanner interface { | |||
// QualifyWithDatabase resolves a possibly unqualified table name into a | |||
// normalized table name that is qualified by database. | |||
QualifyWithDatabase(ctx context.Context, t *NormalizableTableName) (*TableName, error) | |||
|
|||
IncrementSequence(context context.Context, seqName string) (int64, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very specific method to have in this interface. Since this is the first builtin function which makes KV writes, not sure what the best way is to plumb things through.
6cf72bd
to
32cc9c2
Compare
Is anything going to need to change in Review status: 0 of 27 files reviewed at latest revision, 15 unresolved discussions, some commit checks failed. pkg/sql/create.go, line 1333 at r7 (raw file):
nit: these can be inlined in the struct literal I think? pkg/sql/create.go, line 1334 at r7 (raw file):
just to be clear, this will be a no-op (since pkg/sql/create.go, line 1343 at r7 (raw file):
nit: don't need pkg/sql/create.go, line 1831 at r7 (raw file):
nit: capitalize and period pkg/sql/create.go, line 1838 at r7 (raw file):
someone else will have to help you with this one :D pkg/sql/event_log.go, line 59 at r7 (raw file):
Is this meant to be commented out? pkg/sql/logictest/testdata/logic_test/sequences, line 3 at r7 (raw file):
These tests are great - could you also add some integration tests? For Java at the minimum, since JDBC tends to do some wackier stuff than other drivers. I can help you with this if you want, check out pkg/sql/logictest/testdata/logic_test/sequences, line 8 at r7 (raw file):
nit: comments are generally capitalized and have a period (this might be less strictly enforced in logic tests but I think it's a good thing to do) pkg/sql/logictest/testdata/logic_test/sequences, line 18 at r7 (raw file):
We don't do so in many existing logic tests, but I think using pkg/sql/parser/create.go, line 857 at r7 (raw file):
nit: we use pkg/sql/parser/create.go, line 863 at r7 (raw file):
I'm not sure these all need to be their own structs, seems like we could maybe get by with just a pkg/sql/parser/sql.y, line 215 at r7 (raw file):
Reviewable is showing me some kind of red arrow here that I'm not sure about the implication of, this might have tabs instead of spaces (sql.y is spaces for some reason)? err, mostly, there are two lines in it with tabs instead of spaces D: we should fix those pkg/sql/parser/sql.y, line 709 at r7 (raw file):
Postgres calls this If not, is there a reason we need a new nonterminal rather than reusing pkg/sql/parser/sql.y, line 3125 at r7 (raw file):
nit: tabs instead of spaces, we should really lint sql.y Comments from Reviewable |
98aaaea
to
175f0fc
Compare
175f0fc
to
c5dc443
Compare
I'm going to add stuff to Reviewed 5 of 24 files at r1, 2 of 17 files at r4, 20 of 20 files at r8, 1 of 11 files at r11, 2 of 5 files at r14, 1 of 1 files at r15. pkg/sql/create.go, line 1333 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/create.go, line 1334 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. Leaving it in to be explicit (but in a struct literal) pkg/sql/create.go, line 1343 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/create.go, line 1831 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/create.go, line 1838 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. (Realized I could just use pkg/sql/event_log.go, line 59 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. Will add that in a later PR when I do the DROP command pkg/sql/logictest/testdata/logic_test/sequences, line 3 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 8 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 18 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/parser/create.go, line 857 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/parser/create.go, line 863 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/parser/sql.y, line 215 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/parser/sql.y, line 3125 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. Comments from Reviewable |
but @knz should probably take a look as well. Review status: 14 of 28 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed. pkg/sql/planner.go, line 269 at r15 (raw file):
nit i missed before: you could just return this directly pkg/sql/parser/create.go, line 863 at r7 (raw file): Previously, vilterp (Pete Vilter) wrote…
Sorry if I was unclear, I meant that we could have a single struct that gets threaded through like
etc. but on second thought that might be more difficult than I'm imagining with the grammar, maybe check what another statement does here, it might just be what you had before? Sorry if that's the case! Comments from Reviewable |
Actually, would you mind including the Java test in this PR as well (and then the README change isn't so out-of-place :D)? Review status: 14 of 28 files reviewed at latest revision, 4 unresolved discussions, some commit checks failed. Comments from Reviewable |
c5dc443
to
7762037
Compare
Reviewed 2 of 12 files at r9, 4 of 11 files at r11, 1 of 4 files at r13, 7 of 8 files at r16, 3 of 3 files at r17. pkg/sql/planner.go, line 269 at r15 (raw file): Previously, justinj (Justin Jaffray) wrote…
Done. pkg/sql/parser/create.go, line 863 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
The current implementation ( pkg/sql/parser/sql.y, line 709 at r7 (raw file): Previously, justinj (Justin Jaffray) wrote…
Renamed it to pkg/sql/sqlbase/structured.go, line 325 at r17 (raw file):
Wasn't sure what to call this, so I said Comments from Reviewable |
7762037
to
1f95445
Compare
I'm mostly good with this too. Reviewed 2 of 24 files at r1, 13 of 20 files at r18, 5 of 11 files at r21, 1 of 4 files at r23, 2 of 5 files at r24, 4 of 7 files at r25, 3 of 3 files at r26, 5 of 5 files at r27, 2 of 2 files at r28. pkg/sql/planner.go, line 256 at r18 (raw file):
pkg/sql/planner.go, line 258 at r18 (raw file):
call pkg/sql/planner.go, line 262 at r18 (raw file):
That doesn't seem right. As per the pg doc (https://www.postgresql.org/docs/10/static/functions-sequence.html), the sequence name is determined using the search path. pkg/sql/logictest/testdata/logic_test/sequences, line 84 at r28 (raw file):
Add tests that check that sequences can be found in other databases via the search path. pkg/sql/parser/builtins.go, line 1148 at r28 (raw file):
as I explain in a previous comment, the translation from string to TableName must occur here, not in IncrementSequence. pkg/sql/parser/parse_test.go, line 224 at r28 (raw file):
Some pkg/sql/parser/sql.y, line 709 at r7 (raw file): Previously, vilterp (Pete Vilter) wrote…
I don't understand the question; what is the problem to be solved here? Also I'd use Comments from Reviewable |
Reviewed 4 of 24 files at r1, 2 of 17 files at r4, 3 of 20 files at r8, 1 of 20 files at r18, 2 of 12 files at r19, 4 of 11 files at r21, 1 of 4 files at r23, 1 of 5 files at r24, 4 of 7 files at r25, 3 of 3 files at r26, 5 of 5 files at r27, 2 of 2 files at r28. pkg/keys/keys.go, line 620 at r18 (raw file): Previously, petermattis (Peter Mattis) wrote…
pkg/sql/create.go, line 1389 at r28 (raw file):
Should we be throwing errors for the options that are not yet supported? pkg/sql/logictest/testdata/logic_test/sequences, line 1 at r28 (raw file):
Why only the pkg/sql/parser/sql.y, line 3149 at r28 (raw file):
pkg/sql/sqlbase/structured.go, line 326 at r28 (raw file):
nit: I'd do something like:
pkg/sql/sqlbase/structured.go, line 336 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
+1 pkg/sql/sqlbase/structured.proto, line 611 at r28 (raw file):
These fields should be commented. I'd also push to rename this to Comments from Reviewable |
4f45213
to
1d59a5d
Compare
Reviewed 20 of 20 files at r18, 1 of 12 files at r19, 1 of 5 files at r27, 30 of 30 files at r29. pkg/keys/keys.go, line 620 at r18 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/keys/keys.go, line 619 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/create.go, line 1389 at r28 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. The parser now throws errors for the options we don't support pkg/sql/planner.go, line 256 at r18 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/planner.go, line 258 at r18 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/planner.go, line 262 at r18 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/planner.go, line 266 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/update.go, line 56 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 1 at r28 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 29 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 44 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. Changed this to return an error until we actually support it. pkg/sql/logictest/testdata/logic_test/sequences, line 84 at r28 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 1148 at r28 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/eval.go, line 1850 at r3 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/parser/parse_test.go, line 224 at r28 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/sql.y, line 709 at r7 (raw file): Previously, knz (kena) wrote…
The problem is that it seems weird to have the suffix I can't seem to change it to plain pkg/sql/parser/sql.y, line 3149 at r28 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/parser/sql.y, line 3174 at r28 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Not quite sure how to add tests for an individual rule… I see some tests for syntax errors, but they all seem to be parsing statements from the top level. pkg/sql/sqlbase/structured.go, line 325 at r17 (raw file): Previously, jordanlewis (Jordan Lewis) wrote…
Done. pkg/sql/sqlbase/structured.go, line 326 at r28 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
|
1d59a5d
to
2dc4cb2
Compare
Reviewed 9 of 24 files at r30, 14 of 15 files at r31, 4 of 5 files at r32, 7 of 7 files at r33, 3 of 3 files at r34. pkg/sql/planner.go, line 262 at r18 (raw file): Previously, vilterp (Pete Vilter) wrote…
See my comment on pkg/sql/planner.go, line 269 at r33 (raw file):
Given how you've modified the code here, the comment must be extended with: pkg/sql/logictest/testdata/logic_test/sequences, line 124 at r34 (raw file):
We don't typically use USE in logic tests -- SET DATABASE instead. Consistency is desirable here as the editor of a test will often grep for SET DATABASE to determine what to expect. pkg/sql/logictest/testdata/logic_test/sequences, line 133 at r34 (raw file):
This is OK but insufficient. This must work:
pkg/sql/parser/builtins.go, line 1148 at r28 (raw file): Previously, vilterp (Pete Vilter) wrote…
I don't think so -- pkg/sql/parser/sql.y, line 709 at r7 (raw file): Previously, vilterp (Pete Vilter) wrote…
Oh I know what happened. I had forgotten something :)
pkg/sql/parser/sql.y, line 3174 at r28 (raw file): Previously, vilterp (Pete Vilter) wrote…
I had to look again at the entire patch to grok what's going on here. :) Now since this is what it does really, what you currently call This is a good rule to have indeed! And documented as such:
Then:
Comments from Reviewable |
2dc4cb2
to
1234358
Compare
Reviewed 24 of 24 files at r30, 37 of 37 files at r35. pkg/sql/planner.go, line 269 at r33 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 124 at r34 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/logictest/testdata/logic_test/sequences, line 133 at r34 (raw file): Previously, knz (kena) wrote…
Done. pkg/sql/parser/builtins.go, line 1148 at r28 (raw file): Previously, knz (kena) wrote…
pkg/sql/parser/sql.y, line 709 at r7 (raw file): Previously, knz (kena) wrote…
Done. Thanks! pkg/sql/parser/sql.y, line 3174 at r28 (raw file): Previously, knz (kena) wrote…
Done. Thanks for your direction; I suspected a refactor like this was a good idea! Put the introduction of Comments from Reviewable |
Reviewed 10 of 10 files at r36, 14 of 15 files at r37, 4 of 5 files at r38, 7 of 7 files at r39, 3 of 3 files at r40. pkg/sql/parser/builtins.go, line 1148 at r28 (raw file): Previously, vilterp (Pete Vilter) wrote…
Oh interesting. TIL. Thanks! pkg/sql/parser/eval.go, line 2004 at r39 (raw file):
Extend the comment to explain that QualifyWithDatabase also uses the search path. :) Comments from Reviewable |
1234358
to
05426eb
Compare
SELECT id FROM blog_posts ORDER BY id | ||
---- | ||
2 | ||
3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious for some advice from you, @knz:
In the above test, we create a new sequence and then create a table which uses it in a DEFAULT expression. However, when we insert the first row into the table, the primary key comes out as 2, not 1 (where the sequence is supposed to start). This is because default expressions are run once at table creation time to see if they work:
cockroach/pkg/sql/sqlbase/table.go
Lines 198 to 205 in d73de92
// Try to evaluate once. If it is aimed to succeed during a | |
// backfill, it must succeed here too. This tries to ensure that | |
// we don't end up failing the evaluation during the schema change | |
// proper. | |
if _, err := typedExpr.Eval(evalCtx); err != nil { | |
return nil, nil, err | |
} | |
d.DefaultExpr.Expr = typedExpr |
This increments the sequence before a row is inserted into the table, which we don't want.
Would it be reasonable to flag functions which have side effects (e.g. writing to the database, as in this case), and exclude them from being run here? (Note also that we can't run it in a transaction and roll it back, because the nextval
function operates in its own transaction)
If the solution is introduce a new flag on Builtin
and use it to mark nextval
as side effectful, maybe using the same terminology as postgres's volatility categories (https://www.postgresql.org/docs/8.2/static/xfunc-volatility.html) for these flags would be appropriate.
Also FWIW, I think this could be fixed in another PR if all else is good to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we do already have an impure
flag on builtins and it does already propagate to those volatility categories, FWIW.
05426eb
to
41ebb7c
Compare
We shouldn't be evaluating expressions "just to see if they work". That's just wrong thinking. I'm OK with removing that evaluation. If for any reason a user discovers a bug that makes a default expression invalid, they can use ALTER to change it.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
@knz I agree; happy to remove it. Asked you because you added the check. Is there some case we're forgetting? The logic tests still pass with it gone. Anyway, I'm going to make that a separate PR; we can discuss there. |
41ebb7c
to
4f0da30
Compare
I didn't add it -- I merely moved it from where it was (once per backfill processor) to another place (once for the entire backfill). It shouldn't have been there in the first place. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
pkg/sql/parser/sql.y
Outdated
@@ -6563,6 +6527,28 @@ signed_iconst: | |||
$$.val = &tree.NumVal{Value: constant.UnaryOp(token.SUB, $2.numVal().Value, 0)} | |||
} | |||
|
|||
// signed_iconst64 is a variant of signed_iconst which only accepts (signed) integer literals that fit in an int64. | |||
signed_iconst64: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i didn't totally follow the discussion around this one but is it intentional that this rule isn't used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment with more explanation. It's used in a later commit than the one that introduces it.
pkg/sql/sqlbase/structured.proto
Outdated
// Start value of the sequence. | ||
optional int64 start = 4 [(gogoproto.nullable) = false]; | ||
// How many values to pre-allocate and cache in memory (not currently supported) | ||
optional int64 cache = 5 [(gogoproto.nullable) = false]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not planning to support the cache option anytime soon we probably don't need to add this to the proto, especially not as a non-nullable field. An extra int64 per descriptor obviously won't kill us, though. And since descriptors are cached this might just not be worth worrying about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Removed it.
SELECT id FROM blog_posts ORDER BY id | ||
---- | ||
2 | ||
3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we do already have an impure
flag on builtins and it does already propagate to those volatility categories, FWIW.
…uence_opts Creating a table descriptor also creates a range in which the sequence value lives; a single key (/Table/<id>/seqval) is then initialized in that range.
INSERT, UPDATE, DELETE, and TRUNCATE on sequences are not allowed.
4f0da30
to
36d6696
Compare
Implement
CREATE SEQUENCE
and thenextval
function, as the first steps of adding sequences support.The
CREATE SEQUENCE
statement:sequence_settings
field set. The presence of thesequence_settings
field on a descriptor signifies that it represents a sequence, not a table or a view./Table/<id>/SequenceValue
as a bare int64.This PR leaves off the
AS
andOWNED BY
options of theCREATE TABLE
statement, to be revisited in later PRs which handle bounds checks and the column <=> sequence dependency relationship, respectively.The
nextval
function looks up the descriptor by name, calls theInc
KV operation on the key, and returns the result.RFC: #19196
Tracking issue: #19723 (shows future work)