diff --git a/processing/src/main/java/io/druid/query/extraction/JavascriptDimExtractionFn.java b/processing/src/main/java/io/druid/query/extraction/JavascriptDimExtractionFn.java index 1878df5479e..f552ef4e9cd 100644 --- a/processing/src/main/java/io/druid/query/extraction/JavascriptDimExtractionFn.java +++ b/processing/src/main/java/io/druid/query/extraction/JavascriptDimExtractionFn.java @@ -51,7 +51,8 @@ public class JavascriptDimExtractionFn implements DimExtractionFn cx = contextFactory.enterContext(); } - return Context.toString(fn.call(cx, scope, scope, new String[]{input})); + final Object res = fn.call(cx, scope, scope, new String[]{input}); + return res != null ? Context.toString(res) : null; } }; } diff --git a/processing/src/test/java/io/druid/query/extraction/extraction/JavascriptDimExtractionFnTest.java b/processing/src/test/java/io/druid/query/extraction/extraction/JavascriptDimExtractionFnTest.java index cc5a1b26b4e..22e403588fd 100644 --- a/processing/src/test/java/io/druid/query/extraction/extraction/JavascriptDimExtractionFnTest.java +++ b/processing/src/test/java/io/druid/query/extraction/extraction/JavascriptDimExtractionFnTest.java @@ -20,6 +20,7 @@ package io.druid.query.extraction.extraction; import com.google.common.collect.Iterators; +import com.google.common.collect.Lists; import io.druid.query.extraction.DimExtractionFn; import io.druid.query.extraction.JavascriptDimExtractionFn; import org.junit.Assert; @@ -52,6 +53,21 @@ public class JavascriptDimExtractionFnTest } } + @Test + public void testCastingAndNull() + { + String function = "function(x) {\n x = Number(x);\n if(isNaN(x)) return null;\n return Math.floor(x / 5) * 5;\n}"; + DimExtractionFn dimExtractionFn = new JavascriptDimExtractionFn(function); + + Iterator it = Iterators.forArray("0", "5", "5", "10", null); + + for(String str : Lists.newArrayList("1", "5", "6", "10", "CA")) { + String res = dimExtractionFn.apply(str); + String expected = it.next(); + Assert.assertEquals(expected, res); + } + } + @Test public void testJavascriptRegex() {