Repository: tobymao/sqlglot

Issue:
Handle BYTE statement in Oracle DDL to Spark
using sqlglot to transpile some Oracle DDL to Spark that I have from a customer.  The modified code changed the `VARCHAR2(5 BYTE)` to `VACHAR(5 BYTE)` but the DDL still fails in Spark with the `(5 BYTE)`.   Once removing the word BYTE the DDL executed fine.



Discussion:

Resolving patch:
diff --git a/sqlglot/dialects/oracle.py b/sqlglot/dialects/oracle.py
--- a/sqlglot/dialects/oracle.py
+++ b/sqlglot/dialects/oracle.py
@@ -153,6 +153,7 @@ class Generator(generator.Generator):
         JOIN_HINTS = False
         TABLE_HINTS = False
         COLUMN_JOIN_MARKS_SUPPORTED = True
+        DATA_TYPE_SPECIFIERS_ALLOWED = True
 
         LIMIT_FETCH = "FETCH"
 
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -216,6 +216,9 @@ class Generator:
     # Whether or not COLLATE is a function instead of a binary operator
     COLLATE_IS_FUNC = False
 
+    # Whether or not data types support additional specifiers like e.g. CHAR or BYTE (oracle)
+    DATA_TYPE_SPECIFIERS_ALLOWED = False
+
     TYPE_MAPPING = {
         exp.DataType.Type.NCHAR: "CHAR",
         exp.DataType.Type.NVARCHAR: "VARCHAR",
@@ -892,7 +895,7 @@ def rawstring_sql(self, expression: exp.RawString) -> str:
     def datatypeparam_sql(self, expression: exp.DataTypeParam) -> str:
         this = self.sql(expression, "this")
         specifier = self.sql(expression, "expression")
-        specifier = f" {specifier}" if specifier else ""
+        specifier = f" {specifier}" if specifier and self.DATA_TYPE_SPECIFIERS_ALLOWED else ""
         return f"{this}{specifier}"
 
     def datatype_sql(self, expression: exp.DataType) -> str:


Test patch:
diff --git a/tests/dialects/test_oracle.py b/tests/dialects/test_oracle.py
--- a/tests/dialects/test_oracle.py
+++ b/tests/dialects/test_oracle.py
@@ -22,8 +22,6 @@ def test_oracle(self):
         self.validate_identity("SELECT * FROM t FOR UPDATE OF s.t.c, s.t.v SKIP LOCKED")
         self.validate_identity("SELECT STANDARD_HASH('hello')")
         self.validate_identity("SELECT STANDARD_HASH('hello', 'MD5')")
-        self.validate_identity("SELECT CAST(NULL AS VARCHAR2(2328 CHAR)) AS COL1")
-        self.validate_identity("SELECT CAST(NULL AS VARCHAR2(2328 BYTE)) AS COL1")
         self.validate_identity("SELECT * FROM table_name@dblink_name.database_link_domain")
         self.validate_identity("SELECT * FROM table_name SAMPLE (25) s")
         self.validate_identity("SELECT * FROM V$SESSION")
@@ -60,6 +58,20 @@ def test_oracle(self):
             "SELECT * FROM t SAMPLE (0.25)",
         )
 
+        self.validate_all(
+            "SELECT CAST(NULL AS VARCHAR2(2328 CHAR)) AS COL1",
+            write={
+                "oracle": "SELECT CAST(NULL AS VARCHAR2(2328 CHAR)) AS COL1",
+                "spark": "SELECT CAST(NULL AS VARCHAR(2328)) AS COL1",
+            },
+        )
+        self.validate_all(
+            "SELECT CAST(NULL AS VARCHAR2(2328 BYTE)) AS COL1",
+            write={
+                "oracle": "SELECT CAST(NULL AS VARCHAR2(2328 BYTE)) AS COL1",
+                "spark": "SELECT CAST(NULL AS VARCHAR(2328)) AS COL1",
+            },
+        )
         self.validate_all(
             "NVL(NULL, 1)",
             write={