[ 
https://issues.apache.org/jira/browse/CALCITE-6142?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17791054#comment-17791054
 ] 

peng wu commented on CALCITE-6142:
----------------------------------

[~libenchao] I dived into the source code and  found  Quoting.BACK_TICK 
restricts the usage of  CharLiteralStyles:
{code:java}
  public static LexicalState forConfig(SqlParser.Config config) {
    switch (config.quoting()) {
    case BRACKET:
      return DEFAULT;
    case DOUBLE_QUOTE:
      return DQID;
    case BACK_TICK:
      if (config.conformance().allowHyphenInUnquotedTableName()
          && config.charLiteralStyles().equals(
              EnumSet.of(CharLiteralStyle.BQ_SINGLE,
                  CharLiteralStyle.BQ_DOUBLE))) {
        return BQID;
      }
      if (!config.conformance().allowHyphenInUnquotedTableName()
          && config.charLiteralStyles().equals(
              EnumSet.of(CharLiteralStyle.STANDARD))) {
        return BTID;
      }
      // fall through
    default:
      throw new AssertionError(config);
    }
  }
} {code}
In my test case, I need set Quoting.BACK_TICK and CharLiteralStyle.BQ_DOUBLE.

Therefore, I must set to conformance to BIG_QUERY. The parser config works for 
me must be exactly the same as below:
{code:java}
Set<CharLiteralStyle> charLiteralStyles = EnumSet.of(
        CharLiteralStyle.BQ_SINGLE,
        CharLiteralStyle.BQ_DOUBLE);
SqlParser.Config config = 
SqlParser.Config.DEFAULT
        .withConformance(BIG_QUERY)
        .withQuoting(Quoting.BACK_TICK)
        .withCharLiteralStyles(charLiteralStyles); {code}
It would be nice if we can make it more flexible, or let the developers known 
these constraints when they set the parameters.

> Double quote does not work as literal quote string in sql parsing
> -----------------------------------------------------------------
>
>                 Key: CALCITE-6142
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6142
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.32.0, 1.36.0
>            Reporter: peng wu
>            Priority: Critical
>
> When tried to parse spark sql express where double quote(") as literal quote 
> string, calcite sql parser threw an exception: 
>     org.apache.calcite.sql.parser.impl.ParseException: Encountered "\"" at 
> line 1, column 26.
>     Was expecting one of:
>     "ABS" ...
>     "ARRAY" ...    "AVG" ...
> The literal quote string is specified in sql parser config.
> Here is my test case:
> {code:java}
> public static void main(String[] args) throws Exception {
>     String sqlQuery = "case  when `Area` in (   \"Region1\", \"Region2\", 
> \"Region3\"  ) then `Sales`  else null end";
>     //create spark dialect
>     SqlDialect.Context context = SqlDialect.EMPTY_CONTEXT
>             .withDatabaseProduct(SqlDialect.DatabaseProduct.SPARK)
>             .withUnquotedCasing(Casing.UNCHANGED)
>             .withNullCollation(NullCollation.LOW)
>             .withIdentifierQuoteString("`")
>             .withLiteralQuoteString("\"");
>     SqlDialect sparkDialect = new SparkSqlDialect(context);
>     SqlParser.Config config = 
> sparkDialect.configureParser(SqlParser.config());
>     // Create an SQL parser
>     SqlParser parser = SqlParser.create(sqlQuery, config);
>     // Parse the query into an AST. Here the exception occurred
>     SqlNode sqlNode = parser.parseExpression();
>   } {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to