phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tdsi...@apache.org
Subject phoenix git commit: PHOENIX-1872 Use PrimitiveBooleanArray of boolean[] instead of PhoenixArray of Boolean[]
Date Fri, 17 Apr 2015 23:40:33 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master b597ba817 -> 5f3d3892f


PHOENIX-1872 Use PrimitiveBooleanArray of boolean[] instead of PhoenixArray of Boolean[]


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

Branch: refs/heads/master
Commit: 5f3d3892ff925f80289dc7b2ac918a91eedacfb1
Parents: b597ba8
Author: Alex <awarshavsky@salesforce.com>
Authored: Thu Apr 16 17:06:33 2015 -0700
Committer: Thomas <tdsilva@salesforce.com>
Committed: Fri Apr 17 16:17:51 2015 -0700

----------------------------------------------------------------------
 .../apache/phoenix/schema/types/PBoolean.java   | 18 ++++++++++--
 .../phoenix/schema/types/PBooleanArray.java     | 31 ++++++++++----------
 .../phoenix/schema/types/PhoenixArray.java      | 21 ++++++++-----
 .../expression/ArrayAppendFunctionTest.java     | 29 +++++++++++++-----
 .../types/BasePhoenixArrayToStringTest.java     | 11 ++++---
 .../schema/types/PDateArrayToStringTest.java    |  7 +++++
 ...rimitiveBooleanPhoenixArrayToStringTest.java |  6 ++++
 7 files changed, 87 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBoolean.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBoolean.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBoolean.java
index 93f6dc1..66991c5 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBoolean.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBoolean.java
@@ -17,11 +17,12 @@
  */
 package org.apache.phoenix.schema.types;
 
-import com.google.common.base.Preconditions;
-import com.google.common.primitives.Booleans;
+import java.sql.Types;
+
 import org.apache.phoenix.schema.SortOrder;
 
