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

formatting issues when dealing with dates (DATEPART from tsql) #1869

Closed
laurentiupiciu opened this issue Jun 30, 2023 · 0 comments
Closed

formatting issues when dealing with dates (DATEPART from tsql) #1869

laurentiupiciu opened this issue Jun 30, 2023 · 0 comments

Comments

@laurentiupiciu
Copy link
Contributor

import sqlglot
from sqlglot.optimizer import optimize, RULES as optimize_rules

if name == 'main':
sqlglot_metadata = {
'sales': {'ReceiptDate': 'DATE', 'Qtty': 'FLOAT', 'Price': 'FLOAT', 'YEAR': 'STRING'}
}

print(sqlglot.version) # 16.7.7

experiments = [
{
'sql': """SELECT DATEPART(YEAR, s.ReceiptDate) AS Year, DATEPART(MONTH, s.ReceiptDate) AS Month, AVG(s.Price) AS AverageValue, AVG(s.Qtty) AS AverageItemCount FROM sales s WHERE s.ReceiptDate >= '2023-01-01' GROUP BY DATEPART(YEAR, s.ReceiptDate), DATEPART(MONTH, s.ReceiptDate) ORDER BY DATEPART(YEAR, s.ReceiptDate), DATEPART(MONTH, s.ReceiptDate)""",
'read': 'tsql',
'dialect': 'tsql',
},

{
  'sql': """SELECT DATEPART(YEAR, s.ReceiptDate) AS Year, DATEPART(MONTH, s.ReceiptDate) AS Month, AVG(s.Price) AS AverageValue, AVG(s.Qtty) AS AverageItemCount FROM sales s WHERE s.ReceiptDate >= '2023-01-01' GROUP BY DATEPART(YEAR, s.ReceiptDate), DATEPART(MONTH, s.ReceiptDate) ORDER BY DATEPART(YEAR, s.ReceiptDate), DATEPART(MONTH, s.ReceiptDate)""",
  'read': 'tsql',
  'dialect': None,
}

]

for exp in experiments:
expression = optimize(
expression=sqlglot.parse_one(sql=exp['sql'], read=exp['read']),
schema=sqlglot_metadata,
)

out = expression.sql(dialect=exp['dialect'], pretty=True)
print("\n\n" + out)
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