You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by fr...@apache.org on 2022/04/28 02:20:22 UTC

[druid] branch master updated: Improve exception message for native binary operators (#12335)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new df074f2f96 Improve exception message for native binary operators (#12335)
df074f2f96 is described below

commit df074f2f965c71d9abfc3f5aed6d189357ce600f
Author: Frank Chen <fr...@outlook.com>
AuthorDate: Thu Apr 28 10:20:16 2022 +0800

    Improve exception message for native binary operators (#12335)
    
    * Improve exception message
    
    * Update message
---
 .../apache/druid/math/expr/BinaryOperatorExpr.java |  7 ++++-
 .../org/apache/druid/math/expr/FunctionTest.java   | 34 +++++++++++++++++++++-
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/druid/math/expr/BinaryOperatorExpr.java b/core/src/main/java/org/apache/druid/math/expr/BinaryOperatorExpr.java
index d2455df18f..8932a46811 100644
--- a/core/src/main/java/org/apache/druid/math/expr/BinaryOperatorExpr.java
+++ b/core/src/main/java/org/apache/druid/math/expr/BinaryOperatorExpr.java
@@ -21,6 +21,7 @@ package org.apache.druid.math.expr;
 
 import com.google.common.collect.ImmutableSet;
 import org.apache.druid.common.config.NullHandling;
+import org.apache.druid.java.util.common.IAE;
 import org.apache.druid.java.util.common.StringUtils;
 import org.apache.druid.segment.column.Types;
 
@@ -151,7 +152,11 @@ abstract class BinaryEvalOpExprBase extends BinaryOpExprBase
 
   protected ExprEval evalString(@Nullable String left, @Nullable String right)
   {
-    throw new IllegalArgumentException("unsupported type " + ExprType.STRING);
+    throw new IAE("operator '%s' in expression (%s %s %s) is not supported on type 'string'.",
+                  this.op,
+                  this.left.stringify(),
+                  this.op,
+                  this.right.stringify());
   }
 
   protected abstract long evalLong(long left, long right);
diff --git a/core/src/test/java/org/apache/druid/math/expr/FunctionTest.java b/core/src/test/java/org/apache/druid/math/expr/FunctionTest.java
index dfc399b395..03d2e50d1c 100644
--- a/core/src/test/java/org/apache/druid/math/expr/FunctionTest.java
+++ b/core/src/test/java/org/apache/druid/math/expr/FunctionTest.java
@@ -80,7 +80,9 @@ public class FunctionTest extends InitializedNullHandlingTest
                     .put("a", new String[] {"foo", "bar", "baz", "foobar"})
                     .put("b", new Long[] {1L, 2L, 3L, 4L, 5L})
                     .put("c", new Double[] {3.1, 4.2, 5.3})
-                    .put("someComplex", new TypeStrategiesTest.NullableLongPair(1L, 2L));
+                    .put("someComplex", new TypeStrategiesTest.NullableLongPair(1L, 2L))
+                    .put("str1", "v1")
+                    .put("str2", "v2");
     bindings = InputBindings.withMap(builder.build());
   }
 
@@ -973,6 +975,36 @@ public class FunctionTest extends InitializedNullHandlingTest
     assertArrayExpr("mv_to_array()", null);
   }
 
+  @Test
+  public void testPlusOnString()
+  {
+    assertExpr("str1 + str2", "v1v2");
+  }
+
+  @Test
+  public void testMultiplyOnString()
+  {
+    expectedException.expect(IAE.class);
+    expectedException.expectMessage("operator '*' in expression (\"str1\" * \"str2\") is not supported on type 'string'.");
+    assertExpr("str1 * str2", null);
+  }
+
+  @Test
+  public void testMinusOnString()
+  {
+    expectedException.expect(IAE.class);
+    expectedException.expectMessage("operator '-' in expression (\"str1\" - \"str2\") is not supported on type 'string'.");
+    assertExpr("str1 - str2", null);
+  }
+
+  @Test
+  public void testDivOnString()
+  {
+    expectedException.expect(IAE.class);
+    expectedException.expectMessage("operator '/' in expression (\"str1\" / \"str2\") is not supported on type 'string'.");
+    assertExpr("str1 / str2", null);
+  }
+
   private void assertExpr(final String expression, @Nullable final Object expectedResult)
   {
     final Expr expr = Parser.parse(expression, ExprMacroTable.nil());


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org