summaryrefslogtreecommitdiffstats
path: root/sqlglot/dialects/tsql.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 08:11:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 08:12:02 +0000
commit8d36f5966675e23bee7026ba37ae0647fbf47300 (patch)
treedf4227bbb3b07cb70df87237bcff03c8efd7822d /sqlglot/dialects/tsql.py
parentReleasing debian version 22.2.0-1. (diff)
downloadsqlglot-8d36f5966675e23bee7026ba37ae0647fbf47300.tar.xz
sqlglot-8d36f5966675e23bee7026ba37ae0647fbf47300.zip
Merging upstream version 23.7.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/dialects/tsql.py')
-rw-r--r--sqlglot/dialects/tsql.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/sqlglot/dialects/tsql.py b/sqlglot/dialects/tsql.py
index b6f491f..8e06be6 100644
--- a/sqlglot/dialects/tsql.py
+++ b/sqlglot/dialects/tsql.py
@@ -10,6 +10,7 @@ from sqlglot.dialects.dialect import (
NormalizationStrategy,
any_value_to_max_sql,
date_delta_sql,
+ datestrtodate_sql,
generatedasidentitycolumnconstraint_sql,
max_or_greatest,
min_or_least,
@@ -724,6 +725,7 @@ class TSQL(Dialect):
TABLESAMPLE_SEED_KEYWORD = "REPEATABLE"
SUPPORTS_SELECT_INTO = True
JSON_PATH_BRACKETED_KEY_SUPPORTED = False
+ SUPPORTS_TO_NUMBER = False
EXPRESSIONS_WITHOUT_NESTED_CTES = {
exp.Delete,
@@ -760,12 +762,14 @@ class TSQL(Dialect):
TRANSFORMS = {
**generator.Generator.TRANSFORMS,
exp.AnyValue: any_value_to_max_sql,
+ exp.ArrayToString: rename_func("STRING_AGG"),
exp.AutoIncrementColumnConstraint: lambda *_: "IDENTITY",
exp.DateAdd: date_delta_sql("DATEADD"),
exp.DateDiff: date_delta_sql("DATEDIFF"),
exp.CTE: transforms.preprocess([qualify_derived_table_outputs]),
exp.CurrentDate: rename_func("GETDATE"),
exp.CurrentTimestamp: rename_func("GETDATE"),
+ exp.DateStrToDate: datestrtodate_sql,
exp.Extract: rename_func("DATEPART"),
exp.GeneratedAsIdentityColumnConstraint: generatedasidentitycolumnconstraint_sql,
exp.GroupConcat: _string_agg_sql,
@@ -808,6 +812,22 @@ class TSQL(Dialect):
exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
}
+ def select_sql(self, expression: exp.Select) -> str:
+ if expression.args.get("offset"):
+ if not expression.args.get("order"):
+ # ORDER BY is required in order to use OFFSET in a query, so we use
+ # a noop order by, since we don't really care about the order.
+ # See: https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
+ expression.order_by(exp.select(exp.null()).subquery(), copy=False)
+
+ limit = expression.args.get("limit")
+ if isinstance(limit, exp.Limit):
+ # TOP and OFFSET can't be combined, we need use FETCH instead of TOP
+ # we replace here because otherwise TOP would be generated in select_sql
+ limit.replace(exp.Fetch(direction="FIRST", count=limit.expression))
+
+ return super().select_sql(expression)
+
def convert_sql(self, expression: exp.Convert) -> str:
name = "TRY_CONVERT" if expression.args.get("safe") else "CONVERT"
return self.func(
@@ -862,12 +882,12 @@ class TSQL(Dialect):
return rename_func("DATETIMEFROMPARTS")(self, expression)
- def set_operation(self, expression: exp.Union, op: str) -> str:
+ def set_operations(self, expression: exp.Union) -> str:
limit = expression.args.get("limit")
if limit:
return self.sql(expression.limit(limit.pop(), copy=False))
- return super().set_operation(expression, op)
+ return super().set_operations(expression)
def setitem_sql(self, expression: exp.SetItem) -> str:
this = expression.this