sqlglot 27.11.0__py3-none-any.whl → 27.13.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sqlglot/_version.py CHANGED
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '27.11.0'
32
- __version_tuple__ = version_tuple = (27, 11, 0)
31
+ __version__ = version = '27.13.0'
32
+ __version_tuple__ = version_tuple = (27, 13, 0)
33
33
 
34
34
  __commit_id__ = commit_id = None
@@ -310,17 +310,16 @@ def _annotate_math_functions(self: TypeAnnotator, expression: E) -> E:
310
310
  return expression
311
311
 
312
312
 
313
- def _annotate_perncentile_cont(
314
- self: TypeAnnotator, expression: exp.PercentileCont
315
- ) -> exp.PercentileCont:
313
+ def _annotate_by_args_with_coerce(self: TypeAnnotator, expression: E) -> E:
316
314
  """
317
- +------------+-----------+------------+---------+
318
- | INPUT | NUMERIC | BIGNUMERIC | FLOAT64 |
319
- +------------+-----------+------------+---------+
320
- | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
321
- | BIGNUMERIC | BIGNUMERIC| BIGNUMERIC | FLOAT64 |
322
- | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
323
- +------------+-----------+------------+---------+
315
+ +------------+------------+------------+-------------+---------+
316
+ | INPUT | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
317
+ +------------+------------+------------+-------------+---------+
318
+ | INT64 | INT64 | NUMERIC | BIGNUMERIC | FLOAT64 |
319
+ | NUMERIC | NUMERIC | NUMERIC | BIGNUMERIC | FLOAT64 |
320
+ | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | BIGNUMERIC | FLOAT64 |
321
+ | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 | FLOAT64 |
322
+ +------------+------------+------------+-------------+---------+
324
323
  """
325
324
  self._annotate_args(expression)
326
325
 
@@ -492,6 +491,7 @@ class BigQuery(Dialect):
492
491
  exp.DataType.Type.BIGDECIMAL: {exp.DataType.Type.DOUBLE},
493
492
  }
494
493
  COERCES_TO[exp.DataType.Type.DECIMAL] |= {exp.DataType.Type.BIGDECIMAL}
494
+ COERCES_TO[exp.DataType.Type.BIGINT] |= {exp.DataType.Type.BIGDECIMAL}
495
495
 
496
496
  # BigQuery maps Type.TIMESTAMP to DATETIME, so we need to amend the inferred types
497
497
  TYPE_TO_EXPRESSIONS = {
@@ -514,23 +514,47 @@ class BigQuery(Dialect):
514
514
  **{
515
515
  expr_type: lambda self, e: self._annotate_by_args(e, "this")
516
516
  for expr_type in (
517
+ exp.Abs,
518
+ exp.ArgMax,
519
+ exp.ArgMin,
520
+ exp.DateTrunc,
521
+ exp.DatetimeTrunc,
522
+ exp.FirstValue,
523
+ exp.GroupConcat,
524
+ exp.IgnoreNulls,
525
+ exp.JSONExtract,
526
+ exp.Lead,
517
527
  exp.Left,
518
- exp.Right,
519
528
  exp.Lower,
520
- exp.Upper,
529
+ exp.NthValue,
521
530
  exp.Pad,
522
- exp.Trim,
531
+ exp.PercentileDisc,
523
532
  exp.RegexpExtract,
524
533
  exp.RegexpReplace,
525
534
  exp.Repeat,
535
+ exp.Replace,
536
+ exp.RespectNulls,
537
+ exp.Reverse,
538
+ exp.Right,
539
+ exp.SafeNegate,
540
+ exp.Sign,
526
541
  exp.Substring,
542
+ exp.TimestampTrunc,
543
+ exp.Translate,
544
+ exp.Trim,
545
+ exp.Upper,
527
546
  )
528
547
  },
548
+ exp.Acos: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
549
+ exp.Acosh: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
550
+ exp.Asin: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
551
+ exp.Asinh: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
552
+ exp.Atan: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
553
+ exp.Atanh: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
554
+ exp.Atan2: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
529
555
  exp.ApproxTopSum: lambda self, e: _annotate_by_args_approx_top(self, e),
530
556
  exp.ApproxTopK: lambda self, e: _annotate_by_args_approx_top(self, e),
531
557
  exp.ApproxQuantiles: lambda self, e: self._annotate_by_args(e, "this", array=True),
532
- exp.ArgMax: lambda self, e: self._annotate_by_args(e, "this"),
533
- exp.ArgMin: lambda self, e: self._annotate_by_args(e, "this"),
534
558
  exp.Array: _annotate_array,
535
559
  exp.ArrayConcat: lambda self, e: self._annotate_by_args(e, "this", "expressions"),
536
560
  exp.Ascii: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
@@ -540,6 +564,7 @@ class BigQuery(Dialect):
540
564
  exp.BitwiseCountAgg: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
541
565
  exp.ByteLength: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
542
566
  exp.ByteString: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BINARY),
