Skip to content
Permalink
Browse files
Improve exception message for native binary operators (#12335)
* Improve exception message

* Update message
  • Loading branch information
FrankChen021 committed Apr 28, 2022
1 parent 7b89682 commit df074f2f965c71d9abfc3f5aed6d189357ce600f
Showing 2 changed files with 39 additions and 2 deletions.
@@ -21,6 +21,7 @@

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 @@ public ExprEval eval(ObjectBinding bindings)

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);
@@ -80,7 +80,9 @@ public void setup()
.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 void testMVToArrayWithNoArgs()
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());

0 comments on commit df074f2

Please sign in to comment.