Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 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 @@ -579,6 +579,15 @@
SqlNodeList hints, int leftPrec, int rightPrec) {
}

/** Converts a UUID literal to a SQL string. The default implementation
* returns strings such as
* <code>UUID '123e4567-e89b-12d3-a456-426655440000'</code>. A dialect whose
* product spells it differently should override this method. */
public void unparseUuidLiteral(SqlWriter writer,
Comment thread
wasabii marked this conversation as resolved.
SqlUuidLiteral literal, int leftPrec, int rightPrec) {

Check warning on line 587 in core/src/main/java/org/apache/calcite/sql/SqlDialect.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove these unused method parameters "leftPrec", "rightPrec".

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AaB-hYmtsLuWzoMC4gdA&open=AaB-hYmtsLuWzoMC4gdA&pullRequest=5250
writer.literal(literal.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this default implementation align with MySQL or PostgreSQL? Should we choose a generic approach, or simply throw an error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not aware of any real database that uses the Calcite syntax. So the default is kinda useless. But that is the Calcite syntax.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this default Calcite method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is one I believe. Was there before me.

}

/**
* Returns whether the string contains any characters outside the
* comfortable 7-bit ASCII range (32 through 127, plus linefeed (10) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public String toFormattedString() {
SqlWriter writer,
int leftPrec,
int rightPrec) {
writer.literal(this.toString());
writer.getDialect().unparseUuidLiteral(writer, this, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlUuidLiteral;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.fun.SqlLibraryOperators;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
Expand Down Expand Up @@ -200,6 +201,11 @@ private static SqlNode createDatetimeCastSpec(String typeAlias, RelDataType type
writer.literal("'" + literal.toFormattedString() + "'");
}

@Override public void unparseUuidLiteral(SqlWriter writer,
SqlUuidLiteral literal, int leftPrec, int rightPrec) {
writer.literal("'" + literal.toFormattedString() + "'");
}

@Override public void unparseCall(SqlWriter writer, SqlCall call,
int leftPrec, int rightPrec) {
if (call.getOperator() == SqlStdOperatorTable.SUBSTRING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10988,6 +10988,15 @@ private void checkLiteral2(String expression, String expected) {
sql(sql).ok(expected);
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-7768">[CALCITE-7768]
* SqlDialect has no way to unparse a UUID literal</a>. */
@Test void testUuidMssql() {
final String sql = "SELECT UUID '123e4567-e89b-12d3-a456-426655440000' AS x";
final String expected = "SELECT *\n"
+ "FROM (VALUES ('123e4567-e89b-12d3-a456-426655440000')) AS [t] ([X])";
sql(sql).withMssql().ok(expected);
}

@Test void testUpdate() {
final String sql0 = "update \"foodmart\".\"product\"\n"
+ "set \"product_name\" = 'calcite'";
Expand Down
Loading