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 e922c820a7.

* unpatch Function

* move to a different location

* checkstyle
This commit is contained in:
Zoltan Haindrich 2023-10-25 22:37:30 +02:00 committed by GitHub
parent 7a25ee4fd9
commit f48263bbb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -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,8 +187,16 @@ class FunctionExpr implements Expr
@Override
public ExprEval eval(ObjectBinding 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
public boolean canVectorize(InputBindingInspector inspector)

View File

@ -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()
{