Merge pull request #321 from metamx/js-dimextraction-null-fix

fix JS dim extraction null handling + test case
This commit is contained in:
fjy 2013-12-10 17:14:56 -08:00
commit 64e75becba
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()
{