diff options
Diffstat (limited to '')
-rw-r--r-- | sqlglot/generator.py | 81 |
1 files changed, 55 insertions, 26 deletions
diff --git a/sqlglot/generator.py b/sqlglot/generator.py index 0a7a81f..79501ef 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -109,6 +109,9 @@ class Generator: # Whether or not create function uses an AS before the RETURN CREATE_FUNCTION_RETURN_AS = True + # Whether or not to treat the division operator "/" as integer division + INTEGER_DIVISION = True + TYPE_MAPPING = { exp.DataType.Type.NCHAR: "CHAR", exp.DataType.Type.NVARCHAR: "VARCHAR", @@ -550,14 +553,17 @@ class Generator: else: expression_sql = f" AS{expression_sql}" - replace = " OR REPLACE" if expression.args.get("replace") else "" - unique = " UNIQUE" if expression.args.get("unique") else "" - exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else "" + postindex_props_sql = "" + if properties_locs.get(exp.Properties.Location.POST_INDEX): + postindex_props_sql = self.properties( + exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]), + wrapped=False, + prefix=" ", + ) indexes = expression.args.get("indexes") - index_sql = "" if indexes: - indexes_sql = [] + indexes_sql: t.List[str] = [] for index in indexes: ind_unique = " UNIQUE" if index.args.get("unique") else "" ind_primary = " PRIMARY" if index.args.get("primary") else "" @@ -568,21 +574,24 @@ class Generator: if index.args.get("columns") else "" ) - if index.args.get("primary") and properties_locs.get( - exp.Properties.Location.POST_INDEX - ): - postindex_props_sql = self.properties( - exp.Properties( - expressions=properties_locs[exp.Properties.Location.POST_INDEX] - ), - wrapped=False, + ind_sql = f"{ind_unique}{ind_primary}{ind_amp} INDEX{ind_name}{ind_columns}" + + if indexes_sql: + indexes_sql.append(ind_sql) + else: + indexes_sql.append( + f"{ind_sql}{postindex_props_sql}" + if index.args.get("primary") + else f"{postindex_props_sql}{ind_sql}" ) - ind_columns = f"{ind_columns} {postindex_props_sql}" - indexes_sql.append( - f"{ind_unique}{ind_primary}{ind_amp} INDEX{ind_name}{ind_columns}" - ) index_sql = "".join(indexes_sql) + else: + index_sql = postindex_props_sql + + replace = " OR REPLACE" if expression.args.get("replace") else "" + unique = " UNIQUE" if expression.args.get("unique") else "" + volatile = " VOLATILE" if expression.args.get("volatile") else "" postcreate_props_sql = "" if properties_locs.get(exp.Properties.Location.POST_CREATE): @@ -593,7 +602,7 @@ class Generator: wrapped=False, ) - modifiers = "".join((replace, unique, postcreate_props_sql)) + modifiers = "".join((replace, unique, volatile, postcreate_props_sql)) postexpression_props_sql = "" if properties_locs.get(exp.Properties.Location.POST_EXPRESSION): @@ -606,6 +615,7 @@ class Generator: wrapped=False, ) + exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else "" no_schema_binding = ( " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else "" ) @@ -1335,14 +1345,15 @@ class Generator: def placeholder_sql(self, expression: exp.Placeholder) -> str: return f":{expression.name}" if expression.name else "?" - def subquery_sql(self, expression: exp.Subquery) -> str: + def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str: alias = self.sql(expression, "alias") + alias = f"{sep}{alias}" if alias else "" sql = self.query_modifiers( expression, self.wrap(expression), self.expressions(expression, key="pivots", sep=" "), - f" AS {alias}" if alias else "", + alias, ) return self.prepend_ctes(expression, sql) @@ -1643,6 +1654,13 @@ class Generator: def command_sql(self, expression: exp.Command) -> str: return f"{self.sql(expression, 'this').upper()} {expression.text('expression').strip()}" + def comment_sql(self, expression: exp.Comment) -> str: + this = self.sql(expression, "this") + kind = expression.args["kind"] + exists_sql = " IF EXISTS " if expression.args.get("exists") else " " + expression_sql = self.sql(expression, "expression") + return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}" + def transaction_sql(self, *_) -> str: return "BEGIN" @@ -1728,19 +1746,30 @@ class Generator: return f"{self.sql(expression, 'this')} RESPECT NULLS" def intdiv_sql(self, expression: exp.IntDiv) -> str: - return self.sql( - exp.Cast( - this=exp.Div(this=expression.this, expression=expression.expression), - to=exp.DataType(this=exp.DataType.Type.INT), - ) - ) + div = self.binary(expression, "/") + return self.sql(exp.Cast(this=div, to=exp.DataType.build("INT"))) def dpipe_sql(self, expression: exp.DPipe) -> str: return self.binary(expression, "||") def div_sql(self, expression: exp.Div) -> str: + div = self.binary(expression, "/") + + if not self.INTEGER_DIVISION: + return self.sql(exp.Cast(this=div, to=exp.DataType.build("INT"))) + + return div + + def floatdiv_sql(self, expression: exp.FloatDiv) -> str: + if self.INTEGER_DIVISION: + this = exp.Cast(this=expression.this, to=exp.DataType.build("DOUBLE")) + return self.div_sql(exp.Div(this=this, expression=expression.expression)) + return self.binary(expression, "/") + def overlaps_sql(self, expression: exp.Overlaps) -> str: + return self.binary(expression, "OVERLAPS") + def distance_sql(self, expression: exp.Distance) -> str: return self.binary(expression, "<->") |