fix JS dim extraction null handling + test case

This commit is contained in:
Xavier Léauté 2013-12-09 18:22:30 -08:00
parent 98a0f40227
commit bb69f8adcd
2 changed files with 18 additions and 1 deletions

View File

@ -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;
}
};
}

View File

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