From 2bfcee58245912a0fa645d548abedc0608920d3b Mon Sep 17 00:00:00 2001 From: Clint Wylie Date: Fri, 21 May 2021 18:31:28 -0700 Subject: [PATCH] Fix issue with empty array converting to string expression instead of string array (#11270) --- .../main/java/org/apache/druid/math/expr/ExprEval.java | 2 +- .../java/org/apache/druid/math/expr/ExprEvalTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/druid/math/expr/ExprEval.java b/core/src/main/java/org/apache/druid/math/expr/ExprEval.java index 38bb045f5ef..43a1454ab7e 100644 --- a/core/src/main/java/org/apache/druid/math/expr/ExprEval.java +++ b/core/src/main/java/org/apache/druid/math/expr/ExprEval.java @@ -378,7 +378,7 @@ public abstract class ExprEval return new String[]{null}; } else { if (val != null) { - return val.toArray(); + return new String[0]; } return null; } diff --git a/core/src/test/java/org/apache/druid/math/expr/ExprEvalTest.java b/core/src/test/java/org/apache/druid/math/expr/ExprEvalTest.java index 3ce0d0cb319..0f1289baf61 100644 --- a/core/src/test/java/org/apache/druid/math/expr/ExprEvalTest.java +++ b/core/src/test/java/org/apache/druid/math/expr/ExprEvalTest.java @@ -273,6 +273,16 @@ public class ExprEvalTest extends InitializedNullHandlingTest ); } + @Test + public void testEmptyArrayFromList() + { + // empty arrays will materialize from JSON into an empty list, which coerce list to array will make into Object[] + // make sure we can handle it + ExprEval someEmptyArray = ExprEval.bestEffortOf(new ArrayList<>()); + Assert.assertTrue(someEmptyArray.isArray()); + Assert.assertEquals(0, someEmptyArray.asArray().length); + } + private void assertExpr(int position, Object expected) { assertExpr(position, ExprEval.bestEffortOf(expected));