You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by td...@apache.org on 2016/10/07 04:19:48 UTC

phoenix git commit: PHOENIX-3296 ArrayConstructor expression does not serialize arrays with leading nulls correctly

Repository: phoenix
Updated Branches:
  refs/heads/master 34ba28e60 -> dfe671963


PHOENIX-3296 ArrayConstructor expression does not serialize arrays with leading nulls correctly


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

Branch: refs/heads/master
Commit: dfe671963e0001f0e99767c6a10806d59f3c47fe
Parents: 34ba28e
Author: Thomas D'Silva <td...@salesforce.com>
Authored: Wed Oct 5 20:58:03 2016 -0700
Committer: Thomas D'Silva <td...@salesforce.com>
Committed: Thu Oct 6 21:14:57 2016 -0700

----------------------------------------------------------------------
 .../expression/ArrayConstructorExpression.java  |  5 +-
 .../ArrayConstructorExpressionTest.java         | 63 ++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/dfe67196/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
index d8df29a..c2f4dd2 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
@@ -31,6 +31,7 @@ import org.apache.phoenix.util.TrustedByteArrayOutputStream;
 public class ArrayConstructorExpression extends BaseCompoundExpression {
     private PDataType baseType;
     private int position = -1;
+    private int nNulls = 0;
     private Object[] elements;
     private final ImmutableBytesWritable valuePtr = new ImmutableBytesWritable();
     private int estimatedSize = 0;
@@ -71,6 +72,7 @@ public class ArrayConstructorExpression extends BaseCompoundExpression {
     public void reset() {
         super.reset();
         position = 0;
+        nNulls = 0;
         Arrays.fill(elements, null);
         valuePtr.set(ByteUtil.EMPTY_BYTE_ARRAY);
     }
@@ -85,7 +87,7 @@ public class ArrayConstructorExpression extends BaseCompoundExpression {
         DataOutputStream oStream = new DataOutputStream(byteStream);
         try {
             int noOfElements =  children.size();
-            int nNulls = 0;
+            nNulls = 0;
             for (int i = position >= 0 ? position : 0; i < elements.length; i++) {
                 Expression child = children.get(i);
                 if (!child.evaluate(tuple, ptr)) {
@@ -115,6 +117,7 @@ public class ArrayConstructorExpression extends BaseCompoundExpression {
                             offsetPos[i] = byteStream.size();
                             oStream.write(ptr.get(), ptr.getOffset(), ptr.getLength());
                             oStream.write(PArrayDataType.getSeparatorByte(rowKeyOrderOptimizable, getSortOrder()));
+                            nNulls=0;
                         }
                     } else { // No nulls for fixed length
                         oStream.write(ptr.get(), ptr.getOffset(), ptr.getLength());

http://git-wip-us.apache.org/repos/asf/phoenix/blob/dfe67196/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConstructorExpressionTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConstructorExpressionTest.java b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConstructorExpressionTest.java
new file mode 100644
index 0000000..e99a71c
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/ArrayConstructorExpressionTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.expression;
+
+import static org.junit.Assert.assertArrayEquals;
+
+import java.util.List;
+
+import org.apache.phoenix.expression.function.ArrayElemRefExpression;
+import org.apache.phoenix.hbase.index.util.ImmutableBytesPtr;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.apache.phoenix.util.ByteUtil;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+public class ArrayConstructorExpressionTest {
+    
+    private static final byte[] BYTE_ARRAY1 = new byte[]{1,2,3,4,5};
+    private static final byte[] BYTE_ARRAY2 = new byte[]{6,7,8};
+
+    @Test
+    public void testArraysWithLeadingNulls() throws Exception {
+        List<Expression> children = Lists.newArrayListWithExpectedSize(4);
+        LiteralExpression nullExpression = LiteralExpression.newConstant(null);
+        children.add(nullExpression);
+        children.add(nullExpression);
+        children.add(LiteralExpression.newConstant(BYTE_ARRAY1, PVarbinary.INSTANCE));
+        children.add(LiteralExpression.newConstant(BYTE_ARRAY2, PVarbinary.INSTANCE));
+        ArrayConstructorExpression arrayConstructorExpression = new ArrayConstructorExpression(children, PVarbinary.INSTANCE, false);
+        ImmutableBytesPtr ptr = new ImmutableBytesPtr();
+        
+        ArrayElemRefExpression arrayElemRefExpression = new ArrayElemRefExpression(Lists.<Expression>newArrayList(arrayConstructorExpression));
+        
+        arrayElemRefExpression.setIndex(1);
+        arrayElemRefExpression.evaluate(null, ptr);
+        assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptr.copyBytesIfNecessary());
+        arrayElemRefExpression.setIndex(2);
+        arrayElemRefExpression.evaluate(null, ptr);
+        assertArrayEquals(ByteUtil.EMPTY_BYTE_ARRAY, ptr.copyBytesIfNecessary());
+        arrayElemRefExpression.setIndex(3);
+        arrayElemRefExpression.evaluate(null, ptr);
+        assertArrayEquals(BYTE_ARRAY1, ptr.copyBytesIfNecessary());
+        arrayElemRefExpression.setIndex(4);
+        arrayElemRefExpression.evaluate(null, ptr);
+        assertArrayEquals(BYTE_ARRAY2, ptr.copyBytesIfNecessary());
+    }
+}