PHOENIX-3417 Refactor function argument validation with function argument info 
to separate method(Rajeshbabu)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/44c5a032
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/44c5a032
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/44c5a032

Branch: refs/heads/calcite
Commit: 44c5a032818bac2809ef664321542ec77f5df1f9
Parents: 8794f62
Author: Rajeshbabu Chintaguntla <rajeshb...@apache.org>
Authored: Fri Oct 28 12:27:07 2016 +0530
Committer: Rajeshbabu Chintaguntla <rajeshb...@apache.org>
Committed: Fri Oct 28 12:27:07 2016 +0530

----------------------------------------------------------------------
 .../apache/phoenix/parse/FunctionParseNode.java | 73 +++++++++++---------
 1 file changed, 40 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/44c5a032/phoenix-core/src/main/java/org/apache/phoenix/parse/FunctionParseNode.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/parse/FunctionParseNode.java 
b/phoenix-core/src/main/java/org/apache/phoenix/parse/FunctionParseNode.java
index 0dd021b..952d0d3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/FunctionParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/FunctionParseNode.java
@@ -186,44 +186,51 @@ public class FunctionParseNode extends CompoundParseNode {
                     }
                 }
             } else {
-                if (allowedTypes.length > 0) {
-                    boolean isCoercible = false;
-                    for (Class<? extends PDataType> type : allowedTypes) {
-                        if (child.getDataType().isCoercibleTo(
-                            
PDataTypeFactory.getInstance().instanceFromClass(type))) {
-                            isCoercible = true;
-                            break;
-                        }
-                    }
-                    if (!isCoercible) {
-                        throw new 
ArgumentTypeMismatchException(args[i].getAllowedTypes(),
-                            child.getDataType(), info.getName() + " argument " 
+ (i + 1));
-                    }
-                    if (child instanceof LiteralExpression) {
-                        LiteralExpression valueExp = (LiteralExpression) child;
-                        LiteralExpression minValue = args[i].getMinValue();
-                        LiteralExpression maxValue = args[i].getMaxValue();
-                        if (minValue != null && 
minValue.getDataType().compareTo(minValue.getValue(), valueExp.getValue(), 
valueExp.getDataType()) > 0) {
-                            throw new ValueRangeExcpetion(minValue, maxValue 
== null ? "" : maxValue, valueExp.getValue(), info.getName() + " argument " + 
(i + 1));
-                        }
-                        if (maxValue != null && 
maxValue.getDataType().compareTo(maxValue.getValue(), valueExp.getValue(), 
valueExp.getDataType()) < 0) {
-                            throw new ValueRangeExcpetion(minValue == null ? 
"" : minValue, maxValue, valueExp.getValue(), info.getName() + " argument " + 
(i + 1));
-                        }
-                    }
+                validateFunctionArguement(info, i, child);
+            }
+        }
+        return children;
+    }
+
+    public static void validateFunctionArguement(BuiltInFunctionInfo info,
+            int childIndex, Expression child)
+            throws ArgumentTypeMismatchException, ValueRangeExcpetion {
+        BuiltInFunctionArgInfo arg = info.getArgs()[childIndex];
+        if (arg.getAllowedTypes().length > 0) {
+            boolean isCoercible = false;
+            for (Class<? extends PDataType> type :arg.getAllowedTypes()) {
+                if (child.getDataType().isCoercibleTo(
+                    PDataTypeFactory.getInstance().instanceFromClass(type))) {
+                    isCoercible = true;
+                    break;
                 }
-                if (args[i].isConstant() && ! (child instanceof 
LiteralExpression) ) {
-                    throw new ArgumentTypeMismatchException("constant", 
child.toString(), info.getName() + " argument " + (i + 1));
+            }
+            if (!isCoercible) {
+                throw new ArgumentTypeMismatchException(arg.getAllowedTypes(),
+                    child.getDataType(), info.getName() + " argument " + 
(childIndex + 1));
+            }
+            if (child instanceof LiteralExpression) {
+                LiteralExpression valueExp = (LiteralExpression) child;
+                LiteralExpression minValue = arg.getMinValue();
+                LiteralExpression maxValue = arg.getMaxValue();
+                if (minValue != null && 
minValue.getDataType().compareTo(minValue.getValue(), valueExp.getValue(), 
valueExp.getDataType()) > 0) {
+                    throw new ValueRangeExcpetion(minValue, maxValue == null ? 
"" : maxValue, valueExp.getValue(), info.getName() + " argument " + (childIndex 
+ 1));
                 }
-                if (!args[i].getAllowedValues().isEmpty()) {
-                    Object value = ((LiteralExpression)child).getValue();
-                    if 
(!args[i].getAllowedValues().contains(value.toString().toUpperCase())) {
-                        throw new 
ArgumentTypeMismatchException(Arrays.toString(args[i].getAllowedValues().toArray(new
 String[0])),
-                                value.toString(), info.getName() + " argument 
" + (i + 1));
-                    }
+                if (maxValue != null && 
maxValue.getDataType().compareTo(maxValue.getValue(), valueExp.getValue(), 
valueExp.getDataType()) < 0) {
+                    throw new ValueRangeExcpetion(minValue == null ? "" : 
minValue, maxValue, valueExp.getValue(), info.getName() + " argument " + 
(childIndex + 1));
                 }
             }
         }
-        return children;
+        if (arg.isConstant() && ! (child instanceof LiteralExpression) ) {
+            throw new ArgumentTypeMismatchException("constant", 
child.toString(), info.getName() + " argument " + (childIndex + 1));
+        }
+        if (!arg.getAllowedValues().isEmpty()) {
+            Object value = ((LiteralExpression)child).getValue();
+            if 
(!arg.getAllowedValues().contains(value.toString().toUpperCase())) {
+                throw new 
ArgumentTypeMismatchException(Arrays.toString(arg.getAllowedValues().toArray(new
 String[0])),
+                        value.toString(), info.getName() + " argument " + 
(childIndex + 1));
+            }
+        }
     }
 
     /**

Reply via email to