-import java.sql.Types;
+import com.google.common.base.Preconditions;
+import com.google.common.primitives.Booleans;
 
 public class PBoolean extends PDataType<Boolean> {
 
@@ -137,4 +138,15 @@ public class PBoolean extends PDataType<Boolean> {
   public Object getSampleValue(Integer maxLength, Integer arrayLength) {
     return RANDOM.get().nextBoolean();
   }
+  
+    @Override
+    public PhoenixArrayFactory getArrayFactory() {
+        return new PhoenixArrayFactory() {
+
+            @Override
+            public PhoenixArray newArray(PDataType type, Object[] elements) {
+                return new PhoenixArray.PrimitiveBooleanPhoenixArray(type, elements);
+            }
+        };
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBooleanArray.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBooleanArray.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBooleanArray.java
index dfad7e3..87bf9b4 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBooleanArray.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBooleanArray.java
@@ -17,10 +17,11 @@
  */
 package org.apache.phoenix.schema.types;
 
+import java.sql.Types;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.phoenix.schema.SortOrder;
-
-import java.sql.Types;
+import org.apache.phoenix.schema.types.PhoenixArray.PrimitiveBooleanPhoenixArray;
 
 public class PBooleanArray extends PArrayDataType<boolean[]> {
 
@@ -73,20 +74,20 @@ public class PBooleanArray extends PArrayDataType<boolean[]> {
     return isCoercibleTo(targetType, this);
   }
 
-  @Override
-  public boolean isCoercibleTo(PDataType targetType, Object value) {
-    if (value == null) {
-      return true;
-    }
-    PhoenixArray pArr = (PhoenixArray) value;
-    Object[] booleanArr = (Object[]) pArr.array;
-    for (Object i : booleanArr) {
-      if (!super.isCoercibleTo(PBoolean.INSTANCE, i)) {
-        return false;
-      }
+    @Override
+    public boolean isCoercibleTo(PDataType targetType, Object value) {
+        if (value == null) {
+            return true;
+        }
+        PrimitiveBooleanPhoenixArray pArr = (PrimitiveBooleanPhoenixArray) value;
+        boolean[] booleanArr = (boolean[]) pArr.array;
+        for (boolean b : booleanArr) {
+            if (!super.isCoercibleTo(PInteger.INSTANCE, b)) {
+                return false;
+            }
+        }
+        return true;
     }
-    return true;
-  }
 
   @Override
   public int getResultSetSqlType() {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
index 982834d..9ffac83 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
@@ -615,18 +615,25 @@ public class PhoenixArray implements Array,SQLCloseable {
 		public PrimitiveBooleanPhoenixArray(PDataType dataType, Object[] elements) {
 			super(dataType, elements);
 		}
+
 		@Override
-		public Object convertObjectArrayToPrimitiveArray(Object[] elements) {
-			booleanArr = new boolean[elements.length];
-			int i = 0;
-            for(Object o : elements) {
+		public boolean isPrimitiveType() {
+	        // boolean is primitive although PBoolean has no codec
+		    return true;
+		}
+		
+        @Override
+        public Object convertObjectArrayToPrimitiveArray(Object[] elements) {
+            booleanArr = new boolean[elements.length];
+            int i = 0;
+            for (Object o : elements) {
                 if (o != null) {
-                    booleanArr[i] = (Boolean)o;
+                    booleanArr[i] = (Boolean) o;
                 }
                 i++;
             }
-      return booleanArr;
-		}
+            return booleanArr;
+        }
 		
 		@Override
         public int estimateByteSize(int pos) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayAppendFunctionTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayAppendFunctionTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayAppendFunctionTest.java
index 2b4cb84..5405ef3 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayAppendFunctionTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayAppendFunctionTest.java
@@ -27,7 +27,21 @@ import java.util.List;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.phoenix.expression.function.ArrayAppendFunction;
 import org.apache.phoenix.schema.SortOrder;
-import org.apache.phoenix.schema.types.*;
+import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PChar;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDate;
+import org.apache.phoenix.schema.types.PDecimal;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.PFloat;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PLong;
+import org.apache.phoenix.schema.types.PSmallint;
+import org.apache.phoenix.schema.types.PTime;
+import org.apache.phoenix.schema.types.PTimestamp;
+import org.apache.phoenix.schema.types.PTinyint;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.schema.types.PhoenixArray;
 import org.junit.Test;
 
 import com.google.common.collect.Lists;
@@ -118,15 +132,16 @@ public class ArrayAppendFunctionTest {
     }
 
     @Test
-    public void testArrayAppendFunction5() throws Exception {
-        Boolean[] o = new Boolean[]{true, false, false, true};
-        Boolean[] o2 = new Boolean[]{true, false, false, true, false};
+    public void testArrayAppendFunctionBoolean() throws Exception {
+        Boolean[] o = new Boolean[] { true, false, false, true };
+        Boolean[] o2 = new Boolean[] { true, false, false, true, false };
         Boolean element = false;
         PDataType baseType = PBoolean.INSTANCE;
 
-        PhoenixArray arr = new PhoenixArray(baseType, o);
-        PhoenixArray expected = new PhoenixArray(baseType, o2);
-        test(arr, element, PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
null, null, baseType, null, null, expected, SortOrder.ASC, SortOrder.ASC);
+        PhoenixArray arr = new PhoenixArray.PrimitiveBooleanPhoenixArray(baseType, o);
+        PhoenixArray expected = new PhoenixArray.PrimitiveBooleanPhoenixArray(baseType, o2);
+        test(arr, element, PDataType.fromTypeId(baseType.getSqlType() + PDataType.ARRAY_TYPE_BASE),
+            null, null, baseType, null, null, expected, SortOrder.ASC, SortOrder.ASC);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/test/java/org/apache/phoenix/schema/types/BasePhoenixArrayToStringTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/BasePhoenixArrayToStringTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/BasePhoenixArrayToStringTest.java
index d67c599..1ec7742 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/BasePhoenixArrayToStringTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/BasePhoenixArrayToStringTest.java
@@ -28,10 +28,6 @@ public abstract class BasePhoenixArrayToStringTest {
 
     @Test
     public void testMultipleObjectArray() {
-        // Object[] arr = createArray(getBaseJavaType(), 3);
-        // arr[0] = getElement1();
-        // arr[1] = getElement2();
-        // arr[2] = getElement3();
         helpTestToString(getBaseType(),
             new Object[] { getElement1(), getElement2(), getElement3() }, "[" + getString1()
+ ", "
                     + getString2() + ", " + getString3() + "]");
@@ -75,7 +71,14 @@ public abstract class BasePhoenixArrayToStringTest {
 
     protected void helpTestToString(PDataType type, Object[] array, String expected) {
         PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(type, array);
+        boolean isPrimitive = isPrimitive(arr);
+        assertEquals("Expected " + getBaseType() + " array to be " + (isPrimitive ? "" :
"not ")
+                + "primitive.", isPrimitive, !arr.getClass().equals(PhoenixArray.class));
         assertEquals(expected, arr.toString());
     }
 
+    protected boolean isPrimitive(PhoenixArray arr) {
+        return arr.isPrimitiveType();
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDateArrayToStringTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDateArrayToStringTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDateArrayToStringTest.java
index 3243233..4ae210b 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDateArrayToStringTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PDateArrayToStringTest.java
@@ -28,6 +28,13 @@ public class PDateArrayToStringTest extends BasePhoenixArrayToStringTest
{
     }
 
     @Override
+    protected boolean isPrimitive(PhoenixArray arr) {
+        // dates have codec like primitive times but date is not primitive/scalar &&
there's no
+        // primitive date array
+        return false;
+    }
+
+    @Override
     protected Object getElement1() {
         return parseDate(DATE1);
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/5f3d3892/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PrimitiveBooleanPhoenixArrayToStringTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PrimitiveBooleanPhoenixArrayToStringTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PrimitiveBooleanPhoenixArrayToStringTest.java
index e62e592..1ec4752 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PrimitiveBooleanPhoenixArrayToStringTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/schema/types/PrimitiveBooleanPhoenixArrayToStringTest.java
@@ -18,6 +18,12 @@ public class PrimitiveBooleanPhoenixArrayToStringTest extends BasePhoenixArrayTo
     }
 
     @Override
+    protected String getNullString() {
+        // primitive arrays don't use null
+        return "false";
+    }
+
+    @Override
     protected Object getElement1() {
         return true;
     }


Mime
View raw message