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/MariaDB dialect #5856

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions docs/source/dialects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ The dialect for `Materialize`_.

.. _`Materialize`: https://materialize.com/

.. _mariadb_dialect_ref:

MariaDB
-------

The dialect for `MariaDB`_.

.. _`MariaDB`: https://www.mariadb.org/

.. _mysql_dialect_ref:

MySQL
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ keywords = [
"greenplum",
"hive",
"materialize",
"mariadb",
"mysql",
"postgres",
"redshift",
Expand Down
1 change: 1 addition & 0 deletions src/sqlfluff/core/dialects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"greenplum": ("dialect_greenplum", "greenplum_dialect"),
"hive": ("dialect_hive", "hive_dialect"),
"materialize": ("dialect_materialize", "materialize_dialect"),
"mariadb": ("dialect_mariadb", "mariadb_dialect"),
"mysql": ("dialect_mysql", "mysql_dialect"),
"oracle": ("dialect_oracle", "oracle_dialect"),
"postgres": ("dialect_postgres", "postgres_dialect"),
Expand Down
175 changes: 175 additions & 0 deletions src/sqlfluff/dialects/dialect_mariadb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"""MariaDB Dialect.

https://mariadb.com/kb/en/sql-statements-structure/
"""

from sqlfluff.core.dialects import load_raw_dialect
from sqlfluff.core.parser import (
Bracketed,
Delimited,
Matchable,
OneOf,
OptionallyBracketed,
Ref,
Sequence,
)
from sqlfluff.dialects import dialect_mysql as mysql
from sqlfluff.dialects.dialect_mariadb_keywords import (
mariadb_reserved_keywords,
mariadb_unreserved_keywords,
)

# ansi_dialect = load_raw_dialect("ansi")
mysql_dialect = load_raw_dialect("mysql")
mariadb_dialect = mysql_dialect.copy_as("mariadb")
mariadb_dialect.update_keywords_set_from_multiline_string(
"unreserved_keywords", mariadb_unreserved_keywords
)
mariadb_dialect.sets("reserved_keywords").clear()
mariadb_dialect.update_keywords_set_from_multiline_string(
"reserved_keywords", mariadb_reserved_keywords
)


class CreateUserStatementSegment(mysql.CreateUserStatementSegment):
"""`CREATE USER` statement.

https://mariadb.com/kb/en/create-user/
"""

match_grammar = mysql.CreateUserStatementSegment.match_grammar.copy(
insert=[Ref("OrReplaceGrammar", optional=True)],
before=Ref.keyword("USER"),
)


class CreateTableStatementSegment(mysql.CreateTableStatementSegment):
"""`CREATE TABLE` segment.

https://mariadb.com/kb/en/create-table/
"""

match_grammar = mysql.CreateTableStatementSegment.match_grammar.copy(
insert=[Ref("OrReplaceGrammar", optional=True)],
before=Ref("TemporaryTransientGrammar", optional=True),
).copy(
insert=[
OneOf(
# Columns and comment syntax:
Sequence(
Bracketed(
Delimited(
OneOf(
Ref("TableConstraintSegment"),
Ref("ColumnDefinitionSegment"),
),
)
),
Ref("CommentClauseSegment", optional=True),
Sequence(
Ref.keyword("AS", optional=True),
OptionallyBracketed(Ref("SelectableGrammar")),
optional=True,
),
),
# Create AS syntax:
Sequence(
Ref.keyword("AS", optional=True),
OptionallyBracketed(Ref("SelectableGrammar")),
),
# Create like syntax
Sequence("LIKE", Ref("TableReferenceSegment")),
),
],
before=Ref("TableEndClauseSegment", optional=True),
remove=[
OneOf(
Sequence(
Bracketed(
Delimited(
OneOf(
Ref("TableConstraintSegment"),
Ref("ColumnDefinitionSegment"),
),
)
),
Ref("CommentClauseSegment", optional=True),
),
Sequence(
"AS",
OptionallyBracketed(Ref("SelectableGrammar")),
),
Sequence("LIKE", Ref("TableReferenceSegment")),
),
],
)


class FlushStatementSegment(mysql.FlushStatementSegment):
"""A `Flush` statement.

https://mariadb.com/kb/en/flush/
"""

match_grammar: Matchable = Sequence(
"FLUSH",
OneOf(
"NO_WRITE_TO_BINLOG",
"LOCAL",
optional=True,
),
OneOf(
Delimited(
Sequence("BINARY", "LOGS"),
Sequence("ENGINE", "LOGS"),
Sequence("ERROR", "LOGS"),
Sequence("GENERAL", "LOGS"),
Sequence("QUERY", "CACHE"),
Sequence("SLOW", "LOGS"),
Sequence(Ref.keyword("RESET", optional=True), "MASTER"),
Sequence(OneOf("GLOBAL", "SESSION", optional=True), "STATUS"),
Sequence(
"RELAY",
"LOGS",
Sequence("FOR", "CHANNEL", optional=True),
Ref("ObjectReferenceSegment"),
),
"HOSTS",
"LOGS",
"PRIVILEGES",
"CHANGED_PAGE_BITMAPS",
"CLIENT_STATISTICS",
"DES_KEY_FILE",
"INDEX_STATISTICS",
"QUERY_RESPONSE_TIME",
"SLAVE",
"SSL",
"TABLE_STATISTICS",
"USER_STATISTICS",
"USER_VARIABLES",
"USER_RESOURCES",
),
Sequence(
"TABLES",
Sequence(
Delimited(Ref("TableReferenceSegment"), terminators=["WITH"]),
optional=True,
),
Sequence(
"WITH",
"READ",
"LOCK",
Sequence("AND", "DISABLE", "CHECKPOINT", optional=True),
optional=True,
),
),
Sequence(
"TABLES",
Sequence(
Delimited(Ref("TableReferenceSegment"), terminators=["FOR"]),
optional=False,
),
Sequence("FOR", "EXPORT", optional=True),
),
),
)