Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-5988] SqlImplementor.toSql cannot emit VARBINARY literals #3419

Closed
wants to merge 2 commits into from

Conversation

mihaibudiu
Copy link
Contributor

No description provided.

@mihaibudiu
Copy link
Contributor Author

@julianhyde I think you wrote most of this code (long time ago), perhaps you can review this tiny PR? Thank you.

@julianhyde
Copy link
Contributor

Please don't ask me, personally, to review PRs. I have done a huge amount for this project. That doesn't give everyone the right to ask me to do more.

I reviewed a PR yesterday because you said I'd written the code. That wasn't true. I had merely refactored the test.

Copy link
Member

@libenchao libenchao left a comment

Choose a reason for hiding this comment

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

@mihaibudiu Thanks for your PR, it looks good generally, I've left some minor comments.

@@ -1437,6 +1437,8 @@ public static SqlNode toSql(RexLiteral literal) {
return SqlLiteral.createTimestamp(typeName,
castNonNull(literal.getValueAs(TimestampString.class)),
literal.getType().getPrecision(), POS);
case BINARY:
return SqlLiteral.createBinaryString(requireNonNull(literal.getValueAs(byte[].class)), POS);
Copy link
Member

Choose a reason for hiding this comment

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

NIT: why not use castNonNull to keep it the same with other types?

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if select cast(null as binary) would work for current implementation. It's true that all other types does not consider null, they may suffer from the same problem.

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if select cast(null as binary) would work for current implementation. It's true that all other types does not consider null, they may suffer from the same problem.

@mihaibudiu What's your thought on this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a test for this. It works.

Copy link
Member

Choose a reason for hiding this comment

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

Seems that the SqlTypeName of a RexLiteral is always NULL, no matter what the RelDataType is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's much worse, the typeName of RexLiteral is not null, it is used in a very ad-hoc way. It almost never matches the actual type.

The field's Javadoc discusses this:

// TODO jvs 26-May-2006:  Use SqlTypeFamily instead; it exists
  // for exactly this purpose (to avoid the confusion which results
  // from overloading SqlTypeName).
  /**
   * An indication of the broad type of this literal -- even if its type isn't
   * a SQL type. Sometimes this will be different than the SQL type; for
   * example, all exact numbers, including integers have typeName
   * {@link SqlTypeName#DECIMAL}. See {@link #valueMatchesType} for the
   * definitive story.
   */

So the typeName is used instead of a TypeFamily. I personally think this field should be completely removed, but there are many uses in the code that would have to be audited. It should have been deprecated in 2006. Moreover, removing it may break third-party software that depends on Calcite that is not tested as part of the Calcite testing process. The type is the right field to use.

The TypeFamily itself has a very unclear meaning: https://issues.apache.org/jira/browse/CALCITE-5986

I have tried to clean up the TypeFamily in #3411, but I couldn't do it systematically, it's now also used itself in many places.

* Test for <a href="https://issues.apache.org/jira/browse/CALCITE-5988">[CALCITE-5988]</a>
* SqlImplementor.toSql cannot emit VARBINARY literals.
*/
@Test void testBinary() {
Copy link
Member

Choose a reason for hiding this comment

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

how about name it testBinaryLiteral, it's more precise.

@Test void testBinary() {
String query = "SELECT x'ABCD'";
String expected = "SELECT X'ABCD'";
sql(query).withMysql().ok(expected);
Copy link
Member

Choose a reason for hiding this comment

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

Is withMysql necessary to reproduce the issue, if not, I'd suggest to just use sql(query).ok(exptected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a comment explaining this choice, but if you prefer to use the default Calcite dialect I can change the test.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with leaving as it is.

Signed-off-by: Mihai Budiu <mbudiu@gmail.com>
Signed-off-by: Mihai Budiu <mbudiu@gmail.com>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@libenchao libenchao left a comment

Choose a reason for hiding this comment

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

+1, merging

@libenchao libenchao closed this in d9dd3ac Sep 17, 2023
@mihaibudiu mihaibudiu deleted the issue5988 branch September 17, 2023 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants