mihaibudiu commented on code in PR #3558:
URL: https://github.com/apache/calcite/pull/3558#discussion_r1874626256
##########
core/src/test/java/org/apache/calcite/test/JdbcTest.java:
##########
@@ -6998,7 +6998,51 @@ private void checkGetTimestamp(Connection con) throws
SQLException {
with.query(
"select * from \"employee\" where \"full_name\" =
_UTF16'\u82f1\u56fd'")
.throws_(
- "Cannot apply = to the two different charsets ISO-8859-1 and
UTF-16LE");
+ "Cannot apply operation '=' to strings with different charsets
'ISO-8859-1' and 'UTF-16LE'");
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6146">[CALCITE-6146]
+ * Target charset should be used when comparing two strings through
+ * CONVERT/TRANSLATE function during validation</a>. */
+ @Test void testStringComparisonWithConvertFunc() {
+ CalciteAssert.AssertThat with = CalciteAssert.hr();
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where convert(\"name\" using GBK)=_GBK'Eric'")
+ .returns("name=Eric; empid=200\n");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where _BIG5'Eric'=translate(\"name\" using BIG5)")
+ .returns("name=Eric; empid=200\n");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where translate(null using UTF8) is null")
+ .returns("name=Bill; empid=100\n"
+ + "name=Eric; empid=200\n"
+ + "name=Sebastian; empid=150\n"
+ + "name=Theodore; empid=110\n");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where _BIG5'Eric'=translate(\"name\" using LATIN1)")
+ .throws_("Cannot apply operation '=' to strings with "
+ + "different charsets 'Big5' and 'ISO-8859-1'");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where convert(convert(\"name\" using GBK) using
BIG5)=_BIG5'Eric'")
+ .returns("name=Eric; empid=200\n");
+
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where convert(\"name\", UTF8, GBK)=_GBK'Sebastian'")
+ .returns("name=Sebastian; empid=150\n");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where _BIG5'Sebastian'=convert(\"name\", UTF8, BIG5)")
+ .returns("name=Sebastian; empid=150\n");
+
+ // check cast
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where cast(convert(\"name\" using LATIN1) as char(5))='Eric'")
+ .returns("name=Eric; empid=200\n");
+ with.query("select \"name\", \"empid\" from \"hr\".\"emps\"\n"
+ + "where cast(convert(\"name\" using GBK) as char(5))='Eric'")
Review Comment:
why is this the expected result?
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlTranslateFunction.java:
##########
@@ -78,6 +85,40 @@ protected SqlTranslateFunction(String name) {
super.validateQuantifier(validator, call);
}
+ @Override public RelDataType inferReturnType(
+ SqlOperatorBinding opBinding) {
+ final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
+ final RelDataType ret = opBinding.getOperandType(0);
+ if (SqlTypeUtil.isNull(ret)) {
+ return ret;
+ }
+ final String descCharsetName;
+ if (opBinding instanceof SqlCallBinding) {
+ descCharsetName = ((SqlCallBinding)
opBinding).getCall().operand(1).toString();
+ } else {
+ descCharsetName = ((RexCallBinding)
opBinding).getStringLiteralOperand(1);
+ }
+ assert descCharsetName != null;
+ Charset descCharset = SqlUtil.getCharset(descCharsetName);
+ return typeFactory.createTypeWithCharsetAndCollation(ret, descCharset,
getCollation(ret));
+ }
+
+ @Override public RelDataType deriveType(SqlValidator validator,
+ SqlValidatorScope scope, SqlCall call) {
+ // special case for TRANSLATE: don't need to derive type for Charsets
Review Comment:
it's not obvious what this comment refers to. Special compared to what?
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -4479,6 +4479,78 @@ static void checkRlikeFails(SqlOperatorFixture f) {
f.checkType("convert(cast(1 as varchar(2)), utf8, latin1)", "VARCHAR(2)
NOT NULL");
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6146">[CALCITE-6146]
+ * Target charset should be used when comparing two strings through
+ * CONVERT/TRANSLATE function during validation</a>. */
+ @Test void testStringComparisonWithConvertFunc() {
+ final SqlOperatorFixture f = fixture();
+ f.setFor(SqlStdOperatorTable.CONVERT, VM_JAVA);
+ f.check("select 'a' as col\n"
Review Comment:
this example is confusing, because the column name is col and the literal is
also col.
Can you change one of them?
--
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]