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

Feature/v0.10 sql ddl #1075

Merged
merged 11 commits into from
Nov 1, 2024
Merged

Feature/v0.10 sql ddl #1075

merged 11 commits into from
Nov 1, 2024

Conversation

Yaiba
Copy link
Contributor

@Yaiba Yaiba commented Oct 25, 2024

This pr adds new DDLs:

  • create table
  • alter table
  • create index
  • drop index

@Yaiba Yaiba requested a review from brennanjl October 25, 2024 19:25
Copy link
Collaborator

@brennanjl brennanjl left a 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

;

fk_constraint:
REFERENCES table=identifier (LPAREN column=identifier RPAREN) (fk_action (fk_action)*)?
Copy link
Collaborator

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

Copy link
Contributor Author

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

Copy link
Collaborator

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.

@@ -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
Copy link
Collaborator

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

Copy link
Contributor Author

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?

;

create_index_statement:
CREATE UNIQUE? INDEX name=identifier? ON table=identifier LPAREN columns=identifier_list RPAREN
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Collaborator

@brennanjl brennanjl Oct 28, 2024

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

;

common_table_expression:
identifier (LPAREN (identifier (COMMA identifier)*)? RPAREN)? AS LPAREN select_statement RPAREN
;

create_table_statement:
CREATE TABLE name=identifier
Copy link
Collaborator

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?

PRIMARY KEY LPAREN identifier_list RPAREN
| UNIQUE LPAREN identifier_list RPAREN
| CHECK LPAREN sql_expr RPAREN
| FOREIGN KEY LPAREN identifier RPAREN fk_constraint
Copy link
Collaborator

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

Copy link
Contributor Author

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.

@Yaiba
Copy link
Contributor Author

Yaiba commented Oct 29, 2024

To deal with new DDL, I create ParseDDL and WriteDDL for now so I don't break the downstream analyzer/planner, also allow me to write tests.

Also add some basic validations on create table

@Yaiba Yaiba requested a review from brennanjl October 29, 2024 18:26
sql:
sql_statement SCOL
// sql is a top-level SQL statement, it maps to SQLStmt interface in AST.
sql_stmt:
Copy link
Contributor Author

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

Copy link
Collaborator

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
;
Copy link
Collaborator

@brennanjl brennanjl Oct 30, 2024

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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 ?

Copy link
Collaborator

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.

Copy link
Collaborator

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)
Copy link
Collaborator

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.

Copy link
Collaborator

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

Copy link
Contributor Author

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
Copy link
Collaborator

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
|
Copy link
Contributor Author

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

@Yaiba
Copy link
Contributor Author

Yaiba commented Oct 30, 2024

OK Just add Drop table statement, also address feedback on ParseDDL function.

@Yaiba Yaiba marked this pull request as ready for review October 30, 2024 20:41
@brennanjl brennanjl merged commit d1c7ec8 into v0.10-sql Nov 1, 2024
@brennanjl brennanjl deleted the feature/v0.10-sql-ddl branch November 1, 2024 19:49
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.

2 participants