Skip to content

Commit

Permalink
Feat(mysql): add support for operation modifiers like HIGH_PRIORITY
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Oct 14, 2024
1 parent b26a3f6 commit 38c26e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions sqlglot/dialects/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,17 @@ class Parser(parser.Parser):
TokenType.SET,
}

# SELECT [ ALL | DISTINCT | DISTINCTROW ] [ <OPERATION_MODIFIERS> ]
OPERATION_MODIFIERS = {
"HIGH_PRIORITY",
"STRAIGHT_JOIN",
"SQL_SMALL_RESULT",
"SQL_BIG_RESULT",
"SQL_BUFFER_RESULT",
"SQL_NO_CACHE",
"SQL_CALC_FOUND_ROWS",
}

LOG_DEFAULTS_TO_LN = True
STRING_ALIASES = True
VALUES_FOLLOWED_BY_PAREN = False
Expand Down
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,7 @@ class Select(Query):
"distinct": False,
"into": False,
"from": False,
"operation_modifiers": False,
**QUERY_MODIFIERS,
}

Expand Down
5 changes: 4 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2514,13 +2514,16 @@ def select_sql(self, expression: exp.Select) -> str:
)
kind = ""

operation_modifiers = self.expressions(expression, key="operation_modifiers", sep=" ")
operation_modifiers = f"{self.sep()}{operation_modifiers}" if operation_modifiers else ""

# We use LIMIT_IS_TOP as a proxy for whether DISTINCT should go first because tsql and Teradata
# are the only dialects that use LIMIT_IS_TOP and both place DISTINCT first.
top_distinct = f"{distinct}{hint}{top}" if self.LIMIT_IS_TOP else f"{top}{hint}{distinct}"
expressions = f"{self.sep()}{expressions}" if expressions else expressions
sql = self.query_modifiers(
expression,
f"SELECT{top_distinct}{kind}{expressions}",
f"SELECT{top_distinct}{operation_modifiers}{kind}{expressions}",
self.sql(expression, "into", comment=False),
self.sql(expression, "from", comment=False),
)
Expand Down
7 changes: 7 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,8 @@ class Parser(metaclass=_Parser):
# The style options for the DESCRIBE statement
DESCRIBE_STYLES = {"ANALYZE", "EXTENDED", "FORMATTED", "HISTORY"}

OPERATION_MODIFIERS: t.Set[str] = set()

STRICT_CAST = True

PREFIXED_PIVOT_COLUMNS = False
Expand Down Expand Up @@ -2958,6 +2960,10 @@ def _parse_select(
if all_ and distinct:
self.raise_error("Cannot specify both ALL and DISTINCT after SELECT")

operation_modifiers = []
while self._curr and self._match_texts(self.OPERATION_MODIFIERS):
operation_modifiers.append(self._prev.text.upper())

limit = self._parse_limit(top=True)
projections = self._parse_projections()

Expand All @@ -2968,6 +2974,7 @@ def _parse_select(
distinct=distinct,
expressions=projections,
limit=limit,
operation_modifiers=operation_modifiers or None,
)
this.comments = comments

Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def test_ddl(self):
)

def test_identity(self):
self.validate_identity("SELECT HIGH_PRIORITY STRAIGHT_JOIN SQL_CALC_FOUND_ROWS * FROM t")
self.validate_identity("SELECT CAST(COALESCE(`id`, 'NULL') AS CHAR CHARACTER SET binary)")
self.validate_identity("SELECT e.* FROM e STRAIGHT_JOIN p ON e.x = p.y")
self.validate_identity("ALTER TABLE test_table ALTER COLUMN test_column SET DEFAULT 1")
Expand Down

0 comments on commit 38c26e1

Please sign in to comment.