Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.type;
 
 
 
 import static com.facebook.presto.spi.block.SortOrder.ASC_NULLS_FIRST;
 import static com.facebook.presto.spi.block.SortOrder.ASC_NULLS_LAST;
 import static com.facebook.presto.spi.block.SortOrder.DESC_NULLS_FIRST;
 import static com.facebook.presto.spi.block.SortOrder.DESC_NULLS_LAST;
 import static com.facebook.presto.spi.type.TimeZoneKey.UTC_KEY;
 import static com.facebook.presto.type.TypeUtils.hashPosition;
 import static com.facebook.presto.type.TypeUtils.positionEqualsPosition;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static io.airlift.testing.Assertions.assertInstanceOf;
 import static java.util.Collections.unmodifiableSortedMap;
 import static java.util.Locale.ENGLISH;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 public abstract class AbstractTestType
 {
     public static final ConnectorSession SESSION = new ConnectorSession("user", System.currentTimeMillis(), null);
 
     private final Class<?> objectValueType;
     private final Block testBlock;
     private final Type type;
     private final SortedMap<IntegerObjectexpectedStackValues;
     private final SortedMap<IntegerObjectexpectedObjectValues;
     private final Block testBlockWithNulls;
 
     protected AbstractTestType(Type typeClass<?> objectValueTypeBlock testBlock)
     {
         this(typeobjectValueTypetestBlocktestBlock);
     }
 
     protected AbstractTestType(Type typeClass<?> objectValueTypeBlock testBlockBlock expectedValues)
     {
         this. = checkNotNull(type"type is null");
         this. = checkNotNull(objectValueType"objectValueType is null");
         this. = checkNotNull(testBlock"testBlock is null");
 
         checkNotNull(expectedValues"expectedValues is null");
         this. = indexStackValues(typeexpectedValues);
         this. = indexObjectValues(typeexpectedValues);
         this. = createAlternatingNullsBlock(testBlock);
     }
 
     private Block createAlternatingNullsBlock(Block testBlock)
     {
         BlockBuilder nullsBlockBuilder = .createBlockBuilder(new BlockBuilderStatus());
         for (int position = 0; position < testBlock.getPositionCount(); position++) {
             if (.getJavaType() == void.class) {
                 nullsBlockBuilder.appendNull();
             }
             else if (.getJavaType() == boolean.class) {
                 .writeBoolean(nullsBlockBuilder.getBoolean(testBlockposition));
             }
             else if (.getJavaType() == long.class) {
                 .writeLong(nullsBlockBuilder.getLong(testBlockposition));
             }
             else if (.getJavaType() == double.class) {
                 .writeDouble(nullsBlockBuilder.getDouble(testBlockposition));
             }
             else if (.getJavaType() == Slice.class) {
                 Slice slice = .getSlice(testBlockposition);
                 .writeSlice(nullsBlockBuilderslice, 0, slice.length());
             }
             else {
                 throw new RuntimeException("Unsupported Java type " + .getJavaType());
             }
             nullsBlockBuilder.appendNull();
         }
         return nullsBlockBuilder.build();
     }
 
    @Test
    public void testBlock()
    {
        for (Entry<IntegerObjectentry : .entrySet()) {
            assertPositionEquals(entry.getKey(), entry.getValue(), .get(entry.getKey()));
        }
        for (Entry<IntegerObjectentry : .entrySet()) {
            assertPositionEquals(entry.getKey() * 2, entry.getValue(), .get(entry.getKey()));
            assertPositionEquals(, (entry.getKey() * 2) + 1, nullnull);
        }
    }
    protected void assertPositionEquals(Block blockint positionObject expectedStackValueObject expectedObjectValue)
    {
        int hash = 0;
        if (.isComparable()) {
            hash = hashPosition(blockposition);
        }
        assertPositionValue(blockpositionexpectedStackValuehashexpectedObjectValue);
        assertPositionValue(block.getSingleValueBlock(position), 0, expectedStackValuehashexpectedObjectValue);
        assertPositionValue(block.getRegion(position, 1), 0, expectedStackValuehashexpectedObjectValue);
        assertPositionValue(block.getRegion(0, position + 1), positionexpectedStackValuehashexpectedObjectValue);
        assertPositionValue(block.getRegion(positionblock.getPositionCount() - position), 0, expectedStackValuehashexpectedObjectValue);
        BlockBuilder blockBuilder = .createBlockBuilder(new BlockBuilderStatus());
        .appendTo(blockpositionblockBuilder);
        assertPositionValue(blockBuilder.build(), 0, expectedStackValuehashexpectedObjectValue);
    }
    private void assertPositionValue(Block blockint positionObject expectedStackValueint expectedHashObject expectedObjectValue)
    {
        Object objectValue = .getObjectValue(blockposition);
        assertEquals(objectValueexpectedObjectValue);
        if (objectValue != null) {
            assertInstanceOf(objectValue);
        }
        if (.isComparable()) {
            assertEquals(hashPosition(blockposition), expectedHash);
        }
        else {
            try {
                .hash(blockposition);
                fail("Expected UnsupportedOperationException");
            }
            catch (UnsupportedOperationException expected) {
            }
        }
        Block expectedBlock = createBlock(expectedStackValue);
        if (.isComparable()) {
            assertTrue(positionEqualsPosition(blockpositionblockposition));
            assertTrue(positionEqualsPosition(blockpositionexpectedBlock, 0));
            assertTrue(positionEqualsPosition(expectedBlock, 0, blockposition));
        }
        assertEquals(block.isNull(position), expectedStackValue == null);
        if (.isOrderable()) {
            assertTrue(.compareBlockValue(blockpositionexpectedBlock, 0) == 0);
            assertTrue(.compareBlockValue(blockpositionexpectedBlock, 0) == 0);
            assertTrue(.compareBlockValue(blockpositionexpectedBlock, 0) == 0);
            assertTrue(.compareBlockValue(blockpositionexpectedBlock, 0) == 0);
        }
        else {
            try {
                .compareTo(blockpositionexpectedBlock, 0);
                fail("Expected UnsupportedOperationException");
            }
            catch (UnsupportedOperationException expected) {
            }
        }
        verifyInvalidPositionHandling(block);
        if (block.isNull(position)) {
            if (.isOrderable() && .getJavaType() != void.class) {
                Block nonNullValue = toBlock(getNonNullValue());
                assertTrue(.compareBlockValue(blockpositionnonNullValue, 0) < 0);
                assertTrue(.compareBlockValue(blockpositionnonNullValue, 0) > 0);
                assertTrue(.compareBlockValue(blockpositionnonNullValue, 0) < 0);
                assertTrue(.compareBlockValue(blockpositionnonNullValue, 0) > 0);
            }
            return;
        }
        if (.isOrderable() && expectedStackValue != .) {
            Block greaterValue = toBlock(getGreaterValue(expectedStackValue));
            assertTrue(.compareBlockValue(blockpositiongreaterValue, 0) < 0);
            assertTrue(.compareBlockValue(blockpositiongreaterValue, 0) < 0);
            assertTrue(.compareBlockValue(blockpositiongreaterValue, 0) > 0);
            assertTrue(.compareBlockValue(blockpositiongreaterValue, 0) > 0);
        }
        if (.getJavaType() == boolean.class) {
            assertEquals(.getBoolean(blockposition), expectedStackValue);
            try {
                .getLong(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
            try {
                .getDouble(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
        }
        else if (.getJavaType() == long.class) {
            assertEquals(.getLong(blockposition), expectedStackValue);
            try {
                .getBoolean(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
            try {
                .getDouble(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
        }
        else if (.getJavaType() == double.class) {
            assertEquals(.getDouble(blockposition), expectedStackValue);
            try {
                .getBoolean(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
            try {
                .getLong(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
        }
        else if (.getJavaType() == Slice.class) {
            assertEquals(.getSlice(blockposition), expectedStackValue);
            try {
                .getBoolean(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
            try {
                .getLong(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
            try {
                .getDouble(blockposition);
                fail("Expected IllegalStateException or UnsupportedOperationException");
            }
            catch (IllegalStateException | UnsupportedOperationException expected) {
            }
        }
    }
    private void verifyInvalidPositionHandling(Block block)
    {
        try {
            .getObjectValue(block, -1);
            fail("expected RuntimeException");
        }
        catch (RuntimeException expected) {
        }
        try {
            .getObjectValue(blockblock.getPositionCount());
            fail("expected RuntimeException");
        }
        catch (RuntimeException expected) {
        }
        try {
            .hash(block, -1);
            fail("expected RuntimeException");
        }
        catch (RuntimeException expected) {
        }
        try {
            .hash(blockblock.getPositionCount());
            fail("expected RuntimeException");
        }
        catch (RuntimeException expected) {
        }
        if (.isComparable() && .getJavaType() != void.class) {
            Block other = toBlock(getNonNullValue());
            try {
                .equalTo(block, -1, other, 0);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .equalTo(blockblock.getPositionCount(), other, 0);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
        if (.isOrderable() && .getJavaType() != void.class) {
            Block other = toBlock(getNonNullValue());
            try {
                .compareBlockValue(block, -1, other, 0);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .compareBlockValue(blockblock.getPositionCount(), other, 0);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
        if (.getJavaType() == boolean.class) {
            try {
                .getBoolean(block, -1);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .getBoolean(blockblock.getPositionCount());
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
        else if (.getJavaType() == long.class) {
            try {
                .getLong(block, -1);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .getLong(blockblock.getPositionCount());
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
        else if (.getJavaType() == double.class) {
            try {
                .getDouble(block, -1);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .getDouble(blockblock.getPositionCount());
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
        else if (.getJavaType() == Slice.class) {
            try {
                .getSlice(block, -1);
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
            try {
                .getSlice(blockblock.getPositionCount());
                fail("expected RuntimeException");
            }
            catch (RuntimeException expected) {
            }
        }
    }
    private static Block createBlock(Type typeObject value)
    {
        BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus());
        Class<?> javaType = type.getJavaType();
        if (value == null) {
            blockBuilder.appendNull();
        }
        else if (javaType == boolean.class) {
            type.writeBoolean(blockBuilder, (Booleanvalue);
        }
        else if (javaType == long.class) {
            type.writeLong(blockBuilder, (Longvalue);
        }
        else if (javaType == double.class) {
            type.writeDouble(blockBuilder, (Doublevalue);
        }
        else if (javaType == Slice.class) {
            Slice slice = (Slicevalue;
            type.writeSlice(blockBuilderslice, 0, slice.length());
        }
        else {
            throw new UnsupportedOperationException("not yet implemented: " + javaType);
        }
        return blockBuilder.build();
    }
    protected abstract Object getGreaterValue(Object value);
    protected Object getNonNullValue()
    {
        if (.getJavaType() == boolean.class) {
            return true;
        }
        if (.getJavaType() == long.class) {
            return 1L;
        }
        if (.getJavaType() == double.class) {
            return 1.0;
        }
        if (.getJavaType() == Slice.class) {
            return Slices.utf8Slice("_");
        }
        throw new IllegalStateException("Unsupported Java type " + .getJavaType());
    }
    private Block toBlock(Object value)
    {
        BlockBuilder blockBuilder = .createBlockBuilder(new BlockBuilderStatus());
        Class<?> javaType = .getJavaType();
        if (value == null) {
            blockBuilder.appendNull();
        }
        else if (javaType == boolean.class) {
            .writeBoolean(blockBuilder, (Booleanvalue);
        }
        else if (javaType == long.class) {
            .writeLong(blockBuilder, (Longvalue);
        }
        else if (javaType == double.class) {
            .writeDouble(blockBuilder, (Doublevalue);
        }
        else if (javaType == Slice.class) {
            Slice slice = (Slicevalue;
            .writeSlice(blockBuilderslice, 0, slice.length());
        }
        else {
            throw new UnsupportedOperationException("not yet implemented: " + javaType);
        }
        return blockBuilder.build();
    }
    private static SortedMap<IntegerObjectindexStackValues(Type typeBlock block)
    {
        SortedMap<IntegerObjectvalues = new TreeMap<>();
        for (int position = 0; position < block.getPositionCount(); position++) {
            if (block.isNull(position)) {
                values.put(positionnull);
            }
            else if (type.getJavaType() == boolean.class) {
                values.put(positiontype.getBoolean(blockposition));
            }
            else if (type.getJavaType() == long.class) {
                values.put(positiontype.getLong(blockposition));
            }
            else if (type.getJavaType() == double.class) {
                values.put(positiontype.getDouble(blockposition));
            }
            else if (type.getJavaType() == Slice.class) {
                values.put(positiontype.getSlice(blockposition));
            }
            else {
                throw new RuntimeException("Unsupported value type " + type.getJavaType());
            }
        }
        return unmodifiableSortedMap(values);
    }
    private static SortedMap<IntegerObjectindexObjectValues(Type typeBlock block)
    {
        SortedMap<IntegerObjectvalues = new TreeMap<>();
        for (int position = 0; position < block.getPositionCount(); position++) {
            values.put(positiontype.getObjectValue(blockposition));
        }
        return unmodifiableSortedMap(values);
    }
New to GrepCode? Check out our FAQ X