Github user ramkrish86 commented on a diff in the pull request:

    https://github.com/apache/phoenix/pull/79#discussion_r29642510
  
    --- Diff: 
phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java 
---
    @@ -543,6 +544,151 @@ private static void writeEndBytes(byte[] array, int 
newOffsetArrayPosition, int
             Bytes.putByte(array, newOffsetArrayPosition + offsetArrayLength + 
byteSize + 2 * Bytes.SIZEOF_INT, header);
         }
     
    +    public static boolean prependItemToArray(ImmutableBytesWritable ptr, 
int length, int offset, byte[] arrayBytes, PDataType baseType, int arrayLength, 
Integer maxLength, SortOrder sortOrder) {
    +        int elementLength = maxLength == null ? ptr.getLength() : 
maxLength;
    +        if (ptr.getLength() == 0) {
    +            elementLength = 0;
    +        }
    +        //padding
    +        if (elementLength > ptr.getLength()) {
    +            baseType.pad(ptr, elementLength, sortOrder);
    +        }
    +        int elementOffset = ptr.getOffset();
    +        byte[] elementBytes = ptr.get();
    +
    +        byte[] newArray;
    +        if (!baseType.isFixedWidth()) {
    +            int offsetArrayPosition = Bytes.toInt(arrayBytes, offset + 
length - Bytes.SIZEOF_INT - Bytes.SIZEOF_INT - Bytes.SIZEOF_BYTE, 
Bytes.SIZEOF_INT);
    +            int offsetArrayLength = length - offsetArrayPosition - 
Bytes.SIZEOF_INT - Bytes.SIZEOF_INT - Bytes.SIZEOF_BYTE;
    +            arrayLength = Math.abs(arrayLength);
    +
    +            //checks whether offset array consists of shorts or integers
    +            boolean useInt = offsetArrayLength / arrayLength == 
Bytes.SIZEOF_INT;
    +            boolean convertToInt = false;
    +            int endElementPosition = getOffset(arrayBytes, arrayLength - 
1, !useInt, offsetArrayPosition + offset) + elementLength + Bytes.SIZEOF_BYTE;
    +            int newOffsetArrayPosition;
    +            int offsetShift;
    +            int firstNonNullElementPosition = 0;
    +            int currentPosition = 0;
    +            //handle the case where prepended element is null
    +            if (elementLength == 0) {
    +                int nulls = 1;
    +                //counts the number of nulls which are already at the 
beginning of the array
    +                for (int index = 0; index < arrayLength; index++) {
    +                    int currOffset = getOffset(arrayBytes, index, !useInt, 
offsetArrayPosition + offset);
    +                    if (arrayBytes[offset + currOffset] == 
QueryConstants.SEPARATOR_BYTE) {
    +                        nulls++;
    +                    } else {
    +                        //gets the offset of the first element after nulls 
at the beginning
    +                        firstNonNullElementPosition = currOffset;
    --- End diff --
    
    Yes I get that reading the code, but am just seeing if this logic can be 
simplified.  There can be two cases, one when the prepended element is null and 
another is not null. for now we will talk about the null case alone.  Unless 
the number of nulls is not going to be > 255 most of the case appending a  null 
is not going to change the length of the array right?  It is only going to 
increment the count of the array that would have been in offset 1.  When ever 
the null count reaches 255 and more ( or it is was already above 255) there 
could be a change in the length.  
    Just trying to simplify here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to