Skip to content

Redshift Round function does not get parsed correctly #6340

@aman0456

Description

@aman0456

Before you file an issue

  • Make sure you specify the "read" dialect eg. parse_one(sql, read="spark")
  • Make sure you specify the "write" dialect eg. ast.sql(dialect="duckdb")
  • Check if the issue still exists on main

Read dialect: redshift
Write dialect: redshift
Issue exists on the latest release

Issue: the redshift snippet given below, when parsed, adds an extra cast to DECIMAL. This is incorrect because:

  1. It is adding an extra typecast and not maintaining parity with input SQL
  2. DECIMAL in redshift by default converts values to integers. This is because it expects a precision parameter which specifies the number of decimal digits to keep. By default, this is 0.

Interesting point to note is that if I remove either of the CASTs to DOUBLE PRECISION, then this problem goes away.
Fully reproducible code snippet
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.

Python 3.11.11 (main, Dec  3 2024, 17:20:40) [Clang 16.0.0 (clang-1600.0.26.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlglot
>>> sqlglot.__version__
'27.29.0'
>>> x = """ROUND(
...     (
...       CAST(a AS DOUBLE PRECISION)
...     ) / (
...         CAST(b AS DOUBLE PRECISION)
...     ),
...     2
...   )"""
>>> sqlglot.parse_one(x, dialect='redshift').sql(dialect='redshift')
'ROUND(CAST((CAST(a AS DOUBLE PRECISION)) / (CAST(b AS DOUBLE PRECISION)) AS DECIMAL), 2)'

Official Documentation
Please include links to official SQL documentation related to your issue.
Official documentation describing the behavior of DECIMAL
https://docs.aws.amazon.com/redshift/latest/dg/r_Numeric_types201.html#r_Numeric_types201-decimal-or-numeric-type

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions