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/cdcbcf21 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/cdcbcf21 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/cdcbcf21 Branch: refs/heads/3.2 Commit: cdcbcf21815df293f68c9df85928be8cf29ed36e Parents: 5911b4a Author: Gabriel Reid <gr...@apache.org> Authored: Tue Dec 2 22:58:51 2014 +0100 Committer: Gabriel Reid <gabri...@ngdata.com> Committed: Wed Dec 3 17:32:41 2014 +0100 ---------------------------------------------------------------------- .../apache/phoenix/parse/FunctionParseNode.java | 4 +- .../phoenix/parse/BuiltInFunctionInfoTest.java | 121 +++++++++++++++++++ 2 files changed, 123 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/cdcbcf21/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..de1d7bb 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 @@ -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; http://git-wip-us.apache.org/repos/asf/phoenix/blob/cdcbcf21/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