Repository: tobymao/sqlglot

Issue:
Error on parsing SHOW statement with '.' (eg show index from schema_name.table_name)
SQLGlot is throwing an error when I try to "show index from schema_name.table_name" (I am using MySQL as the dialect), due to the period ('.') in the SQL statement. For example "show columns from information_schema.tables" - it is no issue for SELECT statements but possibly for others.

Apologies this is my first time participating on GitHub - if the above isn't specific/clear enough or I can provide any more detail please let me know.

Thanks
James

Discussion:

Patch:
diff --git a/sqlglot/dialects/mysql.py b/sqlglot/dialects/mysql.py
index 1e2cfa32..1e8f694f 100644
--- a/sqlglot/dialects/mysql.py
+++ b/sqlglot/dialects/mysql.py
@@ -303,7 +303,13 @@ class MySQL(Dialect):
                 db = None
             else:
                 position = None
-                db = self._parse_id_var() if self._match_text_seq("FROM") else None
+                db = None
+
+                if self._match(TokenType.FROM):
+                    db = self._parse_id_var()
+                elif self._match(TokenType.DOT):
+                    db = target_id
+                    target_id = self._parse_id_var()
 
             channel = self._parse_id_var() if self._match_text_seq("FOR", "CHANNEL") else None
 


Test patch:
diff --git a/tests/dialects/test_mysql.py b/tests/dialects/test_mysql.py
index 5f8560a0..a0f62d8d 100644
--- a/tests/dialects/test_mysql.py
+++ b/tests/dialects/test_mysql.py
@@ -424,6 +424,10 @@ COMMENT='客户账户表'"""
         show = self.validate_identity("SHOW INDEX FROM foo FROM bar")
         self.assertEqual(show.text("db"), "bar")
 
+        self.validate_all(
+            "SHOW INDEX FROM bar.foo", write={"mysql": "SHOW INDEX FROM foo FROM bar"}
+        )
+
     def test_show_db_like_or_where_sql(self):
         for key in [
             "OPEN TABLES",