Skip to content

Commit

Permalink
Add column constraint support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mytherin committed Nov 21, 2024
1 parent 9cc5563 commit a25c2cd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
36 changes: 35 additions & 1 deletion extension/autocomplete/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ class RepeatMatcher : public Matcher {
// match did not succeed - propagate upwards
return child_match;
}
if (!separator) {
continue;
}
// succeeded in matching the child
// now we optionally match the separator
idx_t successful_token = repeat_state.token_index;
Expand Down Expand Up @@ -282,10 +285,41 @@ unique_ptr<Matcher> QualifiedTableName() {
return Matcher::List(std::move(m));
}

unique_ptr<Matcher> TypeName() {
return Matcher::TypeName();
}

unique_ptr<Matcher> NotNullConstraint() {
vector<unique_ptr<Matcher>> m;
m.push_back(Matcher::Keyword("NOT"));
m.push_back(Matcher::Keyword("NULL", 0, '\0'));
return Matcher::List(std::move(m));
}

unique_ptr<Matcher> UniqueConstraint() {
return Matcher::Keyword("UNIQUE", 0, '\0');
}

unique_ptr<Matcher> PrimaryKeyConstraint() {
vector<unique_ptr<Matcher>> m;
m.push_back(Matcher::Keyword("PRIMARY"));
m.push_back(Matcher::Keyword("KEY", 0, '\0'));
return Matcher::List(std::move(m));
}

unique_ptr<Matcher> ColumnConstraint() {
vector<unique_ptr<Matcher>> m;
m.push_back(NotNullConstraint());
m.push_back(UniqueConstraint());
m.push_back(PrimaryKeyConstraint());
return Matcher::Optional(Matcher::Repeat(Matcher::Choice(std::move(m)), nullptr));
}

unique_ptr<Matcher> CreateTableColumnList() {
vector<unique_ptr<Matcher>> m;
m.push_back(Matcher::Variable());
m.push_back(Matcher::TypeName());
m.push_back(TypeName());
m.push_back(ColumnConstraint());
return Matcher::Repeat(Matcher::List(std::move(m)), Matcher::Keyword(","));
}

Expand Down
33 changes: 25 additions & 8 deletions test/sql/function/table/sql_auto_complete.test
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,32 @@ FROM sql_auto_complete('create table tbl(i INTEGER, j INTE') LIMIT 1;
----
INTEGER 30

## suggest a constraint
#query II
#FROM sql_auto_complete('create table tbl(i INTEGER PRI') LIMIT 1;
#----
# suggest a constraint
query II
FROM sql_auto_complete('create table tbl(i INTEGER PRI') LIMIT 1;
----
PRIMARY 27

query II
FROM sql_auto_complete('create table tbl(i INTEGER PRIMARY KE') LIMIT 1;
----
KEY 35

query II
FROM sql_auto_complete('create table tbl(i INTEGER UNIQ') LIMIT 1;
----
UNIQUE 27

query II
FROM sql_auto_complete('create table tbl(i INTEGER UNIQUE NO') LIMIT 1;
----
NOT 34

query II
FROM sql_auto_complete('create table tbl(i INTEGER UNIQUE NOT N') LIMIT 1;
----
NULL 38

#query II
#FROM sql_auto_complete('create table tbl(i INTEGER PRIMARY KE') LIMIT 1;
#----
#
## suggest a stand-alone constraint
#query II
#FROM sql_auto_complete('create table tbl(i INTEGER, PRI') LIMIT 1;
Expand Down

0 comments on commit a25c2cd

Please sign in to comment.