Skip to content

Commit

Permalink
Fix(tsql): create if not exist index
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 26, 2023
1 parent 178b37f commit fe37fc7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
5 changes: 4 additions & 1 deletion sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,11 @@ def create_sql(self, expression: exp.Create) -> str:
identifier = self.sql(exp.Literal.string(exp.table_name(table) if table else ""))
if kind == "SCHEMA":
sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = {identifier}) EXEC('{sql}')"""
if kind == "TABLE":
elif kind == "TABLE":
sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name = {identifier}) EXEC('{sql}')"""
elif kind == "INDEX":
index = self.sql(exp.Literal.string(expression.this.text("this")))
sql = f"""IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id({identifier}) AND name = {index}) EXEC('{sql}')"""
elif expression.args.get("replace"):
sql = sql.replace("CREATE OR REPLACE ", "CREATE OR ALTER ", 1)

Expand Down
4 changes: 2 additions & 2 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,9 @@ def index_sql(self, expression: exp.Index) -> str:
name = self.sql(expression, "this")
name = f"{name} " if name else ""
table = self.sql(expression, "table")
table = f"{self.INDEX_ON} {table} " if table else ""
table = f"{self.INDEX_ON} {table}" if table else ""
using = self.sql(expression, "using")
using = f"USING {using} " if using else ""
using = f" USING {using} " if using else ""
index = "INDEX " if not table else ""
columns = self.expressions(expression, key="columns", flat=True)
columns = f"({columns})" if columns else ""
Expand Down
24 changes: 12 additions & 12 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,27 +1465,27 @@ def test_limit(self):
},
)
self.validate_all(
"CREATE INDEX my_idx ON tbl (a, b)",
"CREATE INDEX my_idx ON tbl(a, b)",
read={
"hive": "CREATE INDEX my_idx ON TABLE tbl (a, b)",
"sqlite": "CREATE INDEX my_idx ON tbl (a, b)",
"hive": "CREATE INDEX my_idx ON TABLE tbl(a, b)",
"sqlite": "CREATE INDEX my_idx ON tbl(a, b)",
},
write={
"hive": "CREATE INDEX my_idx ON TABLE tbl (a, b)",
"postgres": "CREATE INDEX my_idx ON tbl (a NULLS FIRST, b NULLS FIRST)",
"sqlite": "CREATE INDEX my_idx ON tbl (a, b)",
"hive": "CREATE INDEX my_idx ON TABLE tbl(a, b)",
"postgres": "CREATE INDEX my_idx ON tbl(a NULLS FIRST, b NULLS FIRST)",
"sqlite": "CREATE INDEX my_idx ON tbl(a, b)",
},
)
self.validate_all(
"CREATE UNIQUE INDEX my_idx ON tbl (a, b)",
"CREATE UNIQUE INDEX my_idx ON tbl(a, b)",
read={
"hive": "CREATE UNIQUE INDEX my_idx ON TABLE tbl (a, b)",
"sqlite": "CREATE UNIQUE INDEX my_idx ON tbl (a, b)",
"hive": "CREATE UNIQUE INDEX my_idx ON TABLE tbl(a, b)",
"sqlite": "CREATE UNIQUE INDEX my_idx ON tbl(a, b)",
},
write={
"hive": "CREATE UNIQUE INDEX my_idx ON TABLE tbl (a, b)",
"postgres": "CREATE UNIQUE INDEX my_idx ON tbl (a NULLS FIRST, b NULLS FIRST)",
"sqlite": "CREATE UNIQUE INDEX my_idx ON tbl (a, b)",
"hive": "CREATE UNIQUE INDEX my_idx ON TABLE tbl(a, b)",
"postgres": "CREATE UNIQUE INDEX my_idx ON tbl(a NULLS FIRST, b NULLS FIRST)",
"sqlite": "CREATE UNIQUE INDEX my_idx ON tbl(a, b)",
},
)
self.validate_all(
Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,13 @@ def test_types_bin(self):
)

def test_ddl(self):
self.validate_all(
"IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id('db.tbl') AND name = 'idx') EXEC('CREATE INDEX idx ON db.tbl')",
read={
"": "CREATE INDEX IF NOT EXISTS idx ON db.tbl",
},
)

self.validate_all(
"IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = 'foo') EXEC('CREATE SCHEMA foo')",
read={
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ CREATE FUNCTION a() LANGUAGE sql
CREATE FUNCTION a() LANGUAGE sql RETURNS INT
CREATE FUNCTION a.b(x INT) RETURNS INT AS RETURN x + 1
CREATE FUNCTION a.b.c()
CREATE INDEX abc ON t (a)
CREATE INDEX "abc" ON t (a)
CREATE INDEX abc ON t (a, b, b)
CREATE INDEX abc ON t (a NULLS LAST)
CREATE INDEX abc ON t(a)
CREATE INDEX "abc" ON t(a)
CREATE INDEX abc ON t(a, b, b)
CREATE INDEX abc ON t(a NULLS LAST)
CREATE INDEX pointloc ON points USING GIST(BOX(location, location))
CREATE UNIQUE INDEX abc ON t (a, b, b)
CREATE UNIQUE INDEX IF NOT EXISTS my_idx ON tbl (a, b)
CREATE UNIQUE INDEX abc ON t(a, b, b)
CREATE UNIQUE INDEX IF NOT EXISTS my_idx ON tbl(a, b)
CREATE SCHEMA x
CREATE SCHEMA IF NOT EXISTS y
CREATE DATABASE x
Expand Down

0 comments on commit fe37fc7

Please sign in to comment.