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

Incorrect DATE_SUB output in BigQuery #3509

Closed
elongl opened this issue May 19, 2024 · 1 comment
Closed

Incorrect DATE_SUB output in BigQuery #3509

elongl opened this issue May 19, 2024 · 1 comment

Comments

@elongl
Copy link

elongl commented May 19, 2024

Fully reproducible code snippet

query = "SELECT DATE_SUB(DATE '2008-12-25', INTERVAL 5 DAY) AS five_days_ago;"
opt_query = str(sqlglot.optimizer.optimize(query, dialect="bigquery", rules=[sqlglot.optimizer.qualify.qualify_tables]))
print(str(opt_query))

SELECT DATE_SUB(CAST('2008-12-25' AS DATE), 5, DAY) AS five_days_ago

The output DATE_SUB signature is invalid syntax in BigQuery.

@georgesittas
Copy link
Collaborator

You need to specify the output dialect using either transpile or the sql method. Calling str on an ast implicitly generates towards the default sqlglot dialect which does not necessarily follow bq's syntax.

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

2 participants