Skip to content

Commit

Permalink
[CALCITE-6726] Add translation for MOD operator in MSSQL
Browse files Browse the repository at this point in the history
MOD is not supported in MSSQL. Translate it to %.
  • Loading branch information
sreeharshar84 authored and mihaibudiu committed Dec 11, 2024
1 parent cd0b8da commit 041619f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlNodeList;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
Expand Down Expand Up @@ -177,7 +179,10 @@ public MssqlSqlDialect(Context context) {
}
unparseFloor(writer, call);
break;

case MOD:
SqlOperator op = SqlStdOperatorTable.PERCENT_REMAINDER;
SqlSyntax.BINARY.unparse(writer, op, call, leftPrec, rightPrec);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8940,6 +8940,17 @@ private void checkLiteral2(String expression, String expected) {
.dialect(MssqlSqlDialect.DEFAULT).ok(mssqlExpected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6726">[CALCITE-6726]
* Add translation for MOD operator in MSSQL</a>.
*/
@Test public void testModFunctionEmulationForMSSQL() {
final String query = "select mod(11,3)";
final String mssqlExpected = "SELECT 11 % 3\nFROM (VALUES (0)) AS [t] ([ZERO])";
sql(query).dialect(MssqlSqlDialect.DEFAULT).ok(mssqlExpected);
}


/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6655">[CALCITE-6655]
* Aggregation of deeply nested window not detected when unparsing</a>.
Expand Down

0 comments on commit 041619f

Please sign in to comment.