-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feature/v0.10 sql ddl #1075
Feature/v0.10 sql ddl #1075
Conversation
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.
A few comments but mostly looked great
parse/grammar/KuneiformParser.g4
Outdated
; | ||
|
||
fk_constraint: | ||
REFERENCES table=identifier (LPAREN column=identifier RPAREN) (fk_action (fk_action)*)? |
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.
Probably better to do:
REFERENCES table=identifier (LPAREN column=identifier RPAREN) (fk_action (fk_action)?)?
so that only a max of 2 can be specified
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.
because we only support on DELETE
and on UPDATE
? make sense
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 think that is all postgres supports too.
parse/grammar/KuneiformParser.g4
Outdated
@@ -184,13 +216,58 @@ sql: | |||
|
|||
sql_statement: | |||
(WITH RECURSIVE? common_table_expression (COMMA common_table_expression)*)? | |||
(select_statement | update_statement | insert_statement | delete_statement) | |||
(create_table_statement | alter_table_statement| create_index_statement | drop_index_statement // ddl |
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 seems incorrect. DDL shouldn't have CTEs. For example, I cant have:
WITH my_cte AS (
-- ...
)
CREATE TABLE ...
These should instead be separate clauses
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.
oh I wanted to make a quick pass first then create another statement, totally forget about it. A new ddl_statement
sounds good?
parse/grammar/KuneiformParser.g4
Outdated
; | ||
|
||
create_index_statement: | ||
CREATE UNIQUE? INDEX name=identifier? ON table=identifier LPAREN columns=identifier_list RPAREN |
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.
Can you add if not exists
?
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.
sure, Do we need this for other statements, like alter table add column if not exist xxx
?
I only create the syntax you put in the docs
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.
Yeah that'd be great.
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.
Actually I don't think this is important. As long as we have it for create
that is ok for now. We can add it in a later release
parse/grammar/KuneiformParser.g4
Outdated
; | ||
|
||
common_table_expression: | ||
identifier (LPAREN (identifier (COMMA identifier)*)? RPAREN)? AS LPAREN select_statement RPAREN | ||
; | ||
|
||
create_table_statement: | ||
CREATE TABLE name=identifier |
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.
Can you add if not exists
?
parse/grammar/KuneiformParser.g4
Outdated
PRIMARY KEY LPAREN identifier_list RPAREN | ||
| UNIQUE LPAREN identifier_list RPAREN | ||
| CHECK LPAREN sql_expr RPAREN | ||
| FOREIGN KEY LPAREN identifier RPAREN fk_constraint |
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.
not a huge deal but it might be easier if these are named? e.g.
unnamed_constraint:
PRIMARY KEY LPAREN identifier_list RPAREN #unnamed_constraint_pk
| UNIQUE LPAREN identifier_list RPAREN #unnamed_contract_unique
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.
the syntax of the constraint is simple enough, for now it's done just in one switch, I don't think it's necessary to create different tree nodes on constraints.
To deal with new DDL, I create Also add some basic validations on |
sql: | ||
sql_statement SCOL | ||
// sql is a top-level SQL statement, it maps to SQLStmt interface in AST. | ||
sql_stmt: |
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.
To change sql_statement
to sql_stmt
in action/procedure block will break the parser/analyzer/planner, better do it together when we make the changes
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
(table_column_def | table_constraint_def | table_index_def) | ||
(COMMA (table_column_def | table_constraint_def | table_index_def))* | ||
RPAREN | ||
; |
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 think you are missing drop table
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.
ah shoot... Will add it now
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.
hmm, do you want to add drop table
in our DDL docs? Or do I just use syntax from https://www.postgresql.org/docs/current/sql-droptable.html ?
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.
Yeah we should add drop ddl.
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.
Just base it on postgres's tho
// NOTE: THIS is only temporary so that I can write tests. After we shift from | ||
// *SQLStatement to SQLStmt we can delete this. | ||
func ParseDDL(sql string, schema *types.Schema, skipValidation bool) (res *DDLParseResult, err error) { | ||
parser, errLis, visitor, deferFn, err := setupParser2(sql) |
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.
Why do you need the schema parameter? It is unused.
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.
skipValidation is unused as well
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.
ah didn't event look the function signature, just copied from ParseSQL
, will change.
parse/parse.go
Outdated
ParseErrs ParseErrs | ||
|
||
// Mutative is true if the statement mutates state. | ||
Mutative bool |
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.
All DDL is mutative, so this is redundant
opt_drop_behavior: | ||
CASCADE | ||
| RESTRICT | ||
| |
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.
Inspired by the antlr postgres grammar, I kind of like it
OK Just add |
This pr adds new DDLs: