Skip to content

Commit

Permalink
[CALCITE-6482] Oracle dialect convert boolean literal when version < 23
Browse files Browse the repository at this point in the history
  • Loading branch information
suibianwanwank committed Jul 23, 2024
1 parent ace3afe commit b43d088
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,13 @@ public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec,
}
}

public void unparseBoolLiteral(SqlWriter writer,
SqlLiteral literal, int leftPrec, int rightPrec) {
Object value = literal.getValue();
writer.keyword(
value == null ? "UNKNOWN" : (Boolean) value ? "TRUE" : "FALSE");
}

public void unparseDateTimeLiteral(SqlWriter writer,
SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
writer.literal(literal.toString());
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/apache/calcite/sql/SqlLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,7 @@ public String getStringValue() {
int rightPrec) {
switch (typeName) {
case BOOLEAN:
writer.keyword(
value == null ? "UNKNOWN" : (Boolean) value ? "TRUE" : "FALSE");
writer.getDialect().unparseBoolLiteral(writer, this, leftPrec, rightPrec);
break;
case NULL:
writer.keyword("NULL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ public OracleSqlDialect(Context context) {
return false;
}

@Override public void unparseBoolLiteral(SqlWriter writer,
SqlLiteral literal, int leftPrec, int rightPrec) {
Boolean value = (Boolean) literal.getValue();
if (value == null || majorVersion >= 23) {
super.unparseBoolLiteral(writer, literal, leftPrec, rightPrec);
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);
}

@Override public void unparseDateTimeLiteral(SqlWriter writer,
SqlAbstractDateTimeLiteral literal, int leftPrec, int rightPrec) {
if (literal instanceof SqlTimestampLiteral) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7700,6 +7700,27 @@ private void checkLiteral2(String expression, String expected) {
.withOracle(11).throws_("Lower Oracle version(<12) doesn't support offset/fetch syntax!");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6482">[CALCITE-6482]
* Oracle dialect convert boolean literal when version < 23</a>. */
@Test void testBoolLiteralOracle() {
String query = "SELECT \"e1\".\"department_id\" "
+ "FROM \"employee\" \"e1\""
+ "LEFT JOIN \"employee\" \"e2\""
+ "ON TRUE";
String expectedVersionLow = "SELECT \"employee\".\"department_id\"\n"
+ "FROM \"foodmart\".\"employee\"\n"
+ "LEFT JOIN \"foodmart\".\"employee\" \"employee0\" "
+ "ON (1 = 1)";
String expectedVersionHigh = "SELECT \"employee\".\"department_id\"\n"
+ "FROM \"foodmart\".\"employee\"\n"
+ "LEFT JOIN \"foodmart\".\"employee\" \"employee0\" "
+ "ON TRUE";
sql(query)
.withOracle(23).ok(expectedVersionHigh)
.withOracle(11).ok(expectedVersionLow);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5265">[CALCITE-5265]
* JDBC adapter sometimes adds unnecessary parentheses around SELECT in INSERT</a>. */
Expand Down

0 comments on commit b43d088

Please sign in to comment.