Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlglot/generators/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def _round_sql(self: PostgresGenerator, expression: exp.Round) -> str:

# ROUND(double precision, integer) is not permitted in Postgres
# so it's necessary to cast to decimal before rounding.
if expression.this.is_type(exp.DType.DOUBLE):
if expression.this.is_type(*exp.DataType.FLOAT_TYPES):
decimal_type = exp.DType.DECIMAL.into_expr(expressions=expression.expressions)
this = self.sql(exp.Cast(this=this, to=decimal_type))

Expand Down
5 changes: 5 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,11 @@ def test_round(self):
"bigquery": "ROUND(x::DOUBLE, 4)",
},
)
# ROUND(real, integer) is not permitted in Postgres either, so REAL must be cast too.
self.validate_all(
"ROUND(CAST(CAST(x AS REAL) AS DECIMAL), 4)",
read={"postgres": "ROUND(x::REAL, 4)"},
)
Comment on lines +1802 to +1805

@geooo109 geooo109 Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't supported in postgres, there is no need to cover garbage input and fix it in the output.

ref: #7905 (comment)

self.validate_all(
"ROUND(CAST(x AS DECIMAL(18, 3)), 4)", read={"duckdb": "ROUND(x::DECIMAL, 4)"}
)
Expand Down
Loading