Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snowflake TO_CHAR being replaced with CAST to text #4857

Open
asarama opened this issue Mar 7, 2025 · 0 comments
Open

Snowflake TO_CHAR being replaced with CAST to text #4857

asarama opened this issue Mar 7, 2025 · 0 comments

Comments

@asarama
Copy link

asarama commented Mar 7, 2025

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

Fully reproducible code snippet
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.

from sqlglot import parse_one

sf_query = """
SELECT
    TO_CHAR(RAW_ORDERS."ORDER_DATE", 'YYYY-MM') AS ORDER_YEAR_MONTH
FROM
    DEVELOPER.SCHEMA.RAW_ORDERS AS RAW_ORDERS
"""

sf_query = parse_one(query, dialect="snowflake")
duckdb_query = sf_query.sql(dialect="duckdb", pretty=True)

Expected output

SELECT
    STRFTIME(RAW_ORDERS."ORDER_DATE", '%Y-%m') AS ORDER_YEAR_MONTH
FROM
    DEVELOPER.SCHEMA.RAW_ORDERS AS RAW_ORDERS

Actual output

SELECT
    CAST(RAW_ORDERS."ORDER_DATE" AS TEXT) AS ORDER_YEAR_MONTH
FROM
    DEVELOPER.SCHEMA.RAW_ORDERS AS RAW_ORDERS

The ORDER_DATE column is a timestamp.

Happy to provide more info as needed. This might be quite challenging since TO_CHAR is quite flexible, but the usage patterns for timestamp conversions are fairly small.

https://docs.snowflake.com/en/sql-reference/functions/to_char
https://duckdb.org/docs/stable/sql/functions/dateformat.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant