You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by am...@apache.org on 2021/01/06 21:42:31 UTC

[calcite] branch master updated: [CALCITE-4450] ElasticSearch query with varchar literal projection fails with JsonParseException

This is an automated email from the ASF dual-hosted git repository.

amaliujia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new be649b5  [CALCITE-4450] ElasticSearch query with varchar literal projection fails with JsonParseException
be649b5 is described below

commit be649b52eabb33d7252a146c50b769d271241dcd
Author: Volodymyr Vysotskyi <vv...@gmail.com>
AuthorDate: Thu Dec 31 00:32:31 2020 +0200

    [CALCITE-4450] ElasticSearch query with varchar literal projection fails with JsonParseException
---
 .../adapter/elasticsearch/ElasticsearchRules.java  | 13 +++++++----
 .../adapter/elasticsearch/Projection2Test.java     | 26 ++++++++++++++++++++--
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java
index ccf41e6..78261b9 100644
--- a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java
+++ b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchRules.java
@@ -131,6 +131,10 @@ class ElasticsearchRules {
         ? s.substring(1, s.length() - 1) : s;
   }
 
+  private static String escapeSpecialSymbols(String s) {
+    return s.replace("\\", "\\\\").replace("\"", "\\\"");
+  }
+
   /**
    * Translator from {@link RexNode} to strings in Elasticsearch's expression
    * language.
@@ -149,10 +153,11 @@ class ElasticsearchRules {
       if (literal.getValue() == null) {
         return "null";
       }
-      return "\"literal\":\""
-        + RexToLixTranslator.translateLiteral(literal, literal.getType(),
-          typeFactory, RexImpTable.NullAs.NOT_POSSIBLE)
-        + "\"";
+      return "\"literal\":"
+          + quote(
+          escapeSpecialSymbols(
+              RexToLixTranslator.translateLiteral(literal, literal.getType(),
+                  typeFactory, RexImpTable.NullAs.NOT_POSSIBLE).toString()));
     }
 
     @Override public String visitInputRef(RexInputRef inputRef) {
diff --git a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/Projection2Test.java b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/Projection2Test.java
index d17faea..9f060e2 100644
--- a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/Projection2Test.java
+++ b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/Projection2Test.java
@@ -28,7 +28,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableMap;
 
 import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.parallel.ResourceAccessMode;
 import org.junit.jupiter.api.parallel.ResourceLock;
@@ -50,7 +49,6 @@ import static org.junit.jupiter.api.Assertions.fail;
 /**
  * Checks renaming of fields (also upper, lower cases) during projections.
  */
-@Disabled("RestClient often timeout in PR CI")
 @ResourceLock(value = "elasticsearch-scrolls", mode = ResourceAccessMode.READ)
 class Projection2Test {
 
@@ -213,6 +211,30 @@ class Projection2Test {
 
   }
 
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-4450">[CALCITE-4450]
+   * ElasticSearch query with varchar literal projection fails with JsonParseException</a>. */
+  @Test void projectionStringLiteral() {
+    CalciteAssert.that()
+        .with(newConnectionFactory())
+        .query(
+            String.format(Locale.ROOT, "select 'foo' as \"lit\"\n"
+                + "from \"elastic\".\"%s\"", NAME))
+        .returns("lit=foo\n");
+  }
+
+  /** Test case for
+   * <a href="https://issues.apache.org/jira/browse/CALCITE-4450">[CALCITE-4450]
+   * ElasticSearch query with varchar literal projection fails with JsonParseException</a>. */
+  @Test void projectionStringLiteralAndColumn() {
+    CalciteAssert.that()
+        .with(newConnectionFactory())
+        .query(
+            String.format(Locale.ROOT, "select 'foo\\\"bar\\\"' as \"lit\", _MAP['a'] as \"a\"\n"
+                + "from \"elastic\".\"%s\"", NAME))
+        .returns("lit=foo\\\"bar\\\"; a=1\n");
+  }
+
   /**
    * Allows values to contain regular expressions instead of exact values.
    * <pre>