Repository: tobymao/sqlglot

Issue:
Use of reserved name ("current_time") as an alias cause a parse error in BigQuery
Hey there, and thanks for this awesome project!

I've started using sqlglot recently, and noticed a possible bug, when using a reserved keyword as an alias. This is clearly a bad alias name, but I did get more than a handful of automatic systems creating queries with reserved aliases; `row` and `current_time` seemed quite popular.

I've done some testing, and found this query syntax to be valid in Postgres (15.1) and BigQuery, but not valid in MySQL. 

```
postgres=# select 1 current_time;
 current_time
--------------
            1
(1 row)
```

Parsing such a query with Postgres dialect is done correctly, but fails for BigQuery:

```
>>> sqlglot.parse("SELECT 1 current_time", sqlglot.Dialects.BIGQUERY)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/elisha/.local/lib/python3.9/site-packages/sqlglot/__init__.py", line 55, in parse
    return dialect.parse(sql, **opts)
  File "/home/elisha/.local/lib/python3.9/site-packages/sqlglot/dialects/dialect.py", line 149, in parse
    return self.parser(**opts).parse(self.tokenizer.tokenize(sql), sql)
  File "/home/elisha/.local/lib/python3.9/site-packages/sqlglot/parser.py", line 591, in parse
    return self._parse(
  File "/home/elisha/.local/lib/python3.9/site-packages/sqlglot/parser.py", line 627, in _parse
    self.raise_error("Invalid expression / Unexpected token")
  File "/home/elisha/.local/lib/python3.9/site-packages/sqlglot/parser.py", line 652, in raise_error
    raise error
sqlglot.errors.ParseError: Invalid expression / Unexpected token. Line 1, Col: 10.
  SELECT 1 current_time



>>> sqlglot.parse("SELECT 1 current_time", sqlglot.Dialects.POSTGRES)
[(SELECT expressions:
  (ALIAS this:
    (LITERAL this: 1, is_string: False), alias:
    (IDENTIFIER this: current_time, quoted: False)))]
```

I would love to hear your thoughts about this, or possibly help implementing it, if you could point me in the right direction. 

Thanks! 


Discussion:

Resolving patch:
diff --git a/sqlglot/parser.py b/sqlglot/parser.py
--- a/sqlglot/parser.py
+++ b/sqlglot/parser.py
@@ -153,6 +153,7 @@ class Parser(metaclass=_Parser):
         TokenType.COMMAND,
         TokenType.COMMIT,
         TokenType.CONSTRAINT,
+        TokenType.CURRENT_TIME,
         TokenType.DEFAULT,
         TokenType.DELETE,
         TokenType.DESCRIBE,
@@ -214,6 +215,7 @@ class Parser(metaclass=_Parser):
         TokenType.VOLATILE,
         *SUBQUERY_PREDICATES,
         *TYPE_TOKENS,
+        *NO_PAREN_FUNCTIONS,
     }
 
     TABLE_ALIAS_TOKENS = ID_VAR_TOKENS - {TokenType.NATURAL, TokenType.APPLY}


Test patch:
diff --git a/tests/test_transpile.py b/tests/test_transpile.py
--- a/tests/test_transpile.py
+++ b/tests/test_transpile.py
@@ -20,6 +20,13 @@ def validate(self, sql, target, **kwargs):
         self.assertEqual(transpile(sql, **kwargs)[0], target)
 
     def test_alias(self):
+        self.assertEqual(transpile("SELECT 1 current_time")[0], "SELECT 1 AS current_time")
+        self.assertEqual(
+            transpile("SELECT 1 current_timestamp")[0], "SELECT 1 AS current_timestamp"
+        )
+        self.assertEqual(transpile("SELECT 1 current_date")[0], "SELECT 1 AS current_date")
+        self.assertEqual(transpile("SELECT 1 current_datetime")[0], "SELECT 1 AS current_datetime")
+
         for key in ("union", "filter", "over", "from", "join"):
             with self.subTest(f"alias {key}"):
                 self.validate(f"SELECT x AS {key}", f"SELECT x AS {key}")