bito-code-review[bot] commented on code in PR #42766:
URL: https://github.com/apache/superset/pull/42766#discussion_r3717174760
##########
tests/unit_tests/models/helpers_test.py:
##########
@@ -1750,6 +1750,106 @@ def test_orderby_adhoc_column(database: Database) ->
None:
assert "ORDER BY" in sql.upper()
+def test_orderby_adhoc_column_label_takes_precedence_over_saved_metric(
+ database: Database,
+) -> None:
+ """
+ Test that orderby by an adhoc column label resolves to the selected column.
+ """
+ from superset.connectors.sqla.models import SqlaTable, SqlMetric,
TableColumn
+
+ table: SqlaTable = SqlaTable(
+ database=database,
+ schema=None,
+ table_name="t",
+ columns=[
+ TableColumn(column_name="a"),
+ TableColumn(column_name="b"),
+ ],
+ metrics=[
+ SqlMetric(metric_name="custom_col", expression="SUM(a)"),
+ ],
+ )
+
+ result = table.get_sqla_query(
+ columns=[
+ {"expressionType": "SQL", "label": "custom_col", "sqlExpression":
"a + 1"},
+ "b",
+ ],
+ orderby=[("custom_col", False)],
+ metrics=[],
+ extras={},
+ filter=[],
+ granularity=None,
+ is_timeseries=False,
+ )
+
+ sql = str(result.sqla_query).upper()
+ assert "ORDER BY" in sql
+ assert "SUM(A)" not in sql
+
+
[email protected]("aggregate", [None, "MEDIAN", ["SUM"], {"op": "SUM"}])
+def test_adhoc_metric_to_sqla_invalid_simple_aggregate_raises_validation_error(
+ database: Database,
+ aggregate: Any,
+) -> None:
+ """
+ Test that malformed SIMPLE adhoc metrics fail with a validation error.
+ """
+ from superset.connectors.sqla.models import SqlaTable, TableColumn
+ from superset.exceptions import QueryObjectValidationError
+
+ table: SqlaTable = SqlaTable(
+ database=database,
+ schema=None,
+ table_name="t",
+ columns=[
+ TableColumn(column_name="a"),
+ ],
+ )
+ metric: AdhocMetric = {
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Undefined AdhocMetric import</b></div>
<div id="fix">
The name `AdhocMetric` is used but not imported. Add the import from
`superset.superset_typing` alongside the existing `AdhocColumn` import. This
same issue occurs at line 1842.
</div>
</div>
<small><i>Code Review Run #7626fa</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
##########
tests/unit_tests/models/helpers_test.py:
##########
@@ -1750,6 +1750,106 @@ def test_orderby_adhoc_column(database: Database) ->
None:
assert "ORDER BY" in sql.upper()
+def test_orderby_adhoc_column_label_takes_precedence_over_saved_metric(
+ database: Database,
+) -> None:
+ """
+ Test that orderby by an adhoc column label resolves to the selected column.
+ """
+ from superset.connectors.sqla.models import SqlaTable, SqlMetric,
TableColumn
+
+ table: SqlaTable = SqlaTable(
+ database=database,
+ schema=None,
+ table_name="t",
+ columns=[
+ TableColumn(column_name="a"),
+ TableColumn(column_name="b"),
+ ],
+ metrics=[
+ SqlMetric(metric_name="custom_col", expression="SUM(a)"),
+ ],
+ )
+
+ result = table.get_sqla_query(
+ columns=[
+ {"expressionType": "SQL", "label": "custom_col", "sqlExpression":
"a + 1"},
+ "b",
+ ],
+ orderby=[("custom_col", False)],
+ metrics=[],
+ extras={},
+ filter=[],
+ granularity=None,
+ is_timeseries=False,
+ )
+
+ sql = str(result.sqla_query).upper()
+ assert "ORDER BY" in sql
+ assert "SUM(A)" not in sql
+
+
[email protected]("aggregate", [None, "MEDIAN", ["SUM"], {"op": "SUM"}])
+def test_adhoc_metric_to_sqla_invalid_simple_aggregate_raises_validation_error(
+ database: Database,
+ aggregate: Any,
+) -> None:
+ """
+ Test that malformed SIMPLE adhoc metrics fail with a validation error.
+ """
+ from superset.connectors.sqla.models import SqlaTable, TableColumn
+ from superset.exceptions import QueryObjectValidationError
+
+ table: SqlaTable = SqlaTable(
+ database=database,
+ schema=None,
+ table_name="t",
+ columns=[
+ TableColumn(column_name="a"),
+ ],
+ )
+ metric: AdhocMetric = {
+ "expressionType": "SIMPLE",
+ "column": {"column_name": "a"},
+ "label": "Invalid metric",
+ }
+ if aggregate is not None:
+ metric["aggregate"] = aggregate
+
+ with pytest.raises(QueryObjectValidationError):
+ table.adhoc_metric_to_sqla(metric, {})
+
+
[email protected]("sql_expression", [None, "", " "])
+def test_adhoc_metric_to_sqla_invalid_sql_expression_raises_validation_error(
+ database: Database,
+ sql_expression: str | None,
+) -> None:
+ """
+ Test that malformed SQL adhoc metrics fail with a validation error.
+ """
+ from superset.connectors.sqla.models import SqlaTable, TableColumn
+ from superset.exceptions import QueryObjectValidationError
+
+ table = SqlaTable(
+ database=database,
+ schema=None,
+ table_name="t",
+ columns=[
+ TableColumn(column_name="a"),
+ ],
+ )
+ metric: AdhocMetric = {
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Undefined AdhocMetric import</b></div>
<div id="fix">
Similar to line 1811, `AdhocMetric` is used but not imported. Add the import
(same fix as for line 1811).
</div>
</div>
<small><i>Code Review Run #7626fa</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]