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

hyuan 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 ce86af8  [CALCITE-4177] RelJson should throw if asked to deserialize a 
call to an unknown operator (Xzh & Wang Yanlin)
ce86af8 is described below

commit ce86af83caae335d1b47def55354d42a517f45d0
Author: xzh <[email protected]>
AuthorDate: Tue Sep 14 20:32:36 2021 +0800

    [CALCITE-4177] RelJson should throw if asked to deserialize a call to an 
unknown operator (Xzh & Wang Yanlin)
    
    Close #2520
---
 .../apache/calcite/rel/externalize/RelJson.java    |  3 ++-
 .../apache/calcite/runtime/CalciteResource.java    |  4 ++++
 .../calcite/runtime/CalciteResource.properties     |  1 +
 .../org/apache/calcite/plan/RelWriterTest.java     | 27 ++++++++++++++++++++++
 4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java 
b/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
index 88f9d99..6250240 100644
--- a/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
+++ b/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
@@ -78,6 +78,7 @@ import java.util.Map;
 import java.util.Set;
 
 import static org.apache.calcite.rel.RelDistributions.EMPTY;
+import static org.apache.calcite.util.Static.RESOURCE;
 
 import static java.util.Objects.requireNonNull;
 
@@ -743,7 +744,7 @@ public class RelJson {
     if (class_ != null) {
       return AvaticaUtils.instantiatePlugin(SqlOperator.class, class_);
     }
-    return null;
+    throw RESOURCE.noOperator(name, kind, syntax).ex();
   }
 
   @Nullable SqlAggFunction toAggregation(Map<String, ? extends @Nullable 
Object> map) {
diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java 
b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index ec38767..7f9d54f 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -967,4 +967,8 @@ public interface CalciteResource {
 
   @BaseMessage("Different length for bitwise operands: the first: 
{0,number,#}, the second: {1,number,#}")
   ExInst<CalciteException> differentLengthForBitwiseOperands(int l0, int l1);
+
+  @BaseMessage("No operator for ''{0}'' with kind: ''{1}'', syntax: ''{2}'' 
during JSON deserialization")
+  ExInst<CalciteException> noOperator(String name, String kind, String syntax);
+
 }
diff --git 
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties 
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index 85768e7..c166ab4 100644
--- 
a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++ 
b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -316,4 +316,5 @@ InvalidInputForExtractValue=Invalid input for EXTRACTVALUE: 
xml: ''{0}'', xpath
 InvalidInputForExtractXml=Invalid input for EXTRACT xpath: ''{0}'', namespace: 
''{1}''
 InvalidInputForExistsNode=Invalid input for EXISTSNODE xpath: ''{0}'', 
namespace: ''{1}''
 DifferentLengthForBitwiseOperands=Different length for bitwise operands: the 
first: {0,number,#}, the second: {1,number,#}
+NoOperator=No operator for ''{0}'' with kind: ''{1}'', syntax: ''{2}'' during 
JSON deserialization
 # End CalciteResource.properties
diff --git a/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java 
b/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
index c0720a2..13330d6 100644
--- a/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
+++ b/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
@@ -85,6 +85,7 @@ import static org.apache.calcite.test.Matchers.isLinux;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 /**
  * Unit test for {@link org.apache.calcite.rel.externalize.RelJson}.
@@ -692,6 +693,32 @@ class RelWriterTest {
     assertThat(s, isLinux(expected));
   }
 
+  @Test void testDeserializeInvalidOperatorName() {
+    final FrameworkConfig config = RelBuilderTest.config().build();
+    final RelBuilder builder = RelBuilder.create(config);
+    final RelNode rel = builder
+        .scan("EMP")
+        .project(
+            builder.field("JOB"),
+            builder.field("SAL"))
+        .aggregate(
+            builder.groupKey("JOB"),
+            builder.max("max_sal", builder.field("SAL")),
+            builder.min("min_sal", builder.field("SAL")))
+        .project(
+            builder.field("max_sal"),
+            builder.field("min_sal"))
+        .build();
+    final RelJsonWriter jsonWriter = new RelJsonWriter();
+    rel.explain(jsonWriter);
+    // mock a non exist SqlOperator
+    String relJson = jsonWriter.asString().replace("\"name\": \"MAX\"", 
"\"name\": \"MAXS\"");
+    assertThrows(RuntimeException.class,
+        () -> deserializeAndDumpToTextFormat(getSchema(rel), relJson),
+        "org.apache.calcite.runtime.CalciteException: "
+            + "No operator for 'MAXS' with kind: 'MAX', syntax: 'FUNCTION' 
during JSON deserialization");
+  }
+
   @Test void testAggregateWithoutAlias() {
     final FrameworkConfig config = RelBuilderTest.config().build();
     final RelBuilder builder = RelBuilder.create(config);

Reply via email to