Skip to content

Commit

Permalink
[CALCITE-6006] RelToSqlConverter loses charset information
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu authored and JiajunBernoulli committed Oct 13, 2023
1 parent db60219 commit 485a5d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.calcite.rel.rel2sql;

import org.apache.calcite.config.CalciteSystemProperty;
import org.apache.calcite.linq4j.Ord;
import org.apache.calcite.linq4j.tree.Expressions;
import org.apache.calcite.plan.RelOptUtil;
Expand Down Expand Up @@ -90,6 +91,7 @@
import org.apache.calcite.sql.validate.SqlValidatorUtil;
import org.apache.calcite.util.DateString;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.NlsString;
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.RangeSets;
import org.apache.calcite.util.Sarg;
Expand Down Expand Up @@ -1409,8 +1411,20 @@ public static SqlNode toSql(RexLiteral literal) {
() -> "literal " + literal
+ " has null SqlTypeFamily, and is SqlTypeName is " + typeName);
switch (family) {
case CHARACTER:
case CHARACTER: {
final NlsString value = literal.getValueAs(NlsString.class);
if (value != null) {
final String defaultCharset = CalciteSystemProperty.DEFAULT_CHARSET.value();
final String charsetName = value.getCharsetName();
if (!defaultCharset.equals(charsetName)) {
// Set the charset only if it is not the same as the default charset
return SqlLiteral.createCharString(
castNonNull(value).getValue(), charsetName, POS);
}
}
// Create a string without specifying a charset
return SqlLiteral.createCharString((String) castNonNull(literal.getValue2()), POS);
}
case NUMERIC:
case EXACT_NUMERIC:
return SqlLiteral.createExactNumeric(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RuleSet;
import org.apache.calcite.tools.RuleSets;
import org.apache.calcite.util.ConversionUtil;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.TestUtil;
import org.apache.calcite.util.Util;
Expand Down Expand Up @@ -282,6 +283,17 @@ private static String toSql(RelNode root, SqlDialect dialect,
sql(query).ok(expected);
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6006">[CALCITE-6006]</a>
* RelToSqlConverter loses charset information. */
@Test void testCharset() {
sql("select _UTF8'\u4F60\u597D'")
.withMysql() // produces a simpler output query
.ok("SELECT _UTF-8'\u4F60\u597D'");
sql("select _UTF16'" + ConversionUtil.TEST_UNICODE_STRING + "'")
.withMysql()
.ok("SELECT _UTF-16LE'" + ConversionUtil.TEST_UNICODE_STRING + "'");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4321">[CALCITE-4321]
* JDBC adapter omits FILTER (WHERE ...) expressions when generating SQL</a>
Expand Down

0 comments on commit 485a5d0

Please sign in to comment.