summaryrefslogtreecommitdiffstats
path: root/sqlglot/generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'sqlglot/generator.py')
-rw-r--r--sqlglot/generator.py72
1 files changed, 67 insertions, 5 deletions
diff --git a/sqlglot/generator.py b/sqlglot/generator.py
index 40ba88e..ed0a681 100644
--- a/sqlglot/generator.py
+++ b/sqlglot/generator.py
@@ -68,6 +68,7 @@ class Generator:
exp.EncodeColumnConstraint: lambda self, e: f"ENCODE {self.sql(e, 'this')}",
exp.ExecuteAsProperty: lambda self, e: self.naked_property(e),
exp.ExternalProperty: lambda self, e: "EXTERNAL",
+ exp.HeapProperty: lambda self, e: "HEAP",
exp.InlineLengthColumnConstraint: lambda self, e: f"INLINE LENGTH {self.sql(e, 'this')}",
exp.LanguageProperty: lambda self, e: self.naked_property(e),
exp.LocationProperty: lambda self, e: self.naked_property(e),
@@ -161,6 +162,9 @@ class Generator:
# Whether or not to generate the (+) suffix for columns used in old-style join conditions
COLUMN_JOIN_MARKS_SUPPORTED = False
+ # Whether or not to generate an unquoted value for EXTRACT's date part argument
+ EXTRACT_ALLOWS_QUOTES = True
+
# https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
SELECT_KINDS: t.Tuple[str, ...] = ("STRUCT", "VALUE")
@@ -224,6 +228,7 @@ class Generator:
exp.FallbackProperty: exp.Properties.Location.POST_NAME,
exp.FileFormatProperty: exp.Properties.Location.POST_WITH,
exp.FreespaceProperty: exp.Properties.Location.POST_NAME,
+ exp.HeapProperty: exp.Properties.Location.POST_WITH,
exp.IsolatedLoadingProperty: exp.Properties.Location.POST_NAME,
exp.JournalProperty: exp.Properties.Location.POST_NAME,
exp.LanguageProperty: exp.Properties.Location.POST_SCHEMA,
@@ -265,9 +270,12 @@ class Generator:
# Expressions whose comments are separated from them for better formatting
WITH_SEPARATED_COMMENTS: t.Tuple[t.Type[exp.Expression], ...] = (
+ exp.Delete,
exp.Drop,
exp.From,
+ exp.Insert,
exp.Select,
+ exp.Update,
exp.Where,
exp.With,
)
@@ -985,8 +993,9 @@ class Generator:
) -> str:
if properties.expressions:
expressions = self.expressions(properties, sep=sep, indent=False)
- expressions = self.wrap(expressions) if wrapped else expressions
- return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
+ if expressions:
+ expressions = self.wrap(expressions) if wrapped else expressions
+ return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
return ""
def with_properties(self, properties: exp.Properties) -> str:
@@ -1905,7 +1914,7 @@ class Generator:
return f"NEXT VALUE FOR {self.sql(expression, 'this')}{order}"
def extract_sql(self, expression: exp.Extract) -> str:
- this = self.sql(expression, "this")
+ this = self.sql(expression, "this") if self.EXTRACT_ALLOWS_QUOTES else expression.this.name
expression_sql = self.sql(expression, "expression")
return f"EXTRACT({this} FROM {expression_sql})"
@@ -2370,7 +2379,12 @@ class Generator:
elif arg_value is not None:
args.append(arg_value)
- return self.func(expression.sql_name(), *args)
+ if self.normalize_functions:
+ name = expression.sql_name()
+ else:
+ name = (expression._meta and expression.meta.get("name")) or expression.sql_name()
+
+ return self.func(name, *args)
def func(
self,
@@ -2412,7 +2426,7 @@ class Generator:
return ""
if flat:
- return sep.join(self.sql(e) for e in expressions)
+ return sep.join(sql for sql in (self.sql(e) for e in expressions) if sql)
num_sqls = len(expressions)
@@ -2423,6 +2437,9 @@ class Generator:
result_sqls = []
for i, e in enumerate(expressions):
sql = self.sql(e, comment=False)
+ if not sql:
+ continue
+
comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
if self.pretty:
@@ -2562,6 +2579,51 @@ class Generator:
record_reader = f" RECORDREADER {record_reader}" if record_reader else ""
return f"{transform}{row_format_before}{record_writer}{using}{schema}{row_format_after}{record_reader}"
+ def indexconstraintoption_sql(self, expression: exp.IndexConstraintOption) -> str:
+ key_block_size = self.sql(expression, "key_block_size")
+ if key_block_size:
+ return f"KEY_BLOCK_SIZE = {key_block_size}"
+
+ using = self.sql(expression, "using")
+ if using:
+ return f"USING {using}"
+
+ parser = self.sql(expression, "parser")
+ if parser:
+ return f"WITH PARSER {parser}"
+
+ comment = self.sql(expression, "comment")
+ if comment:
+ return f"COMMENT {comment}"
+
+ visible = expression.args.get("visible")
+ if visible is not None:
+ return "VISIBLE" if visible else "INVISIBLE"
+
+ engine_attr = self.sql(expression, "engine_attr")
+ if engine_attr:
+ return f"ENGINE_ATTRIBUTE = {engine_attr}"
+
+ secondary_engine_attr = self.sql(expression, "secondary_engine_attr")
+ if secondary_engine_attr:
+ return f"SECONDARY_ENGINE_ATTRIBUTE = {secondary_engine_attr}"
+
+ self.unsupported("Unsupported index constraint option.")
+ return ""
+
+ def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str:
+ kind = self.sql(expression, "kind")
+ kind = f"{kind} INDEX" if kind else "INDEX"
+ this = self.sql(expression, "this")
+ this = f" {this}" if this else ""
+ type_ = self.sql(expression, "type")
+ type_ = f" USING {type_}" if type_ else ""
+ schema = self.sql(expression, "schema")
+ schema = f" {schema}" if schema else ""
+ options = self.expressions(expression, key="options", sep=" ")
+ options = f" {options}" if options else ""
+ return f"{kind}{this}{type_}{schema}{options}"
+
def cached_generator(
cache: t.Optional[t.Dict[int, str]] = None