567
+ exp.Cbrt: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
543
568
  exp.CodePointsToBytes: lambda self, e: self._annotate_with_type(
544
569
  e, exp.DataType.Type.BINARY
545
570
  ),
@@ -547,16 +572,21 @@ class BigQuery(Dialect):
547
572
  e, exp.DataType.Type.VARCHAR
548
573
  ),
549
574
  exp.Concat: _annotate_concat,
550
- exp.Contains: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BOOLEAN),
551
575
  exp.Corr: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
576
+ exp.Cot: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
577
+ exp.CosineDistance: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
578
+ exp.Coth: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
552
579
  exp.CovarPop: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
553
580
  exp.CovarSamp: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
581
+ exp.Csc: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
582
+ exp.Csch: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
554
583
  exp.CumeDist: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
555
584
  exp.DateFromUnixDate: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DATE),
556
- exp.DateTrunc: lambda self, e: self._annotate_by_args(e, "this"),
557
585
  exp.DenseRank: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
586
+ exp.EuclideanDistance: lambda self, e: self._annotate_with_type(
587
+ e, exp.DataType.Type.DOUBLE
588
+ ),
558
589
  exp.FarmFingerprint: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
559
- exp.FirstValue: lambda self, e: self._annotate_by_args(e, "this"),
560
590
  exp.Unhex: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BINARY),
561
591
  exp.Float64: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
562
592
  exp.Format: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
@@ -564,7 +594,8 @@ class BigQuery(Dialect):
564
594
  e, exp.DataType.build("ARRAY<TIMESTAMP>", dialect="bigquery")
565
595
  ),
566
596
  exp.Grouping: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
567
- exp.IgnoreNulls: lambda self, e: self._annotate_by_args(e, "this"),
597
+ exp.IsInf: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BOOLEAN),
598
+ exp.IsNan: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BOOLEAN),
568
599
  exp.JSONArray: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.JSON),
569
600
  exp.JSONArrayAppend: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.JSON),
570
601
  exp.JSONArrayInsert: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.JSON),
@@ -572,9 +603,10 @@ class BigQuery(Dialect):
572
603
  exp.JSONExtractScalar: lambda self, e: self._annotate_with_type(
573
604
  e, exp.DataType.Type.VARCHAR
574
605
  ),
575
- exp.JSONExtract: lambda self, e: self._annotate_by_args(e, "this"),
576
606
  exp.JSONExtractArray: lambda self, e: self._annotate_by_args(e, "this", array=True),
577
- exp.JSONFormat: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
607
+ exp.JSONFormat: lambda self, e: self._annotate_with_type(
608
+ e, exp.DataType.Type.JSON if e.args.get("to_json") else exp.DataType.Type.VARCHAR
609
+ ),
578
610
  exp.JSONKeysAtDepth: lambda self, e: self._annotate_with_type(
579
611
  e, exp.DataType.build("ARRAY<VARCHAR>", dialect="bigquery")
580
612
  ),
@@ -587,7 +619,6 @@ class BigQuery(Dialect):
587
619
  e, exp.DataType.build("ARRAY<VARCHAR>", dialect="bigquery")
