sqlglot icon indicating copy to clipboard operation
sqlglot copied to clipboard

DATEPART arguments without quotes (redshift)

Open ArielGonzalezC opened this issue 2 years ago • 0 comments

Hello, I'm having trouble applying a qualify to queries that uses the DATE_PART function. The first argument is being identified as a column of the table instead of as a "Date part".

from sqlglot.optimizer.qualify import qualify
import sqlglot as sg

query = """
    SELECT TOP 10 *
    FROM somedb.sometable
    WHERE DATE_PART(year, "somecol") * 100 + DATE_PART(month, "somecol") = 202403

"""
print(
    qualify(
        sg.parse_one(query, dialect = "redshift")
    ).sql( pretty = True)
)

output:

SELECT
  *
FROM "somedb"."sometable" AS "sometable"
WHERE
  EXTRACT("sometable"."year" FROM "sometable"."somecol") * 100 + EXTRACT("sometable"."month" FROM "sometable"."somecol") = 202403
LIMIT 10

Although this issue is resolved by putting single quotes around year and month, I think it would be ideal if this logic could be included in the current algorithm. The respective documentation states that date parts are accepted as arguments with or without quotes:

https://docs.aws.amazon.com/redshift/latest/dg/r_DATE_PART_function.html

ArielGonzalezC avatar Aug 06 '24 21:08 ArielGonzalezC