Describe the bug
The MSSQL expressions.sort template generates ORDER BY <expression> IS NULL ... to emulate PostgreSQL null ordering. SQL Server cannot use a predicate as a sort expression. When the query also has OFFSET/FETCH, the driver reports Invalid usage of the option NEXT in the FETCH statement. The earlier error in precedingErrors is Incorrect syntax near the keyword 'IS'.
Regular semantic queries can succeed because they use a different ordering template. SQL API queries that enter the pushdown path encounter this defect.
Reproduction
Run this on SQL Server or Azure SQL:
SELECT x
FROM (VALUES (CAST(NULL AS int)), (1), (2)) AS t(x)
ORDER BY x IS NULL ASC, x ASC
OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY;
Expected order for ASC NULLS LAST: 1, 2, NULL.
Actual result: syntax error near IS, followed by the FETCH NEXT error.
The same pagination succeeds when the null discriminator is a scalar:
SELECT x
FROM (VALUES (CAST(NULL AS int)), (1), (2)) AS t(x)
ORDER BY CASE WHEN x IS NULL THEN 1 ELSE 0 END ASC, x ASC
OFFSET 0 ROWS FETCH NEXT 3 ROWS ONLY;
To inspect the responsible template in the published Cube package:
const { MssqlQuery } = require('@cubejs-backend/schema-compiler');
const query = Object.create(MssqlQuery.prototype);
console.log(query.sqlTemplates().expressions.sort);
It returns:
{{ expr }} IS NULL {% if nulls_first %}DESC{% else %}ASC{% endif %}, {{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}
Expected behavior and proposed fix
Wrap the null predicate in CASE WHEN ... THEN 1 ELSE 0 END. Keep the discriminator direction controlled by nulls_first, independently of the value direction controlled by asc.
CASE WHEN {{ expr }} IS NULL THEN 1 ELSE 0 END {% if nulls_first %}DESC{% else %}ASC{% endif %}, {{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}
This preserves ASC/DESC and NULLS FIRST/LAST while generating valid T-SQL. No pagination or planner changes are needed.
Versions and scope
- Reproduced with
cubejs/cube:v1.7.33.
- Azure SQL reports
Microsoft SQL Azure 12.0.2000.8, database compatibility level 150.
Related reports
A search of open and closed Cube issues and pull requests on 2026-09-05 found no exact duplicate. #7392 also concerns MSSQL pagination, but its generated SQL lacks ORDER BY; it is a different failure. MSSQL support work in #10343 is related background.
The independent missing-join template defect and numeric literal precision issue are outside this patch.
Describe the bug
The MSSQL
expressions.sorttemplate generatesORDER BY <expression> IS NULL ...to emulate PostgreSQL null ordering. SQL Server cannot use a predicate as a sort expression. When the query also has OFFSET/FETCH, the driver reportsInvalid usage of the option NEXT in the FETCH statement.The earlier error inprecedingErrorsisIncorrect syntax near the keyword 'IS'.Regular semantic queries can succeed because they use a different ordering template. SQL API queries that enter the pushdown path encounter this defect.
Reproduction
Run this on SQL Server or Azure SQL:
Expected order for ASC NULLS LAST:
1, 2, NULL.Actual result: syntax error near
IS, followed by the FETCH NEXT error.The same pagination succeeds when the null discriminator is a scalar:
To inspect the responsible template in the published Cube package:
It returns:
{{ expr }} IS NULL {% if nulls_first %}DESC{% else %}ASC{% endif %}, {{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}Expected behavior and proposed fix
Wrap the null predicate in
CASE WHEN ... THEN 1 ELSE 0 END. Keep the discriminator direction controlled bynulls_first, independently of the value direction controlled byasc.CASE WHEN {{ expr }} IS NULL THEN 1 ELSE 0 END {% if nulls_first %}DESC{% else %}ASC{% endif %}, {{ expr }} {% if asc %}ASC{% else %}DESC{% endif %}This preserves ASC/DESC and NULLS FIRST/LAST while generating valid T-SQL. No pagination or planner changes are needed.
Versions and scope
cubejs/cube:v1.7.33.Microsoft SQL Azure 12.0.2000.8, database compatibility level 150.Related reports
A search of open and closed Cube issues and pull requests on 2026-09-05 found no exact duplicate. #7392 also concerns MSSQL pagination, but its generated SQL lacks ORDER BY; it is a different failure. MSSQL support work in #10343 is related background.
The independent missing-join template defect and numeric literal precision issue are outside this patch.