Improve exception message for native binary operators (#12335)

* Improve exception message

* Update message
This commit is contained in:
Frank Chen 2022-04-28 10:20:16 +08:00 committed by GitHub
parent 7b89682bbe
commit df074f2f96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 2 deletions

View File

@ -21,6 +21,7 @@ package org.apache.druid.math.expr;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.apache.druid.common.config.NullHandling; 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.java.util.common.StringUtils;
import org.apache.druid.segment.column.Types; 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) 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); protected abstract long evalLong(long left, long right);

View File

@ -80,7 +80,9 @@ public class FunctionTest extends InitializedNullHandlingTest
.put("a", new String[] {"foo", "bar", "baz", "foobar"}) .put("a", new String[] {"foo", "bar", "baz", "foobar"})
.put("b", new Long[] {1L, 2L, 3L, 4L, 5L}) .put("b", new Long[] {1L, 2L, 3L, 4L, 5L})
.put("c", new Double[] {3.1, 4.2, 5.3}) .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()); bindings = InputBindings.withMap(builder.build());
} }
@ -973,6 +975,36 @@ public class FunctionTest extends InitializedNullHandlingTest
assertArrayExpr("mv_to_array()", null); 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) private void assertExpr(final String expression, @Nullable final Object expectedResult)
{ {
final Expr expr = Parser.parse(expression, ExprMacroTable.nil()); final Expr expr = Parser.parse(expression, ExprMacroTable.nil());