Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 ibis/backends/sql/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class MSSQL(TSQL):
class Generator(TSQL.Generator):
TRANSFORMS = TSQL.Generator.TRANSFORMS.copy() | {
sge.ApproxDistinct: rename_func("approx_count_distinct"),
sge.Stddev: rename_func("stdevp"),
sge.Stddev: rename_func("stdev"),
sge.StddevPop: rename_func("stdevp"),
sge.StddevSamp: rename_func("stdev"),
sge.Variance: rename_func("var"),
Expand Down
23 changes: 22 additions & 1 deletion ibis/backends/sql/tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import ibis
from ibis import _
from ibis.backends.sql.dialects import Trino
from ibis.backends.sql.dialects import MSSQL, Trino


def test_window_with_row_number_compiles():
Expand All @@ -26,3 +26,24 @@ def test_transpile_join():
"SELECT * FROM t1 JOIN t2 ON x = y", read="duckdb", write=Trino
)
assert "CROSS JOIN" not in result


def test_mssql_stdev_roundtrips_as_sample():
# GH #12057: sqlglot parses T-SQL STDEV (sample) into sge.Stddev. The MSSQL
# dialect used to render that node as STDEVP (population), so a same-
# dialect round trip through Table.sql() / add_query_to_expr silently
# changed the statistic. Bare Stddev must render as STDEV; StddevPop
# already covers STDEVP.
(result,) = sg.transpile(
"SELECT STDEV([x]) AS [s] FROM [t]", read=MSSQL, write=MSSQL
)
assert result == "SELECT STDEV([x]) AS [s] FROM [t]"


def test_mssql_std_how_emits_distinct_functions():
t = ibis.table({"x": "float"}, name="t")
sample_sql = ibis.to_sql(t.x.std(how="sample"), dialect="mssql")
pop_sql = ibis.to_sql(t.x.std(how="pop"), dialect="mssql")
assert "STDEV(" in sample_sql
assert "STDEVP(" not in sample_sql
assert "STDEVP(" in pop_sql
Loading