diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py index 008727c6db..25c3601e22 100644 --- a/sqlglot/dialects/postgres.py +++ b/sqlglot/dialects/postgres.py @@ -439,6 +439,8 @@ class Generator(generator.Generator): exp.TryCast: no_trycast_sql, exp.TsOrDsToDate: ts_or_ds_to_date_sql("postgres"), exp.UnixToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')})", + exp.VariancePop: rename_func("VAR_POP"), + exp.Variance: rename_func("VAR_SAMP"), exp.Xor: bool_xor_sql, } diff --git a/tests/dialects/test_postgres.py b/tests/dialects/test_postgres.py index 0ddc10613a..641c1ea841 100644 --- a/tests/dialects/test_postgres.py +++ b/tests/dialects/test_postgres.py @@ -724,3 +724,11 @@ def test_string_concat(self): "presto": "CONCAT(CAST(a AS VARCHAR), CAST(b AS VARCHAR))", }, ) + + def test_variance(self): + self.validate_all("VAR_SAMP(x)", write={"postgres": "VAR_SAMP(x)"}) + self.validate_all("VAR_POP(x)", write={"postgres": "VAR_POP(x)"}) + self.validate_all("VARIANCE(x)", write={"postgres": "VAR_SAMP(x)"}) + self.validate_all( + "VAR_POP(x)", read={"": "VARIANCE_POP(x)"}, write={"postgres": "VAR_POP(x)"} + )