-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Conversation
e761b07
to
5aa5089
Compare
@julianhyde I think you wrote most of this code (long time ago), perhaps you can review this tiny PR? Thank you. |
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. |
There was a problem hiding this 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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 considernull
, they may suffer from the same problem.
@mihaibudiu What's your thought on this comment?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
5aa5089
to
34f52c5
Compare
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, merging
No description provided.