diff --git a/processing/src/main/java/org/apache/druid/math/expr/FunctionalExpr.java b/processing/src/main/java/org/apache/druid/math/expr/FunctionalExpr.java index b83efb2b2bc..3d5a7f511f9 100644 --- a/processing/src/main/java/org/apache/druid/math/expr/FunctionalExpr.java +++ b/processing/src/main/java/org/apache/druid/math/expr/FunctionalExpr.java @@ -21,6 +21,7 @@ package org.apache.druid.math.expr; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import org.apache.druid.error.DruidException; import org.apache.druid.java.util.common.StringUtils; import org.apache.druid.math.expr.vector.ExprVectorProcessor; @@ -186,7 +187,15 @@ class FunctionExpr implements Expr @Override public ExprEval eval(ObjectBinding bindings) { - return function.apply(args, bindings); + try { + return function.apply(args, bindings); + } + catch (DruidException | ExpressionValidationException e) { + throw e; + } + catch (Exception e) { + throw DruidException.defensive().build(e, "Invocation of function '%s' encountered exception.", name); + } } @Override diff --git a/processing/src/test/java/org/apache/druid/math/expr/FunctionTest.java b/processing/src/test/java/org/apache/druid/math/expr/FunctionTest.java index a093db167bb..d975ec069d5 100644 --- a/processing/src/test/java/org/apache/druid/math/expr/FunctionTest.java +++ b/processing/src/test/java/org/apache/druid/math/expr/FunctionTest.java @@ -23,10 +23,12 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import org.apache.druid.common.config.NullHandling; +import org.apache.druid.error.DruidException; import org.apache.druid.guice.NestedDataModule; import org.apache.druid.java.util.common.IAE; import org.apache.druid.java.util.common.Pair; import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.math.expr.Expr.ObjectBinding; import org.apache.druid.segment.column.TypeStrategies; import org.apache.druid.segment.column.TypeStrategiesTest; import org.apache.druid.segment.column.TypeStrategy; @@ -38,6 +40,7 @@ import org.junit.BeforeClass; import org.junit.Test; import javax.annotation.Nullable; + import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.ByteBuffer; @@ -46,6 +49,9 @@ import java.util.Arrays; import java.util.List; import java.util.Set; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + public class FunctionTest extends InitializedNullHandlingTest { private Expr.ObjectBinding bestEffortBindings; @@ -122,6 +128,36 @@ public class FunctionTest extends InitializedNullHandlingTest allBindings = new Expr.ObjectBinding[]{bestEffortBindings, typedBindings}; } + @Test + public void testUnknownErrorsAreWrappedAndReported() + { + final Expr expr = Parser.parse("abs(x)", ExprMacroTable.nil()); + + ObjectBinding bind = new ObjectBinding() + { + + @Override + public ExpressionType getType(String name) + { + return ExpressionType.LONG_ARRAY; + } + + @Override + public Object get(String name) + { + throw new RuntimeException("nested-exception"); + } + }; + DruidException e = Assert.assertThrows(DruidException.class, + () -> { + expr.eval(bind); + }); + + assertEquals("Invocation of function 'abs' encountered exception.", e.getMessage()); + assertNotNull(e.getCause()); + assertEquals("nested-exception", e.getCause().getMessage()); + } + @Test public void testCaseSimple() {