Skip to content

Commit

Permalink
Like (#760)
Browse files Browse the repository at this point in the history
* added ability to parse LIKE in CREATE TABLE statement

* fixed formatting

* had to change formatting again

* added EXCLUDING as well

* refactor including/excluding

Co-authored-by: Yuchao <fanyuchao01@gmail.com>
  • Loading branch information
tobymao and yuchaofan13 committed Nov 25, 2022
1 parent 1f9a4f7 commit 4851ce1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class Generator(generator.Generator):
exp.CharacterSetProperty,
exp.CollateProperty,
exp.SchemaCommentProperty,
exp.LikeProperty,
}

WITH_PROPERTIES: t.Set[t.Type[exp.Property]] = set()
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,10 @@ class DistStyleProperty(Property):
arg_types = {"this": True}


class LikeProperty(Property):
arg_types = {"this": True, "expressions": False}


class LocationProperty(Property):
arg_types = {"this": True}

Expand Down
6 changes: 6 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Generator:
exp.DistStyleProperty,
exp.DistKeyProperty,
exp.SortKeyProperty,
exp.LikeProperty,
}

WITH_PROPERTIES = {
Expand Down Expand Up @@ -568,6 +569,11 @@ def property_sql(self, expression):

return f"{property_name}={self.sql(expression, 'this')}"

def likeproperty_sql(self, expression):
options = " ".join(f"{e.name} {self.sql(e, 'value')}" for e in expression.expressions)
options = f" {options}" if options else ""
return f"LIKE {self.sql(expression, 'this')}{options}"

def insert_sql(self, expression):
overwrite = expression.args.get("overwrite")

Expand Down
15 changes: 15 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ class Parser(metaclass=_Parser):
TokenType.DISTKEY: lambda self: self._parse_distkey(),
TokenType.DISTSTYLE: lambda self: self._parse_property_assignment(exp.DistStyleProperty),
TokenType.SORTKEY: lambda self: self._parse_sortkey(),
TokenType.LIKE: lambda self: self._parse_create_like(),
TokenType.RETURNS: lambda self: self._parse_returns(),
TokenType.COLLATE: lambda self: self._parse_property_assignment(exp.CollateProperty),
TokenType.COMMENT: lambda self: self._parse_property_assignment(exp.SchemaCommentProperty),
Expand Down Expand Up @@ -500,6 +501,7 @@ class Parser(metaclass=_Parser):
),
TokenType.FOREIGN_KEY: lambda self: self._parse_foreign_key(),
TokenType.UNIQUE: lambda self: self._parse_unique(),
TokenType.LIKE: lambda self: self._parse_create_like(),
}

NO_PAREN_FUNCTION_PARSERS = {
Expand Down Expand Up @@ -882,6 +884,19 @@ def _parse_stored(self):
def _parse_distkey(self):
return self.expression(exp.DistKeyProperty, this=self._parse_wrapped(self._parse_var))

def _parse_create_like(self):
table = self._parse_table(schema=True)
options = []
while self._match_texts(("INCLUDING", "EXCLUDING")):
options.append(
self.expression(
exp.Property,
this=self._prev.text.upper(),
value=exp.Var(this=self._parse_id_var().this.upper()),
)
)
return self.expression(exp.LikeProperty, this=table, expressions=options)

def _parse_sortkey(self, compound=False):
return self.expression(
exp.SortKeyProperty, this=self._parse_wrapped_csv(self._parse_var), compound=compound
Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def test_identity(self):
self.validate_identity("SELECT TRIM('bla' FROM ' XXX ')")
self.validate_identity("@@GLOBAL.max_connections")

self.validate_identity("CREATE TABLE A LIKE B")

# SET Commands
self.validate_identity("SET @var_name = expr")
self.validate_identity("SET @name = 43")
Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def test_postgres(self):
"spark": "CREATE TABLE x (a UUID, b BINARY)",
},
)

self.validate_identity(
"CREATE TABLE A (LIKE B INCLUDING CONSTRAINT INCLUDING COMPRESSION EXCLUDING COMMENTS)"
)
self.validate_all(
"SELECT SUM(x) OVER (PARTITION BY a ORDER BY d ROWS 1 PRECEDING)",
write={
Expand Down

0 comments on commit 4851ce1

Please sign in to comment.