Skip to content

implement if [not] exists logic for DDL around views and indexes #3006

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

Merged
merged 9 commits into from
Jun 5, 2025

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Jun 2, 2025

This PR adds support for queries:

  • create view if not exists ...
  • create index if not exists ...
  • alter table ... add index if not exists ...
  • drop index if exists ...

fixes: dolthub/dolt#9293
companion pr: dolthub/vitess#417

@jycor jycor mentioned this pull request Jun 4, 2025
Copy link
Contributor

@fulghum fulghum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I think we should match MySQL 9.1's syntax for CREATE IF NOT EXISTS VIEW ... (even though I slightly prefer the current syntax) just for compatibility sake. Then we should decide if we want CREATE INDEX to be consistent with CREATE TABLE IF NOT EXISTS or CREATE IF NOT EXISTS VIEW.

},
Assertions: []ScriptTestAssertion{
{
Query: "create view if not exists v as select 2;",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't notice this while looking at the parser updates... it looks like we are slightly off of MySQL's syntax here... it seems like support for IF NOT EXISTS was added very recently, in MySQL 9.1. The docs show that that the form should be CREATE IF NOT EXISTS VIEW v AS SELECT 2;:
https://dev.mysql.com/doc/refman/9.1/en/create-view.html

I haven't tested this with MySQL 9.x, so it could be that the documentation is incorrect. I know that syntax is a little inconsistent with CREATE TABLE IF NOT EXISTS, too.

ExpectedErr: sql.ErrDuplicateKey,
},
{
Query: "create index if not exists idx on t(j)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure MySQL actually supports this syntax, based on their docs:
https://dev.mysql.com/doc/refman/9.3/en/create-index.html

I tested with MySQL 8.4, but didn't try with MySQL 9.x.

That said, I think it's still useful and I'm not opposed to supporting it, although it will likely be hard for customers to discover if it isn't in the MySQL docs. We should just figure out if we want to be consistent with CREATE TABLE IF NOT EXISTS or CREATE IF NOT EXISTS VIEW, which unfortunately seems to be inconsistent in MySQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I noticed this too.
MariaDB has been doing create view IF NOT EXISTS, which is consistent with all the other CREATE ... IF NOT EXISTS queries... it might be possible to do both?
So CREATE [IF NOT EXISTS] VIEW [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.

I downloaded MySQL 9.3 and tested it out; their docs are just messed up.

mysql> select version();
+-----------+
| version() |
+-----------+
| 9.3.0     |
+-----------+
1 row in set (0.0003 sec)

mysql> create if not exists view v as select 1;
ERROR: 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if not exists view v as select 1' at line 1

mysql> create view if not exists v as select 1;
Query OK, 0 rows affected (0.0044 sec)

mysql> select * from v;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.0009 sec)

@jycor jycor changed the title implement if not exists logic for create view queries implement if [not] exists logic for DDL around views and indexes Jun 5, 2025
@jycor jycor merged commit fd7fc07 into main Jun 5, 2025
10 of 16 checks passed
@jycor jycor deleted the james/ifnotexists branch June 5, 2025 20:02
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.

IF (NOT) EXISTS does not work for some CREATE ...- and DROP-Statements
2 participants