Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 372A3200BA7 for ; Fri, 7 Oct 2016 06:20:40 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 35A9F160AE0; Fri, 7 Oct 2016 04:20:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 54DF1160ADB for ; Fri, 7 Oct 2016 06:20:39 +0200 (CEST) Received: (qmail 78140 invoked by uid 500); 7 Oct 2016 04:20:38 -0000 Mailing-List: contact commits-help@phoenix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@phoenix.apache.org Delivered-To: mailing list commits@phoenix.apache.org Received: (qmail 78131 invoked by uid 99); 7 Oct 2016 04:20:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 07 Oct 2016 04:20:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 09894E00DB; Fri, 7 Oct 2016 04:20:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: tdsilva@apache.org To: commits@phoenix.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: phoenix git commit: PHOENIX-3296 ArrayConstructor expression does not serialize arrays with leading nulls correctly Date: Fri, 7 Oct 2016 04:20:38 +0000 (UTC) archived-at: Fri, 07 Oct 2016 04:20:40 -0000 Repository: phoenix Updated Branches: refs/heads/4.x-HBase-0.98 08d9c7154 -> 67027d618 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/67027d61 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/67027d61 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/67027d61 Branch: refs/heads/4.x-HBase-0.98 Commit: 67027d618f3817eea9ce215dc3e2999397995b83 Parents: 08d9c71 Author: Thomas D'Silva Authored: Wed Oct 5 20:58:03 2016 -0700 Committer: Thomas D'Silva Committed: Thu Oct 6 21:08:22 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/67027d61/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/67027d61/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 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.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()); + } +}