Repository: phoenix
Updated Branches:
  refs/heads/master 7a2944a0d -> 599030a8b


PHOENIX-1516 Add RAND() built-in function


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

Branch: refs/heads/master
Commit: 599030a8ba164039dc76a1cae7b9066ce97c4478
Parents: 7a2944a
Author: James Taylor <[email protected]>
Authored: Fri Jan 23 17:37:53 2015 -0800
Committer: James Taylor <[email protected]>
Committed: Fri Jan 23 17:37:53 2015 -0800

----------------------------------------------------------------------
 .../apache/phoenix/end2end/QueryTimeoutIT.java  |  2 +-
 .../expression/function/RandomFunction.java     | 16 ++++----
 .../expression/ArithmeticOperationTest.java     | 42 +++++++++++++++++---
 3 files changed, 44 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/599030a8/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryTimeoutIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryTimeoutIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryTimeoutIT.java
index fa3afa8..ba7b461 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryTimeoutIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/QueryTimeoutIT.java
@@ -58,7 +58,7 @@ public class QueryTimeoutIT extends 
BaseOwnClusterHBaseManagedTimeIT {
     
     @Test
     public void testQueryTimeout() throws Exception {
-        int nRows = 20000;
+        int nRows = 30000;
         Connection conn;
         Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
         conn = DriverManager.getConnection(getUrl(), props);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/599030a8/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RandomFunction.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RandomFunction.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RandomFunction.java
index 2783300..535a127 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RandomFunction.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/RandomFunction.java
@@ -13,8 +13,8 @@ import org.apache.phoenix.parse.FunctionParseNode.Argument;
 import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
 import org.apache.phoenix.schema.tuple.Tuple;
 import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDouble;
 import org.apache.phoenix.schema.types.PLong;
-import org.apache.phoenix.schema.types.PUnsignedDouble;
 
 /**
  * Random function that produces a unique value upon each invocation unless a 
seed is provided.
@@ -74,7 +74,7 @@ public class RandomFunction extends ScalarFunction {
         if (current == null) {
             current = random.nextDouble();
         }
-        ptr.set(PUnsignedDouble.INSTANCE.toBytes(current));
+        ptr.set(PDouble.INSTANCE.toBytes(current));
         return true;
     }
 
@@ -87,7 +87,7 @@ public class RandomFunction extends ScalarFunction {
 
     @Override
     public PDataType<?> getDataType() {
-        return PUnsignedDouble.INSTANCE;
+        return PDouble.INSTANCE;
     }
 
     @Override
@@ -97,7 +97,7 @@ public class RandomFunction extends ScalarFunction {
 
     @Override
     public Determinism getDeterminism() {
-        return Determinism.PER_INVOCATION;
+        return hasSeed ? Determinism.PER_ROW : Determinism.PER_INVOCATION;
     }
 
     @Override
@@ -108,17 +108,15 @@ public class RandomFunction extends ScalarFunction {
     // take the random object onto account
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + children.hashCode() + random.hashCode();
-        return result;
+        int hashCode = super.hashCode();
+        return hasSeed ? hashCode : (hashCode + random.hashCode());
     }
 
     // take the random object onto account, as otherwise we'll potentially 
collapse two
     // RAND() calls into a single one.
     @Override
     public boolean equals(Object obj) {
-        return super.equals(obj) && 
random.equals(((RandomFunction)obj).random);
+        return super.equals(obj) && (hasSeed || 
random.equals(((RandomFunction)obj).random));
     }
 
     // make sure we do not show the default 'null' parameter

http://git-wip-us.apache.org/repos/asf/phoenix/blob/599030a8/phoenix-core/src/test/java/org/apache/phoenix/expression/ArithmeticOperationTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArithmeticOperationTest.java
 
b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArithmeticOperationTest.java
index f0b983b..028e49a 100644
--- 
a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArithmeticOperationTest.java
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArithmeticOperationTest.java
@@ -18,6 +18,7 @@
 package org.apache.phoenix.expression;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -28,12 +29,7 @@ import java.util.List;
 
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.phoenix.exception.ValueTypeIncompatibleException;
-import org.apache.phoenix.expression.DecimalAddExpression;
-import org.apache.phoenix.expression.DecimalDivideExpression;
-import org.apache.phoenix.expression.DecimalMultiplyExpression;
-import org.apache.phoenix.expression.DecimalSubtractExpression;
-import org.apache.phoenix.expression.Expression;
-import org.apache.phoenix.expression.LiteralExpression;
+import org.apache.phoenix.expression.function.RandomFunction;
 import org.apache.phoenix.expression.visitor.CloneExpressionVisitor;
 import org.apache.phoenix.schema.types.PDataType;
 import org.apache.phoenix.schema.types.PDecimal;
@@ -259,6 +255,40 @@ public class ArithmeticOperationTest {
         assertEqualValue(e, PDecimal.INSTANCE, new 
BigDecimal("3.3333333333333333333333333333333333333"));
     }
 
+    @Test
+    public void testPerInvocationClone() throws Exception {
+        LiteralExpression op1, op2, op3, op4;
+        List<Expression> children;
+        Expression e1, e2, e3, e4;
+        ImmutableBytesWritable ptr1 = new ImmutableBytesWritable();
+        ImmutableBytesWritable ptr2 = new ImmutableBytesWritable();
+
+        op1 = LiteralExpression.newConstant(5.0);
+        op2 = LiteralExpression.newConstant(3.0);
+        op3 = LiteralExpression.newConstant(2.0);
+        op4 = LiteralExpression.newConstant(1.0);
+        children = Arrays.<Expression>asList(op1, op2);
+        e1 = new DoubleAddExpression(children);
+        children = Arrays.<Expression>asList(op3, op4);
+        e2 = new DoubleSubtractExpression(children);
+        e3 = new DoubleAddExpression(Arrays.<Expression>asList(e1, e2));
+        e4 = new DoubleAddExpression(Arrays.<Expression>asList(new 
RandomFunction(Arrays.<Expression>asList(LiteralExpression.newConstant(null))), 
e3));
+        CloneExpressionVisitor visitor = new CloneExpressionVisitor();
+        Expression clone = e4.accept(visitor);
+        assertTrue(clone != e4);
+        e4.evaluate(null, ptr1);
+        clone.evaluate(null, ptr2);
+        assertNotEquals(ptr1, ptr2);
+        
+        e4 = new DoubleAddExpression(Arrays.<Expression>asList(new 
RandomFunction(Arrays.<Expression>asList(LiteralExpression.newConstant(1))), 
e3));
+        visitor = new CloneExpressionVisitor();
+        clone = e4.accept(visitor);
+        assertTrue(clone == e4);
+        e4.evaluate(null, ptr1);
+        clone.evaluate(null, ptr2);
+        assertEquals(ptr1, ptr2);
+    }
+
     private static void assertEqualValue(Expression e, PDataType type, Object 
value) {
         ImmutableBytesWritable ptr = new ImmutableBytesWritable();
         boolean evaluated = e.evaluate(null, ptr);

Reply via email to