Skip to content

Commit

Permalink
[CALCITE-6819] MSSQL doesn't support TRUE/FALSE keywords in its Join …
Browse files Browse the repository at this point in the history
…predicate

In MSSQL dialect use 1=1 instead of TRUE
  • Loading branch information
sreeharshar84 authored and NobiGo committed Feb 19, 2025
1 parent f4ba90b commit dd49d9f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

import org.checkerframework.checker.nullness.qual.Nullable;

import static org.apache.calcite.util.RelToSqlConverterUtil.unparseBoolLiteralToCondition;

import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -189,6 +191,15 @@ public MssqlSqlDialect(Context context) {
}
}

@Override public void unparseBoolLiteral(SqlWriter writer,
SqlLiteral literal, int leftPrec, int rightPrec) {
Boolean value = (Boolean) literal.getValue();
if (value == null) {
return;
}
unparseBoolLiteralToCondition(writer, value);
}

@Override public boolean supportsCharSet() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

import java.util.List;

import static org.apache.calcite.util.RelToSqlConverterUtil.unparseBoolLiteralToCondition;

/**
* A <code>SqlDialect</code> implementation for the Oracle database.
*/
Expand Down Expand Up @@ -139,11 +141,7 @@ public OracleSqlDialect(Context context) {
return;
}
// low version oracle not support bool literal
final SqlWriter.Frame frame = writer.startList("(", ")");
writer.literal("1");
writer.sep(SqlStdOperatorTable.EQUALS.getName());
writer.literal(value ? "1" : "0");
writer.endList(frame);
unparseBoolLiteralToCondition(writer, value);
}

@Override public void unparseDateTimeLiteral(SqlWriter writer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.fun.SqlTrimFunction;
import org.apache.calcite.sql.parser.SqlParserPos;

Expand Down Expand Up @@ -196,4 +197,18 @@ public static SqlSpecialOperator specialOperatorByName(String opName) {
}
};
}

/**
* Writes TRUE/FALSE or 1 = 1/ 1 &lt; &gt; 1 for certain.
*
* @param writer current SqlWriter object
* @param value boolean value to be unparsed.
*/
public static void unparseBoolLiteralToCondition(SqlWriter writer, boolean value) {
final SqlWriter.Frame frame = writer.startList("(", ")");
writer.literal("1");
writer.sep(SqlStdOperatorTable.EQUALS.getName());
writer.literal(value ? "1" : "0");
writer.endList(frame);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8229,6 +8229,27 @@ private void checkLiteral2(String expression, String expected) {
.withOracle(11).ok(expectedVersionLow);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6819">[CALCITE-6819]
* MSSQL doesn't support TRUE/FALSE keywords in its Join predicate</a>. */
@Test void testJoinBoolLiteralMSSQL() {
final String queryTrue = "SELECT \"hire_date\", \"department_description\" FROM \"employee\" "
+ "LEFT JOIN \"department\" ON TRUE";
final String mssqlExpected1 = "SELECT [employee].[hire_date],"
+ " [department].[department_description]\nFROM [foodmart].[employee]\nLEFT JOIN"
+ " [foodmart].[department] ON (1 = 1)";
sql(queryTrue)
.dialect(MssqlSqlDialect.DEFAULT).ok(mssqlExpected1);

final String queryFalse = "SELECT \"hire_date\", \"department_description\" FROM \"employee\" "
+ "LEFT JOIN \"department\" ON False";
final String mssqlExpected2 = "SELECT [employee].[hire_date],"
+ " [department].[department_description]\nFROM [foodmart].[employee]\nLEFT JOIN"
+ " [foodmart].[department] ON (1 = 0)";
sql(queryFalse)
.dialect(MssqlSqlDialect.DEFAULT).ok(mssqlExpected2);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6480">[CALCITE-6480]
* OracleDialect does not support CASE WHEN returning boolean</a>. */
Expand Down

0 comments on commit dd49d9f

Please sign in to comment.