588
620
  ),
589
621
  exp.Lag: lambda self, e: self._annotate_by_args(e, "this", "default"),
590
- exp.Lead: lambda self, e: self._annotate_by_args(e, "this"),
591
622
  exp.LowerHex: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
592
623
  exp.LaxBool: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BOOLEAN),
593
624
  exp.LaxFloat64: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
@@ -595,7 +626,6 @@ class BigQuery(Dialect):
595
626
  exp.LaxString: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
596
627
  exp.MD5Digest: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BINARY),
597
628
  exp.Normalize: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
598
- exp.NthValue: lambda self, e: self._annotate_by_args(e, "this"),
599
629
  exp.Ntile: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
600
630
  exp.ParseTime: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.TIME),
601
631
  exp.ParseDatetime: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DATETIME),
@@ -603,36 +633,39 @@ class BigQuery(Dialect):
603
633
  e, exp.DataType.Type.BIGDECIMAL
604
634
  ),
605
635
  exp.ParseNumeric: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DECIMAL),
606
- exp.PercentileCont: lambda self, e: _annotate_perncentile_cont(self, e),
607
- exp.PercentileDisc: lambda self, e: self._annotate_by_args(e, "this"),
636
+ exp.PercentileCont: lambda self, e: _annotate_by_args_with_coerce(self, e),
608
637
  exp.PercentRank: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
609
638
  exp.Rank: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
639
+ exp.RangeBucket: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
610
640
  exp.RegexpExtractAll: lambda self, e: self._annotate_by_args(e, "this", array=True),
611
641
  exp.RegexpInstr: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
612
- exp.Replace: lambda self, e: self._annotate_by_args(e, "this"),
613
- exp.RespectNulls: lambda self, e: self._annotate_by_args(e, "this"),
614
- exp.Reverse: lambda self, e: self._annotate_by_args(e, "this"),
615
642
  exp.RowNumber: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
643
+ exp.Rand: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
616
644
  exp.SafeConvertBytesToString: lambda self, e: self._annotate_with_type(
617
645
  e, exp.DataType.Type.VARCHAR
618
646
  ),
647
+ exp.SafeAdd: lambda self, e: _annotate_by_args_with_coerce(self, e),
648
+ exp.SafeMultiply: lambda self, e: _annotate_by_args_with_coerce(self, e),
649
+ exp.SafeSubtract: lambda self, e: _annotate_by_args_with_coerce(self, e),
650
+ exp.Sec: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
651
+ exp.Sech: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
619
652
  exp.Soundex: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
620
653
  exp.SHA: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BINARY),
621
654
  exp.SHA2: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BINARY),
622
- exp.Sign: lambda self, e: self._annotate_by_args(e, "this"),
655
+ exp.Sin: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
656
+ exp.Sinh: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.DOUBLE),
623
657
  exp.Split: lambda self, e: self._annotate_by_args(e, "this", array=True),
624
658
  exp.TimestampFromParts: lambda self, e: self._annotate_with_type(
625
659
  e, exp.DataType.Type.DATETIME
626
660
  ),
627
- exp.TimestampTrunc: lambda self, e: self._annotate_by_args(e, "this"),
628
661
  exp.TimeFromParts: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.TIME),
629
662
  exp.TimeTrunc: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.TIME),
630
663
  exp.ToCodePoints: lambda self, e: self._annotate_with_type(
631
664
  e, exp.DataType.build("ARRAY<BIGINT>", dialect="bigquery")
632
665
  ),
633
666
  exp.TsOrDsToTime: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.TIME),
634
- exp.Translate: lambda self, e: self._annotate_by_args(e, "this"),
635
667
  exp.Unicode: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BIGINT),
668
+ exp.Uuid: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.VARCHAR),
636
669
  }
637
670
 
638
671
  def normalize_identifier(self, expression: E) -> E:
