Edit on GitHub

sqlglot expressions - date, time, and timestamp functions.

  1"""sqlglot expressions - date, time, and timestamp functions."""
  2
  3from __future__ import annotations
  4
  5import typing as t
  6
  7from sqlglot.expressions.core import (
  8    Expression,
  9    Func,
 10    TimeUnit,
 11    IntervalOp,
 12    Literal,
 13    Column,
 14    TIMESTAMP_PARTS,
 15)
 16from sqlglot.expressions.datatypes import DataType, DType
 17
 18if t.TYPE_CHECKING:
 19    from sqlglot.expressions.core import Expr, Neg
 20
 21
 22# Current date/time
 23
 24
 25class CurrentDate(Expression, Func):
 26    arg_types = {"this": False}
 27
 28
 29class CurrentDatetime(Expression, Func):
 30    arg_types = {"this": False}
 31
 32
 33class CurrentTime(Expression, Func):
 34    arg_types = {"this": False}
 35
 36
 37class CurrentTimestamp(Expression, Func):
 38    arg_types = {"this": False, "sysdate": False}
 39
 40
 41class CurrentTimestampLTZ(Expression, Func):
 42    arg_types = {}
 43
 44
 45class CurrentTimezone(Expression, Func):
 46    arg_types = {}
 47
 48
 49class Localtime(Expression, Func):
 50    arg_types = {"this": False}
 51
 52
 53class Localtimestamp(Expression, Func):
 54    arg_types = {"this": False}
 55
 56
 57class Systimestamp(Expression, Func):
 58    arg_types = {"this": False}
 59
 60
 61class UtcDate(Expression, Func):
 62    arg_types = {}
 63
 64
 65class UtcTime(Expression, Func):
 66    arg_types = {"this": False}
 67
 68
 69class UtcTimestamp(Expression, Func):
 70    arg_types = {"this": False}
 71
 72
 73# Date arithmetic
 74
 75
 76class AddMonths(Expression, Func):
 77    arg_types = {"this": True, "expression": True, "preserve_end_of_month": False}
 78
 79
 80class DateAdd(Expression, Func, IntervalOp):
 81    arg_types = {"this": True, "expression": True, "unit": False}
 82
 83
 84class DateBin(Expression, Func, IntervalOp):
 85    arg_types = {"this": True, "expression": True, "unit": False, "zone": False, "origin": False}
 86
 87
 88class DateDiff(Expression, Func, TimeUnit):
 89    _sql_names = ["DATEDIFF", "DATE_DIFF"]
 90    arg_types = {
 91        "this": True,
 92        "expression": True,
 93        "unit": False,
 94        "zone": False,
 95        "big_int": False,
 96        "date_part_boundary": False,
 97    }
 98
 99
