diff options
Diffstat (limited to 'sqlglot/dialects/postgres.py')
-rw-r--r-- | sqlglot/dialects/postgres.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index d7cbac4..5f556a5 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -12,6 +12,7 @@ from sqlglot.dialects.dialect import ( no_trycast_sql, rename_func, str_position_sql, + timestamptrunc_sql, trim_sql, ) from sqlglot.helper import seq_get @@ -34,7 +35,7 @@ def _date_add_sql(kind): from sqlglot.optimizer.simplify import simplify this = self.sql(expression, "this") - unit = self.sql(expression, "unit") + unit = expression.args.get("unit") expression = simplify(expression.args["expression"]) if not isinstance(expression, exp.Literal): @@ -92,8 +93,7 @@ def _string_agg_sql(self, expression): this = expression.this if isinstance(this, exp.Order): if this.this: - this = this.this - this.pop() + this = this.this.pop() order = self.sql(expression.this) # Order has a leading space return f"STRING_AGG({self.format_args(this, separator)}{order})" @@ -256,6 +256,9 @@ class Postgres(Dialect): "TO_TIMESTAMP": _to_timestamp, "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"), "GENERATE_SERIES": _generate_series, + "DATE_TRUNC": lambda args: exp.TimestampTrunc( + this=seq_get(args, 1), unit=seq_get(args, 0) + ), } BITWISE = { @@ -311,6 +314,7 @@ class Postgres(Dialect): exp.DateSub: _date_add_sql("-"), exp.DateDiff: _date_diff_sql, exp.LogicalOr: rename_func("BOOL_OR"), + exp.LogicalAnd: rename_func("BOOL_AND"), exp.Min: min_or_least, exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"), exp.ArrayContains: lambda self, e: self.binary(e, "@>"), @@ -320,6 +324,7 @@ class Postgres(Dialect): exp.StrPosition: str_position_sql, exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})", exp.Substring: _substring_sql, + exp.TimestampTrunc: timestamptrunc_sql, exp.TimeStrToTime: lambda self, e: f"CAST({self.sql(e, 'this')} AS TIMESTAMP)", exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})", exp.TableSample: no_tablesample_sql, |