@@ -734,6 +767,10 @@ class BigQuery(Dialect):
734
767
  FUNCTIONS = {
735
768
  **parser.Parser.FUNCTIONS,
736
769
  "APPROX_TOP_COUNT": exp.ApproxTopK.from_arg_list,
770
+ "BIT_AND": exp.BitwiseAndAgg.from_arg_list,
771
+ "BIT_OR": exp.BitwiseOrAgg.from_arg_list,
772
+ "BIT_XOR": exp.BitwiseXorAgg.from_arg_list,
773
+ "BIT_COUNT": exp.BitwiseCountAgg.from_arg_list,
737
774
  "BOOL": exp.JSONBool.from_arg_list,
738
775
  "CONTAINS_SUBSTR": _build_contains_substring,
739
776
  "DATE": _build_date,
@@ -804,6 +841,9 @@ class BigQuery(Dialect):
804
841
  this=seq_get(args, 0), scale=exp.UnixToTime.MILLIS
805
842
  ),
806
843
  "TIMESTAMP_SECONDS": lambda args: exp.UnixToTime(this=seq_get(args, 0)),
844
+ "TO_JSON": lambda args: exp.JSONFormat(
845
+ this=seq_get(args, 0), options=seq_get(args, 1), to_json=True
846
+ ),
807
847
  "TO_JSON_STRING": exp.JSONFormat.from_arg_list,
808
848
  "FORMAT_DATETIME": _build_format_time(exp.TsOrDsToDatetime),
809
849
  "FORMAT_TIMESTAMP": _build_format_time(exp.TsOrDsToTimestamp),
@@ -1236,6 +1276,10 @@ class BigQuery(Dialect):
1236
1276
  exp.ArrayContains: _array_contains_sql,
1237
1277
  exp.ArrayFilter: filter_array_using_unnest,
1238
1278
  exp.ArrayRemove: filter_array_using_unnest,
1279
+ exp.BitwiseAndAgg: rename_func("BIT_AND"),
1280
+ exp.BitwiseOrAgg: rename_func("BIT_OR"),
1281
+ exp.BitwiseXorAgg: rename_func("BIT_XOR"),
1282
+ exp.BitwiseCountAgg: rename_func("BIT_COUNT"),
1239
1283
  exp.ByteLength: rename_func("BYTE_LENGTH"),
1240
1284
  exp.Cast: transforms.preprocess([transforms.remove_precision_parameterized_types]),
1241
1285
  exp.CollateProperty: lambda self, e: (
@@ -1274,7 +1318,11 @@ class BigQuery(Dialect):
1274
1318
  exp.JSONExtract: _json_extract_sql,
1275
1319
  exp.JSONExtractArray: _json_extract_sql,
1276
1320
  exp.JSONExtractScalar: _json_extract_sql,
1277
- exp.JSONFormat: rename_func("TO_JSON_STRING"),
1321
+ exp.JSONFormat: lambda self, e: self.func(
1322
+ "TO_JSON" if e.args.get("to_json") else "TO_JSON_STRING",
1323
+ e.this,
1324
+ e.args.get("options"),
1325
+ ),
1278
1326
  exp.JSONKeysAtDepth: rename_func("JSON_KEYS"),
1279
1327
  exp.JSONValueArray: rename_func("JSON_VALUE_ARRAY"),
1280
1328
  exp.Levenshtein: _levenshtein_sql,
@@ -312,6 +312,7 @@ class ClickHouse(Dialect):
312
312
  "ARRAYREVERSE": exp.ArrayReverse.from_arg_list,
313
313
  "ARRAYSLICE": exp.ArraySlice.from_arg_list,
314
314
  "COUNTIF": _build_count_if,
315
+ "COSINEDISTANCE": exp.CosineDistance.from_arg_list,
315
316
  "DATE_ADD": build_date_delta(exp.DateAdd, default_unit=None),
316
317
  "DATEADD": build_date_delta(exp.DateAdd, default_unit=None),
317
318
  "DATE_DIFF": build_date_delta(exp.DateDiff, default_unit=None, supports_timezone=True),
@@ -324,6 +325,7 @@ class ClickHouse(Dialect):
324
325
  exp.JSONExtractScalar, zero_based_indexing=False
325
326
  ),
