1

I use driver manager (eg. jdbc:calcite:inline) to get a calcite connection and run sql with it, but calcite's default encode character set(ISO) don't support Chinese characters, I want to find a method to change the encode character set.

I tried the following two sql:

  1. select * from tb where id= '啊'
  2. select * from tb where id= _UTF16'啊'

Firstly,I run a sql(select * from tb where id= '啊') and receive a error that is "org.apache.calcite.runtime.CalciteException: Failed to encode '啊' in character set 'ISO-8859-1'".

Secondly, I add a character set to the sql(select * from tb where id= _UTF16'啊'),but I receive another error that is "SqlValidatorException: Cannot apply = to the two different charsets ISO-8859-1 and UTF-16LE".

Long.zhao
  • 1,085
  • 2
  • 11
  • 16
  • I have resolved this problem, see https://stackoverflow.com/questions/33888393/org-apache-calcite-runtime-calciteexception-failed-to-encode-%E5%85%A8%E5%9B%BD-in-character – Long.zhao May 29 '19 at 09:12

2 Answers2

1

You should set the property calcite.default.charset to whatever character set you want to use. That said, I'm not sure this will solve all your problems. Support for other character sets is really a work in progress. See this discussion on the project mailing list.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
0

Background:

  • calcite version is 1.27.0
  • Execute queries over JdbcSchema like SELECT * FROM t WHERE title IN ('中文', '简体')

Solution:

Step 1: Override the org.apache.calcite.sql.SqlDialect by copying the source to your project. And then modify the quoteStringLiteral method (about line 420):

public void quoteStringLiteral(StringBuilder buf, @Nullable String charsetName, String val) {
    
    // remove `if (...) quoteStringLiteralUnicode(...)`

    if (charsetName != null) {
        buf.append("_");
        buf.append(charsetName);
    }
    buf.append(literalQuoteString);
    buf.append(val.replace(literalEndQuoteString, literalEscapedQuote));
    buf.append(literalEndQuoteString);
}

Step 2: Create a property file named saffron.properties under the project resources directory with the following content:

calcite.default.charset = utf8
wizawu
  • 1,881
  • 21
  • 33