phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gr...@apache.org
Subject [1/2] phoenix git commit: PHOENIX-1485 Correct arg count in FunctionParseNode
Date Wed, 03 Dec 2014 16:36:39 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master d6ba63570 -> 684976029


PHOENIX-1485 Correct arg count in FunctionParseNode

Correct the counting of required parameters for a function.


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

Branch: refs/heads/master
Commit: 6849760296465e1a87bd4d5d533661ec1fd1e0bc
Parents: 7121896
Author: Gabriel Reid <greid@apache.org>
Authored: Tue Dec 2 22:58:51 2014 +0100
Committer: Gabriel Reid <gabrielr@ngdata.com>
Committed: Wed Dec 3 17:29:29 2014 +0100

----------------------------------------------------------------------
 .../apache/phoenix/parse/FunctionParseNode.java |  48 ++++----
 .../phoenix/parse/BuiltInFunctionInfoTest.java  | 121 +++++++++++++++++++
 2 files changed, 145 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/68497602/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 ea8c1fb..59f7d3c 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
@@ -48,16 +48,16 @@ import org.apache.phoenix.util.SchemaUtil;
 
 
 /**
- * 
+ *
  * Node representing a function expression in SQL
  *
- * 
+ *
  * @since 0.1
  */
 public class FunctionParseNode extends CompoundParseNode {
     private final String name;
     private final BuiltInFunctionInfo info;
-    
+
     FunctionParseNode(String name, List<ParseNode> children, BuiltInFunctionInfo info)
{
         super(children);
         this.name = SchemaUtil.normalizeIdentifier(name);
@@ -67,7 +67,7 @@ public class FunctionParseNode extends CompoundParseNode {
     public BuiltInFunctionInfo getInfo() {
         return info;
     }
-    
+
     public String getName() {
         return name;
     }
@@ -80,7 +80,7 @@ public class FunctionParseNode extends CompoundParseNode {
         }
         return visitor.visitLeave(this, l);
     }
-    
+
     @Override
     public String toString() {
         StringBuilder buf = new StringBuilder(name + "(");
@@ -96,7 +96,7 @@ public class FunctionParseNode extends CompoundParseNode {
     public boolean isAggregate() {
         return getInfo().isAggregate();
     }
-    
+
     /**
      * Determines whether or not we can collapse a function expression to null if a required
      * parameter is null.
@@ -109,7 +109,7 @@ public class FunctionParseNode extends CompoundParseNode {
     public boolean evalToNullIfParamIsNull(StatementContext context, int index) throws SQLException
{
         return true;
     }
-    
+
 
     private static Constructor<? extends FunctionParseNode> getParseNodeCtor(Class<?
extends FunctionParseNode> clazz) {
         Constructor<? extends FunctionParseNode> ctor;
@@ -121,7 +121,7 @@ public class FunctionParseNode extends CompoundParseNode {
         ctor.setAccessible(true);
         return ctor;
     }
-    
+
     private static Constructor<? extends FunctionExpression> getExpressionCtor(Class<?
extends FunctionExpression> clazz) {
         Constructor<? extends FunctionExpression> ctor;
         try {
@@ -132,7 +132,7 @@ public class FunctionParseNode extends CompoundParseNode {
         ctor.setAccessible(true);
         return ctor;
     }
-    
+
     public List<Expression> validate(List<Expression> children, StatementContext
context) throws SQLException {
         BuiltInFunctionInfo info = this.getInfo();
         BuiltInFunctionArgInfo[] args = info.getArgs();
@@ -221,7 +221,7 @@ public class FunctionParseNode extends CompoundParseNode {
         }
         return children;
     }
-    
+
     /**
      * Entry point for parser to instantiate compiled representation of built-in function
      * @param children Compiled expressions for child nodes
@@ -245,7 +245,7 @@ public class FunctionParseNode extends CompoundParseNode {
             throw new SQLException(e);
         }
     }
-    
+
     /**
      * Marker used to indicate that a class should be used by DirectFunctionExpressionExec
below
      */
@@ -269,7 +269,7 @@ public class FunctionParseNode extends CompoundParseNode {
         String minValue() default "";
         String maxValue() default "";
     }
-    
+
     /**
      * Structure used to hold parse-time information about Function implementation classes
      */
@@ -290,8 +290,8 @@ public class FunctionParseNode extends CompoundParseNode {
             int requiredArgCount = 0;
             for (int i = 0; i < args.length; i++) {
                 this.args[i] = new BuiltInFunctionArgInfo(d.args()[i]);
-                if (requiredArgCount < i && this.args[i].getDefaultValue() !=
null) {
-                    requiredArgCount = i;
+                if (this.args[i].getDefaultValue() == null) {
+                    requiredArgCount = i + 1;
                 }
             }
             this.requiredArgCount = requiredArgCount;
@@ -301,7 +301,7 @@ public class FunctionParseNode extends CompoundParseNode {
         public int getRequiredArgCount() {
             return requiredArgCount;
         }
-        
+
         public String getName() {
             return name;
         }
@@ -317,12 +317,12 @@ public class FunctionParseNode extends CompoundParseNode {
         public boolean isAggregate() {
             return isAggregate;
         }
-        
+
         public BuiltInFunctionArgInfo[] getArgs() {
             return args;
         }
     }
-    
+
     @Immutable
     public static class BuiltInFunctionArgInfo {
         private static final PDataType[] ENUMERATION_TYPES = new PDataType[] {PDataType.VARCHAR};
@@ -332,10 +332,10 @@ public class FunctionParseNode extends CompoundParseNode {
         private final LiteralExpression defaultValue;
         private final LiteralExpression minValue;
         private final LiteralExpression maxValue;
-        
+
         @SuppressWarnings({ "unchecked", "rawtypes" })
         BuiltInFunctionArgInfo(Argument argument) {
-            
+
             if (argument.enumeration().length() > 0) {
                 this.isConstant = true;
                 this.defaultValue = null;
@@ -394,7 +394,7 @@ public class FunctionParseNode extends CompoundParseNode {
             }
             return exp;
         }
-        
+
         public boolean isConstant() {
             return isConstant;
         }
@@ -406,17 +406,17 @@ public class FunctionParseNode extends CompoundParseNode {
         public LiteralExpression getMinValue() {
             return minValue;
         }
-        
+
         public LiteralExpression getMaxValue() {
             return maxValue;
         }
-        
+
         public PDataType[] getAllowedTypes() {
             return allowedTypes;
         }
-        
+
         public Set<String> getAllowedValues() {
             return allowedValues;
         }
-    }    
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/68497602/phoenix-core/src/test/java/org/apache/phoenix/parse/BuiltInFunctionInfoTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/parse/BuiltInFunctionInfoTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/parse/BuiltInFunctionInfoTest.java
new file mode 100644
index 0000000..c5957d6
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/parse/BuiltInFunctionInfoTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+package org.apache.phoenix.parse;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.expression.function.FunctionExpression;
+import org.apache.phoenix.schema.PDataType;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.apache.phoenix.parse.FunctionParseNode.BuiltInFunctionInfo;
+import static org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
+import static org.apache.phoenix.parse.FunctionParseNode.Argument;
+import static org.junit.Assert.assertEquals;
+
+public class BuiltInFunctionInfoTest {
+
+    private static BuiltInFunctionInfo getBuiltInFunctionInfo(Class<? extends FunctionExpression>
funcClass) {
+        return new BuiltInFunctionInfo(funcClass, funcClass.getAnnotation(BuiltInFunction.class));
+    }
+
+    @Test
+    public void testConstruct_NoDefaultArgs() {
+        BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(NoDefaultArgsFunction.class);
+        assertEquals(2, funcInfo.getArgs().length);
+        assertEquals(2, funcInfo.getRequiredArgCount());
+        assertEquals("NO_DEFAULT_ARGS", funcInfo.getName());
+    }
+
+    @Test
+    public void testConstruct_WithOneDefaultArg() {
+        BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(WithOneDefaultArg.class);
+        assertEquals(3, funcInfo.getArgs().length);
+        assertEquals(2, funcInfo.getRequiredArgCount());
+        assertEquals("WITH_ONE_DEFAULT_ARG", funcInfo.getName());
+    }
+
+    @Test
+    public void testConstruct_WithMultipleDefaultArgs() {
+        BuiltInFunctionInfo funcInfo = getBuiltInFunctionInfo(WithMultipleDefaultArgs.class);
+        assertEquals(3, funcInfo.getArgs().length);
+        assertEquals(1, funcInfo.getRequiredArgCount());
+        assertEquals("WITH_MULTIPLE_DEFAULT_ARGS", funcInfo.getName());
+    }
+
+    private static class BaseFunctionAdapter extends FunctionExpression {
+
+
+        private final String name;
+
+        BaseFunctionAdapter(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+            throw new UnsupportedOperationException("Can't evalulate a BaseTestFunction");
+        }
+
+        @Override
+        public PDataType getDataType() {
+            return PDataType.VARCHAR;
+        }
+
+        @Override
+        public String getName() {
+            return name;
+        }
+    }
+
+    @BuiltInFunction(name="NO_DEFAULT_ARGS", args={
+            @Argument(allowedTypes={PDataType.VARCHAR}),
+            @Argument(allowedTypes={PDataType.VARCHAR})})
+    static class NoDefaultArgsFunction extends BaseFunctionAdapter {
+
+        public NoDefaultArgsFunction(List<Expression> ignoreChildren) {
+            super("NO_DEFAULT_ARGS");
+        }
+
+    }
+
+    @BuiltInFunction(name="WITH_ONE_DEFAULT_ARG", args={
+            @Argument(allowedTypes={PDataType.VARCHAR}),
+            @Argument(allowedTypes={PDataType.VARCHAR}),
+            @Argument(allowedTypes={PDataType.VARCHAR}, defaultValue = "'a'") })
+    static class WithOneDefaultArg extends BaseFunctionAdapter {
+
+        public WithOneDefaultArg(List<Expression> ignoreChildren) {
+            super("WITH_ONE_DEFAULT_ARG");
+        }
+    }
+
+    @BuiltInFunction(name="WITH_MULTIPLE_DEFAULT_ARGS", args={
+            @Argument(allowedTypes={PDataType.VARCHAR}),
+            @Argument(allowedTypes={PDataType.VARCHAR}, defaultValue = "'a'"),
+            @Argument(allowedTypes={PDataType.VARCHAR}, defaultValue = "'b'") })
+    static class WithMultipleDefaultArgs extends BaseFunctionAdapter {
+
+        public WithMultipleDefaultArgs(List<Expression> ignoreChildren) {
+            super("WITH_MULTIPLE_DEFAULT_ARGS");
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message