326
327
  "LENGTH": lambda args: exp.Length(this=seq_get(args, 0), binary=True),
328
+ "L2Distance": exp.EuclideanDistance.from_arg_list,
327
329
  "MAP": parser.build_var_map,
328
330
  "MATCH": exp.RegexpLike.from_arg_list,
329
331
  "PARSEDATETIME": _build_datetime_format(exp.ParseDatetime),
@@ -1094,6 +1096,7 @@ class ClickHouse(Dialect):
1094
1096
  exp.Array: inline_array_sql,
1095
1097
  exp.CastToStrType: rename_func("CAST"),
1096
1098
  exp.CountIf: rename_func("countIf"),
1099
+ exp.CosineDistance: rename_func("cosineDistance"),
1097
1100
  exp.CompressColumnConstraint: lambda self,
1098
1101
  e: f"CODEC({self.expressions(e, key='this', flat=True)})",
1099
1102
  exp.ComputedColumnConstraint: lambda self,
@@ -1123,6 +1126,7 @@ class ClickHouse(Dialect):
1123
1126
  exp.Rand: rename_func("randCanonical"),
1124
1127
  exp.StartsWith: rename_func("startsWith"),
1125
1128
  exp.EndsWith: rename_func("endsWith"),
1129
+ exp.EuclideanDistance: rename_func("L2Distance"),
1126
1130
  exp.StrPosition: lambda self, e: strposition_sql(
1127
1131
  self,
1128
1132
  e,
@@ -106,6 +106,7 @@ class Databricks(Spark):
106
106
  ),
107
107
  }
108
108
 
109
+ TRANSFORMS.pop(exp.RegexpLike)
109
110
  TRANSFORMS.pop(exp.TryCast)
110
111
 
