From f48263bbb3f1f258cf435b39ddf94a24ed32c741 Mon Sep 17 00:00:00 2001 From: Zoltan Haindrich Date: Wed, 25 Oct 2023 22:37:30 +0200 Subject: [PATCH] Report function name for unknown exceptions during execution (#14987) * provide function name when unknown exceptions are encountered * fix keywords/etc * fix keywrod order - regex excercise * add test * add check&fix keywords * decoupledIgnore * Revert "decoupledIgnore" This reverts commit e922c820a7d563ca49c9c686644bed967c42cb4b. * unpatch Function * move to a different location * checkstyle --- .../druid/math/expr/FunctionalExpr.java | 11 +++++- .../apache/druid/math/expr/FunctionTest.java | 36 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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 b83efb2b2bcc..3d5a7f511f9e 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 @@ 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 @@ public String toString() @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 a093db167bbe..d975ec069d55 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.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.Test; import javax.annotation.Nullable; + import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.ByteBuffer; @@ -46,6 +49,9 @@ 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 void setup() 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() {