Repository: phoenix Updated Branches: refs/heads/encodecolumns2 ada45e91a -> c1958d07e (forced update)
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/encodecolumns2 Commit: 67027d618f3817eea9ce215dc3e2999397995b83 Parents: 08d9c71 Author: Thomas D'Silva <tdsi...@salesforce.com> Authored: Wed Oct 5 20:58:03 2016 -0700 Committer: Thomas D'Silva <tdsi...@salesforce.com> 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<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()); + } +}