111
112
  TYPE_MAPPING = {
@@ -818,6 +818,7 @@ class Dialect(metaclass=_Dialect):
818
818
  exp.Cast: lambda self, e: self._annotate_with_type(e, e.args["to"]),
819
819
  exp.Case: lambda self, e: self._annotate_by_args(e, "default", "ifs"),
820
820
  exp.Coalesce: lambda self, e: self._annotate_by_args(e, "this", "expressions"),
821
+ exp.Contains: lambda self, e: self._annotate_with_type(e, exp.DataType.Type.BOOLEAN),
821
822
  exp.Count: lambda self, e: self._annotate_with_type(
822
823
  e, exp.DataType.Type.BIGINT if e.args.get("big_int") else exp.DataType.Type.INT
823
824
  ),
@@ -2005,6 +2006,12 @@ def groupconcat_sql(
2005
2006
  on_overflow_sql = self.sql(expression, "on_overflow")
2006
2007
  on_overflow_sql = f" ON OVERFLOW {on_overflow_sql}" if (on_overflow and on_overflow_sql) else ""
2007
2008
 
2009
+ if isinstance(this, exp.Limit) and this.this:
2010
+ limit = this
2011
+ this = limit.this.pop()
2012
+ else:
2013
+ limit = None
2014
+
2008
2015
  order = this.find(exp.Order)
2009
2016
 
2010
2017
  if order and order.this:
@@ -2013,11 +2020,16 @@ def groupconcat_sql(
2013
2020
  args = self.format_args(this, f"{separator}{on_overflow_sql}")
2014
2021
  listagg: exp.Expression = exp.Anonymous(this=func_name, expressions=[args])
2015
2022
 
2023
+ modifiers = self.sql(limit)
2024
+
2016
2025
  if order:
2017
2026
  if within_group:
2018
2027
  listagg = exp.WithinGroup(this=listagg, expression=order)
2019
2028
  else:
2020
- listagg.set("expressions", [f"{args}{self.sql(expression=expression.this)}"])
2029
+ modifiers = f"{self.sql(order)}{modifiers}"
2030
+
2031
+ if modifiers:
2032
+ listagg.set("expressions", [f"{args}{modifiers}"])
2021
2033
 
2022
2034
  return self.sql(listagg)
2023
2035
 
sqlglot/dialects/doris.py CHANGED
@@ -50,6 +50,7 @@ class Doris(MySQL):
50
50
  **MySQL.Parser.FUNCTIONS,
51
51
  "COLLECT_SET": exp.ArrayUniqueAgg.from_arg_list,
52
52
  "DATE_TRUNC": _build_date_trunc,
53
+ "L2_DISTANCE": exp.EuclideanDistance.from_arg_list,
53
54
  "MONTHS_ADD": exp.AddMonths.from_arg_list,
54
55
  "REGEXP": exp.RegexpLike.from_arg_list,
55
56
  "TO_DATE": exp.TsOrDsToDate.from_arg_list,
@@ -210,6 +211,7 @@ class Doris(MySQL):
210
211
  exp.CurrentDate: lambda self, _: self.func("CURRENT_DATE"),
211
212
  exp.CurrentTimestamp: lambda self, _: self.func("NOW"),
212
213
  exp.DateTrunc: lambda self, e: self.func("DATE_TRUNC", e.this, unit_to_str(e)),
214
+ exp.EuclideanDistance: rename_func("L2_DISTANCE"),
213
215
  exp.GroupConcat: lambda self, e: self.func(
214
216
  "GROUP_CONCAT", e.this, e.args.get("separator") or exp.Literal.string(",")
215
217
  ),
@@ -167,6 +167,8 @@ class Dremio(Dialect):
167
167
  FUNCTIONS = {
168
168
  **parser.Parser.FUNCTIONS,
169
169
  "ARRAY_GENERATE_RANGE": exp.GenerateSeries.from_arg_list,
170
+ "BIT_AND": exp.BitwiseAndAgg.from_arg_list,
171
+ "BIT_OR": exp.BitwiseOrAgg.from_arg_list,
170
172
  "DATE_ADD": build_date_delta_with_cast_interval(exp.DateAdd),
171
173
  "DATE_FORMAT": build_formatted_time(exp.TimeToStr, "dremio"),
172
174
  "DATE_SUB": build_date_delta_with_cast_interval(exp.DateSub),
@@ -216,6 +218,8 @@ class Dremio(Dialect):
216
218
 
217
219
  TRANSFORMS = {
218
220
  **generator.Generator.TRANSFORMS,
221
+ exp.BitwiseAndAgg: rename_func("BIT_AND"),
222
+ exp.BitwiseOrAgg: rename_func("BIT_OR"),
219
223
  exp.ToChar: rename_func("TO_CHAR"),
220
224
  exp.TimeToStr: lambda self, e: self.func("TO_CHAR", e.this, self.format_time(e)),
221
225
  exp.DateAdd: _date_delta_sql("DATE_ADD"),
@@ -304,7 +304,6 @@ class DuckDB(Dialect):
304
304
  "CHAR": TokenType.TEXT,
305
305
  "DATETIME": TokenType.TIMESTAMPNTZ,
306
306
  "DETACH": TokenType.DETACH,
307
- "EXCLUDE": TokenType.EXCEPT,
308
307
  "LOGICAL": TokenType.BOOLEAN,
309
308
  "ONLY": TokenType.ONLY,
310
309
  "PIVOT_WIDER": TokenType.PIVOT,
@@ -386,6 +385,8 @@ class DuckDB(Dialect):
386
385
  "JSON_EXTRACT_PATH": parser.build_extract_json_with_path(exp.JSONExtract),
387
386
  "JSON_EXTRACT_STRING": parser.build_extract_json_with_path(exp.JSONExtractScalar),
388
387
  "LIST_CONTAINS": exp.ArrayContains.from_arg_list,
388
+ "LIST_COSINE_DISTANCE": exp.CosineDistance.from_arg_list,
389
+ "LIST_DISTANCE": exp.EuclideanDistance.from_arg_list,
389
390
  "LIST_FILTER": exp.ArrayFilter.from_arg_list,
390
391
  "LIST_HAS": exp.ArrayContains.from_arg_list,
391
392
  "LIST_HAS_ANY": exp.ArrayOverlaps.from_arg_list,
@@ -650,6 +651,7 @@ class DuckDB(Dialect):
650
651
  ),
651
652
  exp.BitwiseXor: rename_func("XOR"),
652
653
  exp.CommentColumnConstraint: no_comment_column_constraint_sql,
654
+ exp.CosineDistance: rename_func("LIST_COSINE_DISTANCE"),
653
655
  exp.CurrentDate: lambda *_: "CURRENT_DATE",
654
656
  exp.CurrentTime: lambda *_: "CURRENT_TIME",
655
657
  exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
@@ -673,6 +675,7 @@ class DuckDB(Dialect):
673
675
  exp.DiToDate: lambda self,
674
676
  e: f"CAST(STRPTIME(CAST({self.sql(e, 'this')} AS TEXT), {DuckDB.DATEINT_FORMAT}) AS DATE)",
675
677
  exp.Encode: lambda self, e: encode_decode_sql(self, e, "ENCODE", replace=False),
678
+ exp.EuclideanDistance: rename_func("LIST_DISTANCE"),
676
679
  exp.GenerateDateArray: _generate_datetime_array_sql,
677
680
  exp.GenerateTimestampArray: _generate_datetime_array_sql,
678
681
  exp.GroupConcat: lambda self, e: groupconcat_sql(self, e, within_group=False),
sqlglot/dialects/hive.py CHANGED
@@ -194,6 +194,16 @@ def _build_to_date(args: t.List) -> exp.TsOrDsToDate:
194
194
  return expr
195
195
 
196
196
 
197
+ def _build_date_add(args: t.List) -> exp.TsOrDsAdd:
198
+ expression = seq_get(args, 1)
199
+ if expression:
200
+ expression = expression * -1
201
+
202
+ return exp.TsOrDsAdd(
203
+ this=seq_get(args, 0), expression=expression, unit=exp.Literal.string("DAY")
204
+ )
205
+
206
+
197
207
  class Hive(Dialect):
198
208
  ALIAS_POST_TABLESAMPLE = True
199
209
  IDENTIFIERS_CAN_START_WITH_DIGIT = True
@@ -314,11 +324,7 @@ class Hive(Dialect):
314
324
  seq_get(args, 1),
315
325
  ]
316
326
  ),
317
- "DATE_SUB": lambda args: exp.TsOrDsAdd(
318
- this=seq_get(args, 0),
319
- expression=exp.Mul(this=seq_get(args, 1), expression=exp.Literal.number(-1)),
320
- unit=exp.Literal.string("DAY"),
321
- ),
327
+ "DATE_SUB": _build_date_add,
322
328
  "DATEDIFF": lambda args: exp.DateDiff(
323
329
  this=exp.TsOrDsToDate(this=seq_get(args, 0)),
324
330
  expression=exp.TsOrDsToDate(this=seq_get(args, 1)),
sqlglot/dialects/mysql.py CHANGED
@@ -301,6 +301,10 @@ class MySQL(Dialect):
301
301
 
302
302
  FUNCTIONS = {
303
303
  **parser.Parser.FUNCTIONS,
304
+ "BIT_AND": exp.BitwiseAndAgg.from_arg_list,
305
+ "BIT_OR": exp.BitwiseOrAgg.from_arg_list,
306
+ "BIT_XOR": exp.BitwiseXorAgg.from_arg_list,
307
+ "BIT_COUNT": exp.BitwiseCountAgg.from_arg_list,
304
308
  "CONVERT_TZ": lambda args: exp.ConvertTimezone(
305
309
  source_tz=seq_get(args, 1), target_tz=seq_get(args, 2), timestamp=seq_get(args, 0)
306
310
  ),
@@ -691,7 +695,7 @@ class MySQL(Dialect):
691
695
  class Generator(generator.Generator):
692
696
  INTERVAL_ALLOWS_PLURAL_FORM = False
693
697
  LOCKING_READS_SUPPORTED = True
694
- NULL_ORDERING_SUPPORTED = None
698
+ NULL_ORDERING_SUPPORTED: t.Optional[bool] = None
695
699
  JOIN_HINTS = False
696
700
  TABLE_HINTS = True
697
701
  DUPLICATE_KEY_UPDATE_WITH_SET = False
@@ -712,6 +716,10 @@ class MySQL(Dialect):
712
716
  TRANSFORMS = {
713
717
  **generator.Generator.TRANSFORMS,
714
718
  exp.ArrayAgg: rename_func("GROUP_CONCAT"),
719
+ exp.BitwiseAndAgg: rename_func("BIT_AND"),
720
+ exp.BitwiseOrAgg: rename_func("BIT_OR"),
721
+ exp.BitwiseXorAgg: rename_func("BIT_XOR"),
722
+ exp.BitwiseCountAgg: rename_func("BIT_COUNT"),
715
723
  exp.CurrentDate: no_paren_current_date_sql,
716
724
  exp.DateDiff: _remove_ts_or_ds_to_date(
717
725
  lambda self, e: self.func("DATEDIFF", e.this, e.expression), ("this", "expression")
@@ -779,6 +787,8 @@ class MySQL(Dialect):
779
787
  exp.Week: _remove_ts_or_ds_to_date(),
780
788
  exp.WeekOfYear: _remove_ts_or_ds_to_date(rename_func("WEEKOFYEAR")),
781
789
  exp.Year: _remove_ts_or_ds_to_date(),
790
+ exp.UtcTimestamp: rename_func("UTC_TIMESTAMP"),
791
+ exp.UtcTime: rename_func("UTC_TIME"),
782
792
  }
783
793
 
784
794
  UNSIGNED_TYPE_MAPPING = {
@@ -107,6 +107,7 @@ class Oracle(Dialect):
107
107
  FUNCTIONS = {
108
108
  **parser.Parser.FUNCTIONS,
109
109
  "CONVERT": exp.ConvertToCharset.from_arg_list,
110
+ "L2_DISTANCE": exp.EuclideanDistance.from_arg_list,
110
111
  "NVL": lambda args: build_coalesce(args, is_nvl=True),
111
112
  "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
112
113
  "TO_CHAR": build_timetostr_or_tochar,
@@ -305,6 +306,7 @@ class Oracle(Dialect):
305
306
  "TO_DATE", e.this, exp.Literal.string("YYYY-MM-DD")
306
307
  ),
307
308
  exp.DateTrunc: lambda self, e: self.func("TRUNC", e.this, e.unit),
309
+ exp.EuclideanDistance: rename_func("L2_DISTANCE"),
308
310
  exp.Group: transforms.preprocess([transforms.unalias_group]),
309
311
  exp.ILike: no_ilike_sql,
310
312
  exp.LogicalOr: rename_func("MAX"),
@@ -336,6 +338,8 @@ class Oracle(Dialect):
336
338
  exp.Unicode: lambda self, e: f"ASCII(UNISTR({self.sql(e.this)}))",
337
339
  exp.UnixToTime: lambda self,
338
340
  e: f"TO_DATE('1970-01-01', 'YYYY-MM-DD') + ({self.sql(e, 'this')} / 86400)",
341
+ exp.UtcTimestamp: rename_func("UTC_TIMESTAMP"),
342
+ exp.UtcTime: rename_func("UTC_TIME"),
339
343
  }
340
344
 
341
345
  PROPERTIES_LOCATION = {
@@ -327,8 +327,7 @@ class Postgres(Dialect):
327
327
  "<@": TokenType.LT_AT,
328
328
  "|/": TokenType.PIPE_SLASH,
329
329
  "||/": TokenType.DPIPE_SLASH,
330
- "BEGIN": TokenType.COMMAND,
331
- "BEGIN TRANSACTION": TokenType.BEGIN,
330
+ "BEGIN": TokenType.BEGIN,
332
331
  "BIGSERIAL": TokenType.BIGSERIAL,
333
332
  "CONSTRAINT TRIGGER": TokenType.COMMAND,
334
333
  "CSTRING": TokenType.PSEUDO_TYPE,