Skip to content

Commit

Permalink
Fix(postgres): generate float if the type has precision (tobymao#4516)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Dec 12, 2024
1 parent 2b68b9b commit e15cd0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,14 @@ def datatype_sql(self, expression: exp.DataType) -> str:
values = self.expressions(expression, key="values", flat=True)
return f"{self.expressions(expression, flat=True)}[{values}]"
return "ARRAY"

if (
expression.is_type(exp.DataType.Type.DOUBLE, exp.DataType.Type.FLOAT)
and expression.expressions
):
# Postgres doesn't support precision for REAL and DOUBLE PRECISION types
return f"FLOAT({self.expressions(expression, flat=True)})"

return super().datatype_sql(expression)

def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def test_redshift(self):
)

def test_identity(self):
self.validate_identity("SELECT CAST(value AS FLOAT(8))")
self.validate_identity("1 div", "1 AS div")
self.validate_identity("LISTAGG(DISTINCT foo, ', ')")
self.validate_identity("CREATE MATERIALIZED VIEW orders AUTO REFRESH YES AS SELECT 1")
Expand Down

0 comments on commit e15cd0b

Please sign in to comment.