Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlglot-integration-tests
16 changes: 16 additions & 0 deletions sqlglot/generators/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ def regravgx_sql(self, expression: exp.RegrAvgx) -> str:
)
return self.func("REGR_AVGX", expression.this, x)

def regrsxx_sql(self, expression: exp.RegrSxx) -> str:
x = expression.expression
if isinstance(x, exp.Distinct):
return self.func(
"REGR_SXX", exp.Distinct(expressions=[expression.this]), *x.expressions
)
return self.func("REGR_SXX", expression.this, x)

def regrsyy_sql(self, expression: exp.RegrSyy) -> str:
x = expression.expression
if isinstance(x, exp.Distinct):
return self.func(
"REGR_SYY", exp.Distinct(expressions=[expression.this]), *x.expressions
)
return self.func("REGR_SYY", expression.this, x)

@geooo109 geooo109 Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor here:

    def _regr_sql(self, expression: exp.RegrAvgx  | exp.RegrSxx | exp.RegrSyy) -> str:
        name = expression.sql_name()
        x = expression.expression
        if isinstance(x, exp.Distinct):
            return self.func(name, exp.Distinct(expressions=[expression.this]), *x.expressions)
       
        return self.func(name, expression.this, x)

Then we can resue this ^ for all the 3 cases.


def clusterproperty_sql(self, expression):
this = self.sql(expression, "this") or f"({self.expressions(expression, flat=True)})"
return f"CLUSTER BY {this}"
3 changes: 3 additions & 0 deletions sqlglot/parsers/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class DatabricksParser(SparkParser):
**SparkParser.FUNCTION_PARSERS,
"REGR_AVGX": lambda self: self._parse_distinct_arg_function(exp.RegrAvgx, distinct_index=1),
"REGR_AVGY": lambda self: self._parse_distinct_arg_function(exp.RegrAvgy),
"REGR_SXX": lambda self: self._parse_distinct_arg_function(exp.RegrSxx, distinct_index=1),
"REGR_SXY": lambda self: self._parse_distinct_arg_function(exp.RegrSxy),
"REGR_SYY": lambda self: self._parse_distinct_arg_function(exp.RegrSyy, distinct_index=1),
}

FACTOR = {
Expand Down
3 changes: 3 additions & 0 deletions sqlglot/typing/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
exp.RegrIntercept,
exp.RegrR2,
exp.RegrSlope,
exp.RegrSxx,
exp.RegrSxy,
exp.RegrSyy,
}
},
**{
Expand Down
60 changes: 60 additions & 0 deletions tests/fixtures/optimizer/annotate_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4439,6 +4439,66 @@ DOUBLE;
REGR_SYY(tbl.decfloat_col, tbl.decfloat_col);
DECFLOAT;

# dialect: databricks
REGR_SXX(tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXX(tbl.int_col, tbl.int_col);
DOUBLE;

# dialect: databricks
REGR_SXX(ALL tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXX(DISTINCT tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXX(tbl.double_col, tbl.double_col) OVER (PARTITION BY 1);
DOUBLE;

# dialect: databricks
REGR_SXY(tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXY(tbl.int_col, tbl.int_col);
DOUBLE;

# dialect: databricks
REGR_SXY(ALL tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXY(DISTINCT tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SXY(tbl.double_col, tbl.double_col) OVER (PARTITION BY 1);
DOUBLE;

# dialect: databricks
REGR_SYY(tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SYY(tbl.int_col, tbl.int_col);
DOUBLE;

# dialect: databricks
REGR_SYY(ALL tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SYY(DISTINCT tbl.double_col, tbl.double_col);
DOUBLE;

# dialect: databricks
REGR_SYY(tbl.double_col, tbl.double_col) OVER (PARTITION BY 1);
DOUBLE;

# dialect: snowflake
REGR_SLOPE(tbl.double_col, tbl.double_col);
DOUBLE;
Expand Down
Loading