Skip to content

fix(mssql): map bare Stddev to STDEV, not STDEVP - #12133

Closed
00200200 wants to merge 1 commit into
ibis-project:mainfrom
00200200:fix/mssql-stdev-sample-stddev
Closed

00200200 wants to merge 1 commit into
ibis-project:mainfrom
00200200:fix/mssql-stdev-sample-stddev

Conversation

@00200200

Copy link
Copy Markdown

Summary

Closes #12057.

The MSSQL dialect mapped bare sge.Stddev to STDEVP (population). sqlglot parses T-SQL STDEV — the sample function — into that node, so a same-dialect re-render silently changed the statistic.

sge.StddevPop already renders STDEVP, and three lines below sge.Variance already maps to the sample VAR, so this looks like a typo rather than an intentional choice. Vanilla sqlglot's tsql dialect already round-trips STDEV correctly.

Reproduction path

Backend.sql(query) without dialect= returns early and does not transpile. The path that does hit this is Table.sql() / add_query_to_expr, which re-renders unconditionally:

# before: SELECT STDEVP([x]) AS [s] FROM [t]
# after:  SELECT STDEV([x]) AS [s] FROM [t]
sqlglot.transpile("SELECT STDEV([x]) AS [s] FROM [t]", read=MSSQL, write=MSSQL)

Ibis's own t.x.std(how="sample"|"pop") compilation already went through StddevSamp / StddevPop and was unaffected; the tests lock that in.

Test plan

  • pytest ibis/backends/sql/tests/test_compiler.py::test_mssql_stdev_roundtrips_as_sample -o addopts=
  • pytest ibis/backends/sql/tests/test_compiler.py::test_mssql_std_how_emits_distinct_functions -o addopts=
  • Confirmed the round-trip assertion fails if the mapping is reverted to stdevp

sqlglot parses T-SQL STDEV (sample) into sge.Stddev, but the MSSQL
generator rendered that node as STDEVP (population). Same-dialect
round-trips through Table.sql() therefore changed the statistic.
StddevPop already maps to STDEVP; Variance already maps to the sample
function, so this matches that pattern.

Closes #12057
@github-actions github-actions Bot added tests Issues or PRs related to tests sql Backends that generate SQL labels Sep 29, 2026
@deepyaman deepyaman added the maybe-ai Apply to low-effort autonomous PRs to automatically close them. label Oct 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This was marked as potentially AI generated and will be closed now. If this is an error, please provide additional details, make sure to read the docs about contributing and AI.

@github-actions github-actions Bot closed this Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maybe-ai Apply to low-effort autonomous PRs to automatically close them. sql Backends that generate SQL tests Issues or PRs related to tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: MSSQL dialect rewrites STDEV (sample) to STDEVP (population) when round-tripping SQL through Backend.sql()

2 participants