diff options
Diffstat (limited to 'sqlglot/dialects/tsql.py')
-rw-r--r-- | sqlglot/dialects/tsql.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/sqlglot/dialects/tsql.py b/sqlglot/dialects/tsql.py index 23cf803..6500242 100644 --- a/sqlglot/dialects/tsql.py +++ b/sqlglot/dialects/tsql.py @@ -3,7 +3,7 @@ from __future__ import annotations import datetime import re import typing as t -from functools import partial +from functools import partial, reduce from sqlglot import exp, generator, parser, tokens, transforms from sqlglot.dialects.dialect import ( @@ -21,6 +21,7 @@ from sqlglot.dialects.dialect import ( timestrtotime_sql, ) from sqlglot.helper import seq_get +from sqlglot.parser import build_coalesce from sqlglot.time import format_time from sqlglot.tokens import TokenType @@ -521,6 +522,12 @@ class TSQL(Dialect): substr=seq_get(args, 0), position=seq_get(args, 2), ), + "COUNT": lambda args: exp.Count( + this=seq_get(args, 0), expressions=args[1:], big_int=False + ), + "COUNT_BIG": lambda args: exp.Count( + this=seq_get(args, 0), expressions=args[1:], big_int=True + ), "DATEADD": build_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL), "DATEDIFF": _build_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL), "DATENAME": _build_formatted_time(exp.TimeToStr, full_format_mapping=True), @@ -530,7 +537,7 @@ class TSQL(Dialect): "FORMAT": _build_format, "GETDATE": exp.CurrentTimestamp.from_arg_list, "HASHBYTES": _build_hashbytes, - "ISNULL": exp.Coalesce.from_arg_list, + "ISNULL": build_coalesce, "JSON_QUERY": _build_json_query, "JSON_VALUE": parser.build_extract_json_with_path(exp.JSONExtractScalar), "LEN": _build_with_arg_as_text(exp.Length), @@ -809,6 +816,7 @@ class TSQL(Dialect): SET_OP_MODIFIERS = False COPY_PARAMS_EQ_REQUIRED = True PARSE_JSON_NAME = None + EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE = False EXPRESSIONS_WITHOUT_NESTED_CTES = { exp.Create, @@ -1065,6 +1073,10 @@ class TSQL(Dialect): return self.prepend_ctes(expression, sql) + def count_sql(self, expression: exp.Count) -> str: + func_name = "COUNT_BIG" if expression.args.get("big_int") else "COUNT" + return rename_func(func_name)(self, expression) + def offset_sql(self, expression: exp.Offset) -> str: return f"{super().offset_sql(expression)} ROWS" @@ -1180,3 +1192,8 @@ class TSQL(Dialect): def options_modifier(self, expression: exp.Expression) -> str: options = self.expressions(expression, key="options") return f" OPTION{self.wrap(options)}" if options else "" + + def dpipe_sql(self, expression: exp.DPipe) -> str: + return self.sql( + reduce(lambda x, y: exp.Add(this=x, expression=y), expression.flatten()) + ) |