Skip to content

Commit

Permalink
Report function name for unknown exceptions during execution (#14987)
Browse files Browse the repository at this point in the history
* 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 e922c82.

* unpatch Function

* move to a different location

* checkstyle
  • Loading branch information
kgyrtkirk authored Oct 25, 2023
1 parent 7a25ee4 commit f48263b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +40,7 @@
import org.junit.Test;

import javax.annotation.Nullable;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.ByteBuffer;
Expand All @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit f48263b

Please sign in to comment.