100class DateSub(Expression, Func, IntervalOp):
101    arg_types = {"this": True, "expression": True, "unit": False}
102
103
104class DatetimeAdd(Expression, Func, IntervalOp):
105    arg_types = {"this": True, "expression": True, "unit": False}
106
107
108class DatetimeDiff(Expression, Func, TimeUnit):
109    arg_types = {"this": True, "expression": True, "unit": False}
110
111
112class DatetimeSub(Expression, Func, IntervalOp):
113    arg_types = {"this": True, "expression": True, "unit": False}
114
115
116class MonthsBetween(Expression, Func):
117    arg_types = {"this": True, "expression": True, "roundoff": False}
118
119
120class TimeAdd(Expression, Func, TimeUnit):
121    arg_types = {"this": True, "expression": True, "unit": False}
122
123
124class TimeDiff(Expression, Func, TimeUnit):
125    arg_types = {"this": True, "expression": True, "unit": False}
126
127
128class TimeSub(Expression, Func, TimeUnit):
129    arg_types = {"this": True, "expression": True, "unit": False}
130
131
132class TimestampAdd(Expression, Func, TimeUnit):
133    arg_types = {"this": True, "expression": True, "unit": False}
134
135
136class TimestampDiff(Expression, Func, TimeUnit):
137    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
138    arg_types = {"this": True, "expression": True, "unit": False}
139
140
141class TimestampSub(Expression, Func, TimeUnit):
142    arg_types = {"this": True, "expression": True, "unit": False}
143
144
145class TsOrDsAdd(Expression, Func, TimeUnit):
146    # return_type is used to correctly cast the arguments of this expression when transpiling it
147    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
148
149    @property
150    def return_type(self) -> DataType:
151        return DataType.build(self.args.get("return_type") or DType.DATE)
152
153
154class TsOrDsDiff(Expression, Func, TimeUnit):
155    arg_types = {"this": True, "expression": True, "unit": False}
156
157
158# Truncation
159
160
161class DatetimeTrunc(Expression, Func, TimeUnit):
162    arg_types = {"this": True, "unit": True, "zone": False}
163
164
165class DateTrunc(Expression, Func):
166    arg_types = {"unit": True, "this": True, "zone": False, "input_type_preserved": False}
167
168    def __init__(self, **args):
169        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
170        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
171        unabbreviate = args.pop("unabbreviate", True)
172
173        unit = args.get("unit")
174        if isinstance(unit, TimeUnit.VAR_LIKE) and not (
175            isinstance(unit, Column) and len(unit.parts) != 1
176        ):
177            unit_name = unit.name.upper()
178            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
179                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
180
181            args["unit"] = Literal.string(unit_name)
182
183        super().__init__(**args)
184
185    @property
186    def unit(self) -> Expr:
187        return self.args["unit"]
188
189
190class TimestampTrunc(Expression, Func, TimeUnit):
191    arg_types = {"this": True, "unit": True, "zone": False, "input_type_preserved": False}
192
193
194class TimeSlice(Expression, Func, TimeUnit):
195    arg_types = {"this": True, "expression": True, "unit": True, "kind": False}
196
197
198class TimeTrunc(Expression, Func, TimeUnit):
199    arg_types = {"this": True, "unit": True, "zone": False}
200
201
202# Date/time extraction
203
204
205class Day(Expression, Func):
206    pass
207
208
209class DayOfMonth(Expression, Func):
210    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
211
212
213class DayOfWeek(Expression, Func):
214    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
215
216
217class DayOfWeekIso(Expression, Func):
218    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
219
220
221class DayOfYear(Expression, Func):
222    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
223
224
225class Dayname(Expression, Func):
226    arg_types = {"this": True, "abbreviated": False}
227
228
229class Extract(Expression, Func):
230    arg_types = {"this": True, "expression": True}
231
232
233class GetExtract(Expression, Func):
234    arg_types = {"this": True, "expression": True}
235
236
237class Hour(Expression, Func):
238    pass
239
240
241class Minute(Expression, Func):
242    pass
243
244
245class Month(Expression, Func):
246    pass
247
248
249class Monthname(Expression, Func):
250    arg_types = {"this": True, "abbreviated": False}
251
252
253class Quarter(Expression, Func):
254    pass
255
256
257class Second(Expression, Func):
258    pass
259
260
261class ToDays(Expression, Func):
262    pass
263
264
265class Week(Expression, Func):
266    arg_types = {"this": True, "mode": False}
267
268
269class WeekOfYear(Expression, Func):
270    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
271
272
273class Year(Expression, Func):
274    pass
275
276
277class YearOfWeek(Expression, Func):
278    _sql_names = ["YEAR_OF_WEEK", "YEAROFWEEK"]
279
280
281class YearOfWeekIso(Expression, Func):
282    _sql_names = ["YEAR_OF_WEEK_ISO", "YEAROFWEEKISO"]
283
284
285# Date/time construction
286
287
288class Date(Expression, Func):
289    arg_types = {"this": False, "zone": False, "expressions": False}
290    is_var_len_args = True
291
292
293class DateFromParts(Expression, Func):
294    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
295    arg_types = {"year": True, "month": False, "day": False, "allow_overflow": False}
296
297
298class DateFromUnixDate(Expression, Func):
299    pass
300
301
302class Datetime(Expression, Func):
303    arg_types = {"this": True, "expression": False}
304
305
306class GapFill(Expression, Func):
307    arg_types = {
308        "this": True,
309        "ts_column": True,
310        "bucket_width": True,
311        "partitioning_columns": False,
312        "value_columns": False,
313        "origin": False,
314        "ignore_nulls": False,
315    }
316
317
318class GenerateDateArray(Expression, Func):
319    arg_types = {"start": True, "end": True, "step": False}
320
321
322class GenerateTimestampArray(Expression, Func):
323    arg_types = {"start": True, "end": True, "step": True}
324
325
326class JustifyDays(Expression, Func):
327    pass
328
329
330class JustifyHours(Expression, Func):
331    pass
332
333
334class JustifyInterval(Expression, Func):
335    pass
336
337
338class LastDay(Expression, Func, TimeUnit):
339    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
340    arg_types = {"this": True, "unit": False}
341
342
343class MakeInterval(Expression, Func):
344    arg_types = {
345        "year": False,
346        "month": False,
347        "week": False,
348        "day": False,
349        "hour": False,
350        "minute": False,
351        "second": False,
352    }
353
354
355class NextDay(Expression, Func):
356    arg_types = {"this": True, "expression": True}
357
358
359class PreviousDay(Expression, Func):
360    arg_types = {"this": True, "expression": True}
361
362
363class Time(Expression, Func):
364    arg_types = {"this": False, "zone": False}
365
366
367class TimeFromParts(Expression, Func):
368    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
369    arg_types = {
370        "hour": True,
371        "min": True,
372        "sec": True,
373        "nano": False,
374        "fractions": False,
375        "precision": False,
376        "overflow": False,
377    }
378
379
380class Timestamp(Expression, Func):
381    arg_types = {"this": False, "zone": False, "with_tz": False}
382
383
384class TimestampFromParts(Expression, Func):
385    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
386    arg_types = {
387        **TIMESTAMP_PARTS,
388        "zone": False,
389        "milli": False,
390        "this": False,
391        "expression": False,
392    }
393
394
395class TimestampLtzFromParts(Expression, Func):
396    _sql_names = ["TIMESTAMP_LTZ_FROM_PARTS", "TIMESTAMPLTZFROMPARTS"]
397    arg_types = TIMESTAMP_PARTS.copy()
398
399
400class TimestampTzFromParts(Expression, Func):
401    _sql_names = ["TIMESTAMP_TZ_FROM_PARTS", "TIMESTAMPTZFROMPARTS"]
402    arg_types = {
403        **TIMESTAMP_PARTS,
404        "zone": False,
405    }
406
407
408# Date/time conversion
409
410
411class ConvertTimezone(Expression, Func):
412    arg_types = {
413        "source_tz": False,
414        "target_tz": True,
415        "timestamp": True,
416        "options": False,
417    }
418
419
420class DateStrToDate(Expression, Func):
421    pass
422
423
424class DateToDateStr(Expression, Func):
425    pass
426
427
428class DateToDi(Expression, Func):
429    pass
430
431
432class DiToDate(Expression, Func):
433    pass
434
435
436class FromISO8601Timestamp(Expression, Func):
437    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
438
439
440class ParseDatetime(Expression, Func):
441    arg_types = {"this": True, "format": False, "zone": False}
442
443
444class ParseTime(Expression, Func):
445    arg_types = {"this": True, "format": True}
446
447
448class StrToDate(Expression, Func):
449    arg_types = {"this": True, "format": False, "safe": False}
450
451
452class StrToTime(Expression, Func):
453    arg_types = {"this": True, "format": True, "zone": False, "safe": False, "target_type": False}
454
455
456class StrToUnix(Expression, Func):
457    arg_types = {"this": False, "format": False}
458
459
460class TimeStrToDate(Expression, Func):
461    pass
462
463
464class TimeStrToTime(Expression, Func):
465    arg_types = {"this": True, "zone": False}
466
467
468class TimeStrToUnix(Expression, Func):
469    pass
470
471
472class TimeToStr(Expression, Func):
473    arg_types = {"this": True, "format": True, "culture": False, "zone": False}
474
475
476class TimeToTimeStr(Expression, Func):
477    pass
478
479
480class TimeToUnix(Expression, Func):
481    pass
482
483
484class TsOrDiToDi(Expression, Func):
485    pass
486
487
488class TsOrDsToDate(Expression, Func):
489    arg_types = {"this": True, "format": False, "safe": False}
490
491
492class TsOrDsToDateStr(Expression, Func):
493    pass
494
495
496class TsOrDsToDatetime(Expression, Func):
497    pass
498
499
500class TsOrDsToTime(Expression, Func):
501    arg_types = {"this": True, "format": False, "safe": False}
502
503
504class TsOrDsToTimestamp(Expression, Func):
505    pass
506
507
508class UnixDate(Expression, Func):
509    pass
510
511
512class UnixMicros(Expression, Func):
513    pass
514
515
516class UnixMillis(Expression, Func):
517    pass
518
519
520class UnixSeconds(Expression, Func):
521    pass
522
523
524class UnixToStr(Expression, Func):
525    arg_types = {"this": True, "format": False}
526
527
528class UnixToTime(Expression, Func):
529    arg_types = {
530        "this": True,
531        "scale": False,
532        "zone": False,
533        "hours": False,
534        "minutes": False,
535        "format": False,
536        "target_type": False,
537    }
538
539    SECONDS: t.ClassVar[Literal | Neg] = Literal.number(0)
540    DECIS: t.ClassVar[Literal | Neg] = Literal.number(1)
541    CENTIS: t.ClassVar[Literal | Neg] = Literal.number(2)
542    MILLIS: t.ClassVar[Literal | Neg] = Literal.number(3)
543    DECIMILLIS: t.ClassVar[Literal | Neg] = Literal.number(4)
544    CENTIMILLIS: t.ClassVar[Literal | Neg] = Literal.number(5)
545    MICROS: t.ClassVar[Literal | Neg] = Literal.number(6)
546    DECIMICROS: t.ClassVar[Literal | Neg] = Literal.number(7)
547    CENTIMICROS: t.ClassVar[Literal | Neg] = Literal.number(8)
548    NANOS: t.ClassVar[Literal | Neg] = Literal.number(9)
549
550
551class UnixToTimeStr(Expression, Func):
552    pass
26class CurrentDate(Expression, Func):
27    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'currentdate'
required_args: 't.ClassVar[set[str]]' = set()
30class CurrentDatetime(Expression, Func):
31    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'currentdatetime'
required_args: 't.ClassVar[set[str]]' = set()
34class CurrentTime(Expression, Func):
35    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'currenttime'
required_args: 't.ClassVar[set[str]]' = set()
38class CurrentTimestamp(Expression, Func):
39    arg_types = {"this": False, "sysdate": False}
arg_types = {'this': False, 'sysdate': False}
key: ClassVar[str] = 'currenttimestamp'
required_args: 't.ClassVar[set[str]]' = set()
class CurrentTimestampLTZ(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
42class CurrentTimestampLTZ(Expression, Func):
43    arg_types = {}
arg_types = {}
key: ClassVar[str] = 'currenttimestampltz'
required_args: 't.ClassVar[set[str]]' = set()
46class CurrentTimezone(Expression, Func):
47    arg_types = {}
arg_types = {}
key: ClassVar[str] = 'currenttimezone'
required_args: 't.ClassVar[set[str]]' = set()
50class Localtime(Expression, Func):
51    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'localtime'
required_args: 't.ClassVar[set[str]]' = set()
54class Localtimestamp(Expression, Func):
55    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'localtimestamp'
required_args: 't.ClassVar[set[str]]' = set()
58class Systimestamp(Expression, Func):
59    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'systimestamp'
required_args: 't.ClassVar[set[str]]' = set()
62class UtcDate(Expression, Func):
63    arg_types = {}
arg_types = {}
key: ClassVar[str] = 'utcdate'
required_args: 't.ClassVar[set[str]]' = set()
66class UtcTime(Expression, Func):
67    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'utctime'
required_args: 't.ClassVar[set[str]]' = set()
70class UtcTimestamp(Expression, Func):
71    arg_types = {"this": False}
arg_types = {'this': False}
key: ClassVar[str] = 'utctimestamp'
required_args: 't.ClassVar[set[str]]' = set()
77class AddMonths(Expression, Func):
78    arg_types = {"this": True, "expression": True, "preserve_end_of_month": False}
arg_types = {'this': True, 'expression': True, 'preserve_end_of_month': False}
key: ClassVar[str] = 'addmonths'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
81class DateAdd(Expression, Func, IntervalOp):
82    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'dateadd'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
85class DateBin(Expression, Func, IntervalOp):
86    arg_types = {"this": True, "expression": True, "unit": False, "zone": False, "origin": False}
arg_types = {'this': True, 'expression': True, 'unit': False, 'zone': False, 'origin': False}
key: ClassVar[str] = 'datebin'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
89class DateDiff(Expression, Func, TimeUnit):
90    _sql_names = ["DATEDIFF", "DATE_DIFF"]
91    arg_types = {
92        "this": True,
93        "expression": True,
94        "unit": False,
95        "zone": False,
96        "big_int": False,
97        "date_part_boundary": False,
98    }
arg_types = {'this': True, 'expression': True, 'unit': False, 'zone': False, 'big_int': False, 'date_part_boundary': False}
key: ClassVar[str] = 'datediff'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
101class DateSub(Expression, Func, IntervalOp):
102    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'datesub'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
105class DatetimeAdd(Expression, Func, IntervalOp):
106    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'datetimeadd'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
109class DatetimeDiff(Expression, Func, TimeUnit):
110    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'datetimediff'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
113class DatetimeSub(Expression, Func, IntervalOp):
114    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'datetimesub'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
117class MonthsBetween(Expression, Func):
118    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key: ClassVar[str] = 'monthsbetween'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
121class TimeAdd(Expression, Func, TimeUnit):
122    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timeadd'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
125class TimeDiff(Expression, Func, TimeUnit):
126    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timediff'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
129class TimeSub(Expression, Func, TimeUnit):
130    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timesub'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
133class TimestampAdd(Expression, Func, TimeUnit):
134    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timestampadd'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
137class TimestampDiff(Expression, Func, TimeUnit):
138    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
139    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timestampdiff'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
142class TimestampSub(Expression, Func, TimeUnit):
143    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'timestampsub'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
146class TsOrDsAdd(Expression, Func, TimeUnit):
147    # return_type is used to correctly cast the arguments of this expression when transpiling it
148    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
149
150    @property
151    def return_type(self) -> DataType:
152        return DataType.build(self.args.get("return_type") or DType.DATE)
arg_types = {'this': True, 'expression': True, 'unit': False, 'return_type': False}
return_type: sqlglot.expressions.datatypes.DataType
150    @property
151    def return_type(self) -> DataType:
152        return DataType.build(self.args.get("return_type") or DType.DATE)
key: ClassVar[str] = 'tsordsadd'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
155class TsOrDsDiff(Expression, Func, TimeUnit):
156    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key: ClassVar[str] = 'tsordsdiff'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
162class DatetimeTrunc(Expression, Func, TimeUnit):
163    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key: ClassVar[str] = 'datetimetrunc'
required_args: 't.ClassVar[set[str]]' = {'unit', 'this'}
166class DateTrunc(Expression, Func):
167    arg_types = {"unit": True, "this": True, "zone": False, "input_type_preserved": False}
168
169    def __init__(self, **args):
170        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
171        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
172        unabbreviate = args.pop("unabbreviate", True)
173
174        unit = args.get("unit")
175        if isinstance(unit, TimeUnit.VAR_LIKE) and not (
176            isinstance(unit, Column) and len(unit.parts) != 1
177        ):
178            unit_name = unit.name.upper()
179            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
180                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
181
182            args["unit"] = Literal.string(unit_name)
183
184        super().__init__(**args)
185
186    @property
187    def unit(self) -> Expr:
188        return self.args["unit"]
DateTrunc(**args)
169    def __init__(self, **args):
170        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
171        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
172        unabbreviate = args.pop("unabbreviate", True)
173
174        unit = args.get("unit")
175        if isinstance(unit, TimeUnit.VAR_LIKE) and not (
176            isinstance(unit, Column) and len(unit.parts) != 1
177        ):
178            unit_name = unit.name.upper()
179            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
180                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
181
182            args["unit"] = Literal.string(unit_name)
183
184        super().__init__(**args)
arg_types = {'unit': True, 'this': True, 'zone': False, 'input_type_preserved': False}
unit: sqlglot.expressions.core.Expr
186    @property
187    def unit(self) -> Expr:
188        return self.args["unit"]
key: ClassVar[str] = 'datetrunc'
required_args: 't.ClassVar[set[str]]' = {'unit', 'this'}
191class TimestampTrunc(Expression, Func, TimeUnit):
192    arg_types = {"this": True, "unit": True, "zone": False, "input_type_preserved": False}
arg_types = {'this': True, 'unit': True, 'zone': False, 'input_type_preserved': False}
key: ClassVar[str] = 'timestamptrunc'
required_args: 't.ClassVar[set[str]]' = {'unit', 'this'}
195class TimeSlice(Expression, Func, TimeUnit):
196    arg_types = {"this": True, "expression": True, "unit": True, "kind": False}
arg_types = {'this': True, 'expression': True, 'unit': True, 'kind': False}
key: ClassVar[str] = 'timeslice'
required_args: 't.ClassVar[set[str]]' = {'unit', 'expression', 'this'}
199class TimeTrunc(Expression, Func, TimeUnit):
200    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key: ClassVar[str] = 'timetrunc'
required_args: 't.ClassVar[set[str]]' = {'unit', 'this'}
206class Day(Expression, Func):
207    pass
key: ClassVar[str] = 'day'
required_args: 't.ClassVar[set[str]]' = {'this'}
210class DayOfMonth(Expression, Func):
211    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key: ClassVar[str] = 'dayofmonth'
required_args: 't.ClassVar[set[str]]' = {'this'}
214class DayOfWeek(Expression, Func):
215    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key: ClassVar[str] = 'dayofweek'
required_args: 't.ClassVar[set[str]]' = {'this'}
218class DayOfWeekIso(Expression, Func):
219    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
key: ClassVar[str] = 'dayofweekiso'
required_args: 't.ClassVar[set[str]]' = {'this'}
222class DayOfYear(Expression, Func):
223    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key: ClassVar[str] = 'dayofyear'
required_args: 't.ClassVar[set[str]]' = {'this'}
226class Dayname(Expression, Func):
227    arg_types = {"this": True, "abbreviated": False}
arg_types = {'this': True, 'abbreviated': False}
key: ClassVar[str] = 'dayname'
required_args: 't.ClassVar[set[str]]' = {'this'}
230class Extract(Expression, Func):
231    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'extract'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
234class GetExtract(Expression, Func):
235    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'getextract'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
238class Hour(Expression, Func):
239    pass
key: ClassVar[str] = 'hour'
required_args: 't.ClassVar[set[str]]' = {'this'}
242class Minute(Expression, Func):
243    pass
key: ClassVar[str] = 'minute'
required_args: 't.ClassVar[set[str]]' = {'this'}
246class Month(Expression, Func):
247    pass
key: ClassVar[str] = 'month'
required_args: 't.ClassVar[set[str]]' = {'this'}
250class Monthname(Expression, Func):
251    arg_types = {"this": True, "abbreviated": False}
arg_types = {'this': True, 'abbreviated': False}
key: ClassVar[str] = 'monthname'
required_args: 't.ClassVar[set[str]]' = {'this'}
254class Quarter(Expression, Func):
255    pass
key: ClassVar[str] = 'quarter'
required_args: 't.ClassVar[set[str]]' = {'this'}
258class Second(Expression, Func):
259    pass
key: ClassVar[str] = 'second'
required_args: 't.ClassVar[set[str]]' = {'this'}
262class ToDays(Expression, Func):
263    pass
key: ClassVar[str] = 'todays'
required_args: 't.ClassVar[set[str]]' = {'this'}
266class Week(Expression, Func):
267    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key: ClassVar[str] = 'week'
required_args: 't.ClassVar[set[str]]' = {'this'}
270class WeekOfYear(Expression, Func):
271    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key: ClassVar[str] = 'weekofyear'
required_args: 't.ClassVar[set[str]]' = {'this'}
274class Year(Expression, Func):
275    pass
key: ClassVar[str] = 'year'
required_args: 't.ClassVar[set[str]]' = {'this'}
278class YearOfWeek(Expression, Func):
279    _sql_names = ["YEAR_OF_WEEK", "YEAROFWEEK"]
key: ClassVar[str] = 'yearofweek'
required_args: 't.ClassVar[set[str]]' = {'this'}
282class YearOfWeekIso(Expression, Func):
283    _sql_names = ["YEAR_OF_WEEK_ISO", "YEAROFWEEKISO"]
key: ClassVar[str] = 'yearofweekiso'
required_args: 't.ClassVar[set[str]]' = {'this'}
289class Date(Expression, Func):
290    arg_types = {"this": False, "zone": False, "expressions": False}
291    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key: ClassVar[str] = 'date'
required_args: 't.ClassVar[set[str]]' = set()
294class DateFromParts(Expression, Func):
295    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
296    arg_types = {"year": True, "month": False, "day": False, "allow_overflow": False}
arg_types = {'year': True, 'month': False, 'day': False, 'allow_overflow': False}
key: ClassVar[str] = 'datefromparts'
required_args: 't.ClassVar[set[str]]' = {'year'}
299class DateFromUnixDate(Expression, Func):
300    pass
key: ClassVar[str] = 'datefromunixdate'
required_args: 't.ClassVar[set[str]]' = {'this'}
303class Datetime(Expression, Func):
304    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key: ClassVar[str] = 'datetime'
required_args: 't.ClassVar[set[str]]' = {'this'}
307class GapFill(Expression, Func):
308    arg_types = {
309        "this": True,
310        "ts_column": True,
311        "bucket_width": True,
312        "partitioning_columns": False,
313        "value_columns": False,
314        "origin": False,
315        "ignore_nulls": False,
316    }
arg_types = {'this': True, 'ts_column': True, 'bucket_width': True, 'partitioning_columns': False, 'value_columns': False, 'origin': False, 'ignore_nulls': False}
key: ClassVar[str] = 'gapfill'
required_args: 't.ClassVar[set[str]]' = {'bucket_width', 'this', 'ts_column'}
319class GenerateDateArray(Expression, Func):
320    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key: ClassVar[str] = 'generatedatearray'
required_args: 't.ClassVar[set[str]]' = {'start', 'end'}
class GenerateTimestampArray(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
323class GenerateTimestampArray(Expression, Func):
324    arg_types = {"start": True, "end": True, "step": True}
arg_types = {'start': True, 'end': True, 'step': True}
key: ClassVar[str] = 'generatetimestamparray'
required_args: 't.ClassVar[set[str]]' = {'step', 'start', 'end'}
327class JustifyDays(Expression, Func):
328    pass
key: ClassVar[str] = 'justifydays'
required_args: 't.ClassVar[set[str]]' = {'this'}
331class JustifyHours(Expression, Func):
332    pass
key: ClassVar[str] = 'justifyhours'
required_args: 't.ClassVar[set[str]]' = {'this'}
335class JustifyInterval(Expression, Func):
336    pass
key: ClassVar[str] = 'justifyinterval'
required_args: 't.ClassVar[set[str]]' = {'this'}
339class LastDay(Expression, Func, TimeUnit):
340    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
341    arg_types = {"this": True, "unit": False}
arg_types = {'this': True, 'unit': False}
key: ClassVar[str] = 'lastday'
required_args: 't.ClassVar[set[str]]' = {'this'}
344class MakeInterval(Expression, Func):
345    arg_types = {
346        "year": False,
347        "month": False,
348        "week": False,
349        "day": False,
350        "hour": False,
351        "minute": False,
352        "second": False,
353    }
arg_types = {'year': False, 'month': False, 'week': False, 'day': False, 'hour': False, 'minute': False, 'second': False}
key: ClassVar[str] = 'makeinterval'
required_args: 't.ClassVar[set[str]]' = set()
356class NextDay(Expression, Func):
357    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'nextday'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
360class PreviousDay(Expression, Func):
361    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key: ClassVar[str] = 'previousday'
required_args: 't.ClassVar[set[str]]' = {'expression', 'this'}
364class Time(Expression, Func):
365    arg_types = {"this": False, "zone": False}
arg_types = {'this': False, 'zone': False}
key: ClassVar[str] = 'time'
required_args: 't.ClassVar[set[str]]' = set()
368class TimeFromParts(Expression, Func):
369    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
370    arg_types = {
371        "hour": True,
372        "min": True,
373        "sec": True,
374        "nano": False,
375        "fractions": False,
376        "precision": False,
377        "overflow": False,
378    }
arg_types = {'hour': True, 'min': True, 'sec': True, 'nano': False, 'fractions': False, 'precision': False, 'overflow': False}
key: ClassVar[str] = 'timefromparts'
required_args: 't.ClassVar[set[str]]' = {'sec', 'min', 'hour'}
381class Timestamp(Expression, Func):
382    arg_types = {"this": False, "zone": False, "with_tz": False}
arg_types = {'this': False, 'zone': False, 'with_tz': False}
key: ClassVar[str] = 'timestamp'
required_args: 't.ClassVar[set[str]]' = set()
class TimestampFromParts(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
385class TimestampFromParts(Expression, Func):
386    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
387    arg_types = {
388        **TIMESTAMP_PARTS,
389        "zone": False,
390        "milli": False,
391        "this": False,
392        "expression": False,
393    }
arg_types = {'year': False, 'month': False, 'day': False, 'hour': False, 'min': False, 'sec': False, 'nano': False, 'zone': False, 'milli': False, 'this': False, 'expression': False}
key: ClassVar[str] = 'timestampfromparts'
required_args: 't.ClassVar[set[str]]' = set()
class TimestampLtzFromParts(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
396class TimestampLtzFromParts(Expression, Func):
397    _sql_names = ["TIMESTAMP_LTZ_FROM_PARTS", "TIMESTAMPLTZFROMPARTS"]
398    arg_types = TIMESTAMP_PARTS.copy()
arg_types = {'year': False, 'month': False, 'day': False, 'hour': False, 'min': False, 'sec': False, 'nano': False}
key: ClassVar[str] = 'timestampltzfromparts'
required_args: 't.ClassVar[set[str]]' = set()
class TimestampTzFromParts(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
401class TimestampTzFromParts(Expression, Func):
402    _sql_names = ["TIMESTAMP_TZ_FROM_PARTS", "TIMESTAMPTZFROMPARTS"]
403    arg_types = {
404        **TIMESTAMP_PARTS,
405        "zone": False,
406    }
arg_types = {'year': False, 'month': False, 'day': False, 'hour': False, 'min': False, 'sec': False, 'nano': False, 'zone': False}
key: ClassVar[str] = 'timestamptzfromparts'
required_args: 't.ClassVar[set[str]]' = set()
412class ConvertTimezone(Expression, Func):
413    arg_types = {
414        "source_tz": False,
415        "target_tz": True,
416        "timestamp": True,
417        "options": False,
418    }
arg_types = {'source_tz': False, 'target_tz': True, 'timestamp': True, 'options': False}
key: ClassVar[str] = 'converttimezone'
required_args: 't.ClassVar[set[str]]' = {'target_tz', 'timestamp'}
421class DateStrToDate(Expression, Func):
422    pass
key: ClassVar[str] = 'datestrtodate'
required_args: 't.ClassVar[set[str]]' = {'this'}
425class DateToDateStr(Expression, Func):
426    pass
key: ClassVar[str] = 'datetodatestr'
required_args: 't.ClassVar[set[str]]' = {'this'}
429class DateToDi(Expression, Func):
430    pass
key: ClassVar[str] = 'datetodi'
required_args: 't.ClassVar[set[str]]' = {'this'}
433class DiToDate(Expression, Func):
434    pass
key: ClassVar[str] = 'ditodate'
required_args: 't.ClassVar[set[str]]' = {'this'}
class FromISO8601Timestamp(sqlglot.expressions.core.Expression, sqlglot.expressions.core.Func):
437class FromISO8601Timestamp(Expression, Func):
438    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
key: ClassVar[str] = 'fromiso8601timestamp'
required_args: 't.ClassVar[set[str]]' = {'this'}
441class ParseDatetime(Expression, Func):
442    arg_types = {"this": True, "format": False, "zone": False}
arg_types = {'this': True, 'format': False, 'zone': False}
key: ClassVar[str] = 'parsedatetime'
required_args: 't.ClassVar[set[str]]' = {'this'}
445class ParseTime(Expression, Func):
446    arg_types = {"this": True, "format": True}
arg_types = {'this': True, 'format': True}
key: ClassVar[str] = 'parsetime'
required_args: 't.ClassVar[set[str]]' = {'format', 'this'}
449class StrToDate(Expression, Func):
450    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key: ClassVar[str] = 'strtodate'
required_args: 't.ClassVar[set[str]]' = {'this'}
453class StrToTime(Expression, Func):
454    arg_types = {"this": True, "format": True, "zone": False, "safe": False, "target_type": False}
arg_types = {'this': True, 'format': True, 'zone': False, 'safe': False, 'target_type': False}
key: ClassVar[str] = 'strtotime'
required_args: 't.ClassVar[set[str]]' = {'format', 'this'}
457class StrToUnix(Expression, Func):
458    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key: ClassVar[str] = 'strtounix'
required_args: 't.ClassVar[set[str]]' = set()
461class TimeStrToDate(Expression, Func):
462    pass
key: ClassVar[str] = 'timestrtodate'
required_args: 't.ClassVar[set[str]]' = {'this'}
465class TimeStrToTime(Expression, Func):
466    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key: ClassVar[str] = 'timestrtotime'
required_args: 't.ClassVar[set[str]]' = {'this'}
469class TimeStrToUnix(Expression, Func):
470    pass
key: ClassVar[str] = 'timestrtounix'
required_args: 't.ClassVar[set[str]]' = {'this'}
473class TimeToStr(Expression, Func):
474    arg_types = {"this": True, "format": True, "culture": False, "zone": False}
arg_types = {'this': True, 'format': True, 'culture': False, 'zone': False}
key: ClassVar[str] = 'timetostr'
required_args: 't.ClassVar[set[str]]' = {'format', 'this'}
477class TimeToTimeStr(Expression, Func):
478    pass
key: ClassVar[str] = 'timetotimestr'
required_args: 't.ClassVar[set[str]]' = {'this'}
481class TimeToUnix(Expression, Func):
482    pass
key: ClassVar[str] = 'timetounix'
required_args: 't.ClassVar[set[str]]' = {'this'}
485class TsOrDiToDi(Expression, Func):
486    pass
key: ClassVar[str] = 'tsorditodi'
required_args: 't.ClassVar[set[str]]' = {'this'}
489class TsOrDsToDate(Expression, Func):
490    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key: ClassVar[str] = 'tsordstodate'
required_args: 't.ClassVar[set[str]]' = {'this'}
493class TsOrDsToDateStr(Expression, Func):
494    pass
key: ClassVar[str] = 'tsordstodatestr'
required_args: 't.ClassVar[set[str]]' = {'this'}
497class TsOrDsToDatetime(Expression, Func):
498    pass
key: ClassVar[str] = 'tsordstodatetime'
required_args: 't.ClassVar[set[str]]' = {'this'}
501class TsOrDsToTime(Expression, Func):
502    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key: ClassVar[str] = 'tsordstotime'
required_args: 't.ClassVar[set[str]]' = {'this'}
505class TsOrDsToTimestamp(Expression, Func):
506    pass
key: ClassVar[str] = 'tsordstotimestamp'
required_args: 't.ClassVar[set[str]]' = {'this'}
509class UnixDate(Expression, Func):
510    pass
key: ClassVar[str] = 'unixdate'
required_args: 't.ClassVar[set[str]]' = {'this'}
513class UnixMicros(Expression, Func):
514    pass
key: ClassVar[str] = 'unixmicros'
required_args: 't.ClassVar[set[str]]' = {'this'}
517class UnixMillis(Expression, Func):
518    pass
key: ClassVar[str] = 'unixmillis'
required_args: 't.ClassVar[set[str]]' = {'this'}
521class UnixSeconds(Expression, Func):
522    pass
key: ClassVar[str] = 'unixseconds'
required_args: 't.ClassVar[set[str]]' = {'this'}
525class UnixToStr(Expression, Func):
526    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key: ClassVar[str] = 'unixtostr'
required_args: 't.ClassVar[set[str]]' = {'this'}
529class UnixToTime(Expression, Func):
530    arg_types = {
531        "this": True,
532        "scale": False,
533        "zone": False,
534        "hours": False,
535        "minutes": False,
536        "format": False,
537        "target_type": False,
538    }
539
540    SECONDS: t.ClassVar[Literal | Neg] = Literal.number(0)
541    DECIS: t.ClassVar[Literal | Neg] = Literal.number(1)
542    CENTIS: t.ClassVar[Literal | Neg] = Literal.number(2)
543    MILLIS: t.ClassVar[Literal | Neg] = Literal.number(3)
544    DECIMILLIS: t.ClassVar[Literal | Neg] = Literal.number(4)
545    CENTIMILLIS: t.ClassVar[Literal | Neg] = Literal.number(5)
546    MICROS: t.ClassVar[Literal | Neg] = Literal.number(6)
547    DECIMICROS: t.ClassVar[Literal | Neg] = Literal.number(7)
548    CENTIMICROS: t.ClassVar[Literal | Neg] = Literal.number(8)
549    NANOS: t.ClassVar[Literal | Neg] = Literal.number(9)
arg_types = {'this': True, 'scale': False, 'zone': False, 'hours': False, 'minutes': False, 'format': False, 'target_type': False}
SECONDS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=0, is_string=False)
DECIS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=1, is_string=False)
CENTIS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=2, is_string=False)
MILLIS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=3, is_string=False)
DECIMILLIS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=4, is_string=False)
CENTIMILLIS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=5, is_string=False)
MICROS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=6, is_string=False)
DECIMICROS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=7, is_string=False)
CENTIMICROS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=8, is_string=False)
NANOS: ClassVar[sqlglot.expressions.core.Literal | sqlglot.expressions.core.Neg] = Literal(this=9, is_string=False)
key: ClassVar[str] = 'unixtotime'
required_args: 't.ClassVar[set[str]]' = {'this'}
552class UnixToTimeStr(Expression, Func):
553    pass
key: ClassVar[str] = 'unixtotimestr'
required_args: 't.ClassVar[set[str]]' = {'this'}