NobiGo commented on code in PR #4108:
URL: https://github.com/apache/calcite/pull/4108#discussion_r1896481992
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -4646,6 +4646,49 @@ static void checkRlikeFails(SqlOperatorFixture f) {
false);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6730">[CALCITE-6730]
+ * Add CONVERT function(enabled in Oracle library)</a>. */
+ @Test void testOracleConvertFunc() {
+ final SqlOperatorFixture f = fixture()
+ .setFor(SqlLibraryOperators.ORACLE_CONVERT, VM_JAVA)
+ .withLibrary(SqlLibrary.ORACLE);
+
+ final Consumer<SqlOperatorFixture> consumer = f0 -> {
+ f0.checkFails("convert('a', utf8, utf10)", "UTF10", false);
+ f0.checkFails("^convert(1, utf8, gbk)^",
+ "Invalid type 'INTEGER NOT NULL' in 'CONVERT' function\\. "
+ + "Only 'CHARACTER' type is supported",
+ false);
+ f0.checkType("convert('a', utf16, gbk)", "CHAR(1) NOT NULL");
+ f0.checkType("convert('a', utf16)", "CHAR(1) NOT NULL");
+ f0.checkType("convert(null, utf16, gbk)", "NULL");
Review Comment:
Maybe we can add test `convert('a', null, gbk)` or `convert('a', gbk, null)`.
##########
core/src/main/java/org/apache/calcite/sql2rel/StandardConvertletTable.java:
##########
@@ -853,15 +854,35 @@ protected RexNode convertFloorCeil(SqlRexContext cx,
SqlCall call) {
protected RexNode convertCharset(
@UnknownInitialization StandardConvertletTable this,
SqlRexContext cx, SqlCall call) {
+ final RexBuilder rexBuilder = cx.getRexBuilder();
final SqlParserPos pos = call.getParserPosition();
final SqlNode expr = call.operand(0);
- final String srcCharset = call.operand(1).toString();
- final String destCharset = call.operand(2).toString();
- final RexBuilder rexBuilder = cx.getRexBuilder();
- return rexBuilder.makeCall(pos, SqlStdOperatorTable.CONVERT,
- cx.convertExpression(expr),
- rexBuilder.makeLiteral(srcCharset),
- rexBuilder.makeLiteral(destCharset));
+ final SqlOperator op = call.getOperator();
+ final String srcCharset;
+ final String destCharset;
+ if (op == SqlStdOperatorTable.CONVERT) {
Review Comment:
We could use SWITCH and throw unsupported exceptions when we have another
function.
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -4646,6 +4646,49 @@ static void checkRlikeFails(SqlOperatorFixture f) {
false);
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6730">[CALCITE-6730]
+ * Add CONVERT function(enabled in Oracle library)</a>. */
+ @Test void testOracleConvertFunc() {
+ final SqlOperatorFixture f = fixture()
+ .setFor(SqlLibraryOperators.ORACLE_CONVERT, VM_JAVA)
+ .withLibrary(SqlLibrary.ORACLE);
+
+ final Consumer<SqlOperatorFixture> consumer = f0 -> {
+ f0.checkFails("convert('a', utf8, utf10)", "UTF10", false);
+ f0.checkFails("^convert(1, utf8, gbk)^",
+ "Invalid type 'INTEGER NOT NULL' in 'CONVERT' function\\. "
+ + "Only 'CHARACTER' type is supported",
+ false);
+ f0.checkType("convert('a', utf16, gbk)", "CHAR(1) NOT NULL");
+ f0.checkType("convert('a', utf16)", "CHAR(1) NOT NULL");
+ f0.checkType("convert(null, utf16, gbk)", "NULL");
+ f0.checkType("convert('', utf16, gbk)", "CHAR(0) NOT NULL");
+ f0.checkType("convert(cast(1 as varchar(2)), utf8, latin1)", "VARCHAR(2)
NOT NULL");
+
+ // cast check
+ f.check("select 'a' as alia\n"
+ + " from (values(true)) where cast(convert('col', latin1) as
char(3))='col'",
+ SqlTests.ANY_TYPE_CHECKER, 'a');
+ f.checkFails("select 'a' as alia\n"
+ + " from (values(true)) where ^cast(convert('col', latin1) as
char(3))=_GBK'col'^",
+ "Cannot apply operation '=' to strings with "
+ + "different charsets 'ISO-8859-1' and 'GBK'",
+ false);
+ // the result of convert('col', gbk) has GBK charset
+ // while CHAR(3) has ISO-8859-1 charset, which is not allowed to cast
+ f.checkFails("select 'a' as alia\n"
+ + " from (values(true)) where ^cast(convert('col', gbk) as
char(3))^=_GBK'col'",
+ "Cast function cannot convert value of type "
+ + "CHAR\\(3\\) CHARACTER SET \"GBK\" NOT NULL to type
CHAR\\(3\\) NOT NULL",
Review Comment:
We can add the target encoding format to the abnormal information like
`ISO-8859-1 charset`
##########
core/src/main/codegen/templates/Parser.jj:
##########
@@ -6319,10 +6319,17 @@ SqlNode BuiltinFunctionCall() :
}
|
<COMMA> e = SimpleIdentifier() { args.add(e); }
- <COMMA> e = SimpleIdentifier() { args.add(e); }
- <RPAREN> {
- return SqlStdOperatorTable.CONVERT.createCall(s.end(this),
args);
- }
+ (
+ <COMMA> e = SimpleIdentifier() { args.add(e); }
+ <RPAREN> {
+ SqlOperator op =
SqlStdOperatorTable.getConvertFuncByConformance(this.conformance);
+ return op.createCall(s.end(this), args);
+ }
+ |
+ <RPAREN> {
+ return
SqlLibraryOperators.ORACLE_CONVERT.createCall(s.end(this), args);
Review Comment:
Is this still a necessary modification? I thought
`getConvertFuncByConformance` could get the function when we use Oracle mode.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]