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

Support AUTO_INCREMENT. #250

Closed
wants to merge 2 commits into from
Closed

Support AUTO_INCREMENT. #250

wants to merge 2 commits into from

Conversation

tzzed
Copy link
Contributor

@tzzed tzzed commented Oct 10, 2020

This a proposal draft for #43.

SQL Server seems good because it proposes a customizable AUTO_INCREMENT with a "natural language".

### Default value
CREATE TABLE foo (id INTEGER AUTO_INCREMENT);
INSERT INTO foo VALUES {"a": "foo"};
SELECT * FROM foo;
{ "a": "foo", "id": 1}

### Set a start index and an increment value;
###`AUTO_INCREMENT(startIndex, incBy)`
###The first value of the sequence start at 10 and the next is incremented by 5
CREATE TABLE bar (id INTEGER AUTO_INCREMENT(10, 5));
INSERT INTO bar VALUES {"a": "bar"};
INSERT INTO bar VALUES {"a": "baz"};
SELECT * FROM bar;
{ "a": "bar", "id": 10 }
{  "a": "baz", "id": 15 }

AUTO_INCREMENT have to be applied only on number value type. INTEGER and DOUBLE

genji> CREATE TABLE foo(bar TEXT AUTO_INCREMENT);
genji> found text, expected integer, double at line 1, char 27

About ALTER TABLE table_name AUTO_INCREMENT=100, If we keep it like that, we should be able to write the both following syntaxes:

###For default value
ALTER TABLE foo AUTO_INCREMENT=100;

### And this even if the creation was with default value
ALTER TABLE foo AUTO_INCREMENT(100, 10);

Thank you for your feedbacks.

@codecov-io
Copy link

codecov-io commented Oct 10, 2020

Codecov Report

Merging #250 into master will decrease coverage by 0.23%.
The diff coverage is 43.37%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #250      +/-   ##
==========================================
- Coverage   61.32%   61.09%   -0.24%     
==========================================
  Files          68       68              
  Lines        6345     6428      +83     
==========================================
+ Hits         3891     3927      +36     
- Misses       1938     1969      +31     
- Partials      516      532      +16     
Impacted Files Coverage Δ
sql/scanner/token.go 22.22% <ø> (ø)
database/table.go 54.54% <20.00%> (-2.71%) ⬇️
sql/parser/create.go 63.70% <45.16%> (-5.53%) ⬇️
database/config.go 67.31% <56.25%> (-1.28%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 375ed7a...5528622. Read the comment docs.

@tzzed tzzed marked this pull request as ready for review October 10, 2020 22:52
@tzzed tzzed marked this pull request as draft October 10, 2020 22:56
@tzzed tzzed force-pushed the autoInc branch 2 times, most recently from 1264d41 to 8096c83 Compare October 16, 2020 17:49
Add FieldBuffer #Copy.
Fix comments.
@tzzed tzzed marked this pull request as ready for review October 16, 2020 18:18
@tie
Copy link
Contributor

tie commented Oct 25, 2020

@tzzed thanks for your work! What’s the current status of this PR? It looks like the counter is not being persisted.

Step 1
tie@xhyve ~/genji/cmd/genji $ go run . bolt.db
On-disk database using BoltDB engine at path bolt.db.
Enter ".help" for usage hints.
genji> CREATE TABLE foo (id INTEGER AUTO_INCREMENT);
genji> INSERT INTO foo VALUES {"a": "foo"};
genji> SELECT * FROM foo;
{
  "a": "foo",
  "id": 1
}
genji> .exit
Step 2
tie@xhyve ~/genji/cmd/genji $ go run . bolt.db
On-disk database using BoltDB engine at path bolt.db.
Enter ".help" for usage hints.
genji> SELECT * FROM foo;
{
  "a": "foo",
  "id": 1
}
genji> INSERT INTO foo VALUES {"a": "foo"};
genji> SELECT * FROM foo;
{
  "a": "foo",
  "id": 1
}
{
  "a": "foo",
  "id": 1
}
genji> INSERT INTO foo VALUES {"a": "foo"};
genji> SELECT * FROM foo;
{
  "a": "foo",
  "id": 1
}
{
  "a": "foo",
  "id": 1
}
{
  "a": "foo",
  "id": 2
}
genji> .exit

@tzzed
Copy link
Contributor Author

tzzed commented Oct 25, 2020

It was draft. I just submitted the idea, there is still some work.
What do you think about the proposition?

@tie
Copy link
Contributor

tie commented Oct 25, 2020

Looks nice! I’d also suggest using AUTOINCREMENT as a keyword (underscore feels weird).

Though I’m not quite sure what we actually want from the auto increment feature. E.g. in SQLite it guarantees monotonically increasing 64-bit integer sequence for rowid (pk() in Genji) with an error on overflow. Also, I think it’s possible to compute non-primary key auto incremented value with (start, step) parameters given a document sequence number (that is strictly monotonically increasing, i.e. we’ll get an error on overflow).

See also

@tie
Copy link
Contributor

tie commented Oct 25, 2020

Note that we currently don’t handle integer overflows for sequence numbers.

@tie
Copy link
Contributor

tie commented Oct 25, 2020

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
https://sqlite.org/autoinc.html

Rename AUTO_INCREMENT to AUTOINCREMENT.
Remove updatefielconstraints method.
@tzzed
Copy link
Contributor Author

tzzed commented Oct 26, 2020

Thank you @tie ! In view of the documentation, this PR is more complex than I though. @asdine can you revert it. We should create a new table and more...
Thanks

@tzzed tzzed closed this Nov 3, 2020
@tzzed tzzed deleted the autoInc branch November 3, 2020 21:43
@mrusme
Copy link

mrusme commented Jan 9, 2021

Trying to understand whether auto incrementing primary keys work, even though this PR wasn't accepted. The documentation says:

This will create a table teams that can hold any document. An auto-incrementing primary key will be generated every time a document is inserted.

However, in a quick test locally this doesn't seem the case - at least not with only a CREATE TABLE mytable; schema. Do auto incrementing ID fields require a full schema specification?

@asdine
Copy link
Collaborator

asdine commented Jan 9, 2021

@mrusme For now, only auto generated "docids" are generated. These are internal IDs, equivalent to SQLite's rowids.
You can select them using SELECT pk() FROM foo; but like rowids you have no guarantee that they will always be the same.

Regarding auto incremented user defined primary keys, they are not supported yet.
I suggest using uuids or equivalent for now, until the feature is implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants