Repository: tobymao/sqlglot

Issue:
Problem with parsing Teradata COLLECT STATISTICS command
I have found a problem parsing the COLLECT STATISTICS command in Teradata.  
All variations of the COLLECT STATISTICS command fail

COLLECT STATISTICS ON TABLE1
INDEX(COL1);

COLLECT STATS
ON TABLE1
COLUMNS(COL1);

COLLECT STATS ON TABLE1
COLUMNS(COL1);

COLLECT STATS
COLUMNS(COL1)
ON TABLE1;

sqlglot Version: 17.4.1
Teradata version 17.10

Error message is 

File ~\AppData\Roaming\Python\Python311\site-packages\sqlglot\parser.py:946, in Parser._parse(self, parse_method, raw_tokens, sql)
    943     expressions.append(parse_method(self))
    945     if self._index < len(self._tokens):
--> 946         self.raise_error("Invalid expression / Unexpected token")
    948     self.check_errors()
    950 return expressions

File ~\AppData\Roaming\Python\Python311\site-packages\sqlglot\parser.py:987, in Parser.raise_error(self, message, token)
    975 error = ParseError.new(
    976     f"{message}. Line {token.line}, Col: {token.col}.\n"
    977     f"  {start_context}\033[4m{highlight}\033[0m{end_context}",
   (...)
    983     end_context=end_context,
    984 )
    986 if self.error_level == ErrorLevel.IMMEDIATE:
--> 987     raise error
    989 self.errors.append(error)

or 

File ~\AppData\Roaming\Python\Python311\site-packages\sqlglot\parser.py:946, in Parser._parse(self, parse_method, raw_tokens, sql)
    943     expressions.append(parse_method(self))
    945     if self._index < len(self._tokens):
--> 946         self.raise_error("Invalid expression / Unexpected token")
    948     self.check_errors()
    950 return expressions

File ~\AppData\Roaming\Python\Python311\site-packages\sqlglot\parser.py:987, in Parser.raise_error(self, message, token)
    975 error = ParseError.new(
    976     f"{message}. Line {token.line}, Col: {token.col}.\n"
    977     f"  {start_context}\033[4m{highlight}\033[0m{end_context}",
   (...)
    983     end_context=end_context,
    984 )
    986 if self.error_level == ErrorLevel.IMMEDIATE:
--> 987     raise error
    989 self.errors.append(error)

ParseError: Invalid expression / Unexpected token. Line 2, Col: 16.
  
COLLECT STATS ON TABLE1
COLUMNS(COL1);


Comments:

Patch:
diff --git a/sqlglot/dialects/teradata.py b/sqlglot/dialects/teradata.py
index 4e8ffb4a..2e0e8f9d 100644
--- a/sqlglot/dialects/teradata.py
+++ b/sqlglot/dialects/teradata.py
@@ -33,8 +33,11 @@ class Teradata(Dialect):
             **tokens.Tokenizer.KEYWORDS,
             "^=": TokenType.NEQ,
             "BYTEINT": TokenType.SMALLINT,
+            "COLLECT STATS": TokenType.COMMAND,
+            "COLLECT STATISTICS": TokenType.COMMAND,
             "GE": TokenType.GTE,
             "GT": TokenType.GT,
+            "HELP": TokenType.COMMAND,
             "INS": TokenType.INSERT,
             "LE": TokenType.LTE,
             "LT": TokenType.LT,


Test patch:
diff --git a/tests/dialects/test_teradata.py b/tests/dialects/test_teradata.py
index 0df6d0be..4d322419 100644
--- a/tests/dialects/test_teradata.py
+++ b/tests/dialects/test_teradata.py
@@ -22,6 +22,13 @@ class TestTeradata(Validator):
             },
         )
 
+    def test_statistics(self):
+        self.validate_identity("COLLECT STATISTICS ON tbl INDEX(col)")
+        self.validate_identity("COLLECT STATS ON tbl COLUMNS(col)")
+        self.validate_identity("COLLECT STATS COLUMNS(col) ON tbl")
+        self.validate_identity("HELP STATISTICS personel.employee")
+        self.validate_identity("HELP STATISTICS personnel.employee FROM my_qcd")
+
     def test_create(self):
         self.validate_identity("CREATE TABLE x (y INT) PRIMARY INDEX (y) PARTITION BY y INDEX (y)")
         self.validate_identity("CREATE TABLE x (y INT) PARTITION BY y INDEX (y)")