DRILL-1141:ISNUMERIC function
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/0a28365a Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/0a28365a Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/0a28365a Branch: refs/heads/master Commit: 0a28365a252491b70c9a39a0d49dd68114fdc8fa Parents: 5fe54e0 Author: Yash Sharma <[email protected]> Authored: Mon Nov 24 11:43:43 2014 -0800 Committer: Mehant Baid <[email protected]> Committed: Mon Nov 24 11:48:09 2014 -0800 ---------------------------------------------------------------------- .../src/main/codegen/data/NumericTypes.tdd | 32 ++++- .../templates/NumericFunctionsTemplates.java | 120 +++++++++++++++++++ .../exec/fn/impl/TestNewMathFunctions.java | 7 ++ .../functions/testIsNumericFunction.json | 35 ++++++ 4 files changed, 190 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/0a28365a/exec/java-exec/src/main/codegen/data/NumericTypes.tdd ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/data/NumericTypes.tdd b/exec/java-exec/src/main/codegen/data/NumericTypes.tdd index f37a3dd..90b432e 100644 --- a/exec/java-exec/src/main/codegen/data/NumericTypes.tdd +++ b/exec/java-exec/src/main/codegen/data/NumericTypes.tdd @@ -14,7 +14,31 @@ # See the License for the specific language governing permissions and # limitations under the License. -{ - numeric: ["Int", "BigInt", "TinyInt", "SmallInt", "UInt1", "UInt2", "UInt4", "UInt8", "Float4", "Float8", - "Decimal9", "Decimal18", "Decimal28Dense", "Decimal28Sparse", "Decimal38Dense", "Decimal38Sparse"] -} \ No newline at end of file + { + numeric: ["Int", "BigInt", "TinyInt", "SmallInt", "UInt1", "UInt2", "UInt4", "UInt8", "Float4", "Float8", + "Decimal9", "Decimal18", "Decimal28Dense", "Decimal28Sparse", "Decimal38Dense", "Decimal38Sparse"], + + numericFunctions: [ + {className: "IsNumeric", funcName: "isnumeric", aliasName: "", outputType: "Int", types: [ + {inputType: "BigInt", intype: "numeric"}, + {inputType: "NullableBigInt", intype: "numeric"}, + {inputType: "Int", intype: "numeric"}, + {inputType: "NullableInt", intype: "numeric"}, + {inputType: "SmallInt", intype: "numeric"}, + {inputType: "NullableSmallInt", intype: "numeric"}, + {inputType: "TinyInt", intype: "numeric"}, + {inputType: "NullableTinyInt", intype: "numeric"}, + {inputType: "UInt1", intype: "numeric"}, + {inputType: "NullableUInt1", intype: "numeric"}, + {inputType: "UInt2", intype: "numeric"}, + {inputType: "NullableUInt2", intype: "numeric"}, + {inputType: "UInt4", intype: "numeric"}, + {inputType: "NullableUInt4", intype: "numeric"}, + {inputType: "UInt8", intype: "numeric"}, + {inputType: "NullableUInt8", intype: "numeric"}, + {inputType: "VarChar", intype: "char"}, + {inputType: "NullableVarChar", intype: "char"} + ] + } + ] +} http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/0a28365a/exec/java-exec/src/main/codegen/templates/NumericFunctionsTemplates.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/templates/NumericFunctionsTemplates.java b/exec/java-exec/src/main/codegen/templates/NumericFunctionsTemplates.java new file mode 100644 index 0000000..65ac923 --- /dev/null +++ b/exec/java-exec/src/main/codegen/templates/NumericFunctionsTemplates.java @@ -0,0 +1,120 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +<@pp.dropOutputFile /> + + + +<#list numericTypes.numericFunctions as numericFunc> +<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/${numericFunc.className}Functions.java" /> + +<#include "/@includes/license.ftl" /> + +<#-- A utility class that is used to generate java code for numeric functions --> + +/* + * This class is automatically generated from NumericTypes.tdd using FreeMarker. + */ + +package org.apache.drill.exec.expr.fn.impl; + +import org.apache.drill.exec.expr.DrillSimpleFunc; +import org.apache.drill.exec.expr.annotations.FunctionTemplate; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling; +import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope; +import org.apache.drill.exec.expr.annotations.Output; +import org.apache.drill.exec.expr.annotations.Param; +import org.apache.drill.exec.expr.annotations.Workspace; +import org.apache.drill.exec.expr.holders.BigIntHolder; +import org.apache.drill.exec.expr.holders.NullableBigIntHolder; +import org.apache.drill.exec.expr.holders.IntHolder; +import org.apache.drill.exec.expr.holders.NullableIntHolder; +import org.apache.drill.exec.expr.holders.SmallIntHolder; +import org.apache.drill.exec.expr.holders.NullableSmallIntHolder; +import org.apache.drill.exec.expr.holders.TinyIntHolder; +import org.apache.drill.exec.expr.holders.NullableTinyIntHolder; +import org.apache.drill.exec.expr.holders.UInt1Holder; +import org.apache.drill.exec.expr.holders.NullableUInt1Holder; +import org.apache.drill.exec.expr.holders.UInt2Holder; +import org.apache.drill.exec.expr.holders.NullableUInt2Holder; +import org.apache.drill.exec.expr.holders.UInt4Holder; +import org.apache.drill.exec.expr.holders.NullableUInt4Holder; +import org.apache.drill.exec.expr.holders.UInt8Holder; +import org.apache.drill.exec.expr.holders.NullableUInt8Holder; +import org.apache.drill.exec.expr.holders.VarCharHolder; +import org.apache.drill.exec.expr.holders.NullableVarCharHolder; +import org.apache.drill.exec.record.RecordBatch; + +@SuppressWarnings("unused") + +public class ${numericFunc.className}Functions { + static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(${numericFunc.className}Functions.class); + +<#list numericFunc.types as type> + +<#if numericFunc.aliasName == ""> +@FunctionTemplate(name = "${numericFunc.funcName}", scope = FunctionTemplate.FunctionScope.SIMPLE) +<#else> +@FunctionTemplate(names = {"${numericFunc.funcName}", "${numericFunc.aliasName}"}, scope = FunctionTemplate.FunctionScope.SIMPLE) +</#if> +public static class ${type.inputType}${numericFunc.className} implements DrillSimpleFunc{ + + @Param ${type.inputType}Holder in; + @Output ${numericFunc.outputType}Holder out; + <#if type.intype != "numeric"> + @Workspace java.util.regex.Pattern pattern; + @Workspace java.util.regex.Matcher matcher; + </#if> + + public void setup(RecordBatch b) { + <#if type.intype != "numeric"> + pattern = java.util.regex.Pattern.compile("[-+]?\\d+(\\.\\d+)?"); + matcher = pattern.matcher(""); + </#if> + } + + public void eval() { + + <#if type.intype == "char"> + <#if type.inputType?matches("^Nullable.*")> + if(in.isSet==0){ + out.value = 0; + return; + } + </#if> + String s = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(in.start, in.end, in.buffer); + out.value = matcher.reset(s).matches() ? 1 : 0; + + + <#elseif type.intype == "numeric"> + <#if type.inputType?matches("^Nullable.*")> + if(in.isSet==0){ + out.value = 0; + return; + } + </#if> + out.value = 1; + </#if> + + } +} + + +</#list> +} +</#list> http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/0a28365a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewMathFunctions.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewMathFunctions.java b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewMathFunctions.java index f715747..880184e 100644 --- a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewMathFunctions.java +++ b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewMathFunctions.java @@ -141,4 +141,11 @@ public class TestNewMathFunctions { runTest(bitContext, connection, expected, "functions/testDivModTruncFunctions.json"); } + @Test + public void testIsNumeric(@Injectable final DrillbitContext bitContext, + @Injectable UserServer.UserClientConnection connection) throws Throwable{ + Object [] expected = new Object[] {1, 1, 1, 0}; + runTest(bitContext, connection, expected, "functions/testIsNumericFunction.json"); + } + } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/0a28365a/exec/java-exec/src/test/resources/functions/testIsNumericFunction.json ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/test/resources/functions/testIsNumericFunction.json b/exec/java-exec/src/test/resources/functions/testIsNumericFunction.json new file mode 100644 index 0000000..2c4edb9 --- /dev/null +++ b/exec/java-exec/src/test/resources/functions/testIsNumericFunction.json @@ -0,0 +1,35 @@ +{ + head : { + version : 1, + generator : { + type : "optiq", + info : "na" + }, + type : "APACHE_DRILL_PHYSICAL" + }, + graph:[ + { + @id:1, + pop:"mock-sub-scan", + url: "http://apache.org", + entries:[ + {records: 1, types: [ + {name: "blue", type: "BIGINT", mode: "REQUIRED"} + ]} + ] + }, { + pop : "project", + @id : 2, + exprs : [ + { ref : "IS_NUM_1", expr : " isnumeric(500) "}, + { ref : "IS_NUM_2", expr : " isnumeric(-500) "}, + { ref : "IS_NUM_3", expr : " isnumeric(1000.000000000089) "}, + { ref : "IS_NUM_4", expr : " isnumeric('String') " } + ], + child : 1 + }, { + pop : "screen", + @id : 3, + child : 2 